| 1 |
const ruleComposer = require("eslint-rule-composer"); |
| 2 |
const eslint = require("eslint"); |
| 3 |
const eslintVersion = eslint.ESLint.version; |
| 4 |
|
| 5 |
const OPT_OUT_PATTERN = /^[-[(/+`]/; |
| 6 |
|
| 7 |
const rule = new eslint.Linter().getRules().get("semi"); |
| 8 |
|
| 9 |
function isSemicolon(token) { |
| 10 |
return token.type === "Punctuator" && token.value === ";"; |
| 11 |
} |
| 12 |
|
| 13 |
function isUnnecessarySemicolon(context, lastToken) { |
| 14 |
if (!isSemicolon(lastToken)) { |
| 15 |
return false; |
| 16 |
} |
| 17 |
|
| 18 |
const nextToken = context.getSourceCode().getTokenAfter(lastToken); |
| 19 |
|
| 20 |
if (!nextToken) { |
| 21 |
return true; |
| 22 |
} |
| 23 |
|
| 24 |
const lastTokenLine = lastToken.loc.end.line; |
| 25 |
const nextTokenLine = nextToken.loc.start.line; |
| 26 |
const isOptOutToken = |
| 27 |
OPT_OUT_PATTERN.test(nextToken.value) && |
| 28 |
nextToken.value !== "++" && |
| 29 |
nextToken.value !== "--"; |
| 30 |
const isDivider = nextToken.value === "}" || nextToken.value === ";"; |
| 31 |
|
| 32 |
return (lastTokenLine !== nextTokenLine && !isOptOutToken) || isDivider; |
| 33 |
} |
| 34 |
|
| 35 |
function isOneLinerBlock(context, node) { |
| 36 |
const nextToken = context.getSourceCode().getTokenAfter(node); |
| 37 |
|
| 38 |
if (!nextToken || nextToken.value !== "}") { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
const parent = node.parent; |
| 43 |
|
| 44 |
return ( |
| 45 |
parent && |
| 46 |
parent.type === "BlockStatement" && |
| 47 |
parent.loc.start.line === parent.loc.end.line |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
function report(context, node, missing) { |
| 52 |
const lastToken = context.getSourceCode().getLastToken(node); |
| 53 |
|
| 54 |
let message, |
| 55 |
fix, |
| 56 |
loc = lastToken.loc; |
| 57 |
|
| 58 |
if (!missing) { |
| 59 |
message = "Missing semicolon."; |
| 60 |
loc = loc.end; |
| 61 |
fix = function (fixer) { |
| 62 |
return fixer.insertTextAfter(lastToken, ";"); |
| 63 |
}; |
| 64 |
} else { |
| 65 |
message = "Extra semicolon."; |
| 66 |
loc = loc.start; |
| 67 |
fix = function (fixer) { |
| 68 |
return fixer.remove(lastToken); |
| 69 |
}; |
| 70 |
} |
| 71 |
|
| 72 |
context.report({ |
| 73 |
node, |
| 74 |
loc, |
| 75 |
message, |
| 76 |
fix, |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
if (parseInt(eslintVersion, 10) >= 8) { |
| 81 |
|
| 82 |
|
| 83 |
module.exports = rule; |
| 84 |
} else { |
| 85 |
module.exports = ruleComposer.joinReports([ |
| 86 |
rule, |
| 87 |
context => ({ |
| 88 |
"ClassProperty, ClassPrivateProperty, PropertyDefinition"(node) { |
| 89 |
const options = context.options[1]; |
| 90 |
const exceptOneLine = |
| 91 |
options && options.omitLastInOneLineBlock === true; |
| 92 |
const sourceCode = context.getSourceCode(); |
| 93 |
const lastToken = sourceCode.getLastToken(node); |
| 94 |
|
| 95 |
if (context.options[0] === "never") { |
| 96 |
if (isUnnecessarySemicolon(context, lastToken)) { |
| 97 |
report(context, node, true); |
| 98 |
} |
| 99 |
} else { |
| 100 |
if (!isSemicolon(lastToken)) { |
| 101 |
if (!exceptOneLine || !isOneLinerBlock(context, node)) { |
| 102 |
report(context, node); |
| 103 |
} |
| 104 |
} else { |
| 105 |
if (exceptOneLine && isOneLinerBlock(context, node)) { |
| 106 |
report(context, node, true); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
}, |
| 111 |
}), |
| 112 |
]); |
| 113 |
} |