1 | /**
|
2 | * @fileoverview Rule to flag missing semicolons.
|
3 | * @author Nicholas C. Zakas
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | const FixTracker = require("../util/fix-tracker");
|
12 | const astUtils = require("../ast-utils");
|
13 |
|
14 | //------------------------------------------------------------------------------
|
15 | // Rule Definition
|
16 | //------------------------------------------------------------------------------
|
17 |
|
18 | module.exports = {
|
19 | meta: {
|
20 | docs: {
|
21 | description: "require or disallow semicolons instead of ASI",
|
22 | category: "Stylistic Issues",
|
23 | recommended: false,
|
24 | url: "https://eslint.org/docs/rules/semi"
|
25 | },
|
26 |
|
27 | fixable: "code",
|
28 |
|
29 | schema: {
|
30 | anyOf: [
|
31 | {
|
32 | type: "array",
|
33 | items: [
|
34 | {
|
35 | enum: ["never"]
|
36 | },
|
37 | {
|
38 | type: "object",
|
39 | properties: {
|
40 | beforeStatementContinuationChars: {
|
41 | enum: ["always", "any", "never"]
|
42 | }
|
43 | },
|
44 | additionalProperties: false
|
45 | }
|
46 | ],
|
47 | minItems: 0,
|
48 | maxItems: 2
|
49 | },
|
50 | {
|
51 | type: "array",
|
52 | items: [
|
53 | {
|
54 | enum: ["always"]
|
55 | },
|
56 | {
|
57 | type: "object",
|
58 | properties: {
|
59 | omitLastInOneLineBlock: { type: "boolean" }
|
60 | },
|
61 | additionalProperties: false
|
62 | }
|
63 | ],
|
64 | minItems: 0,
|
65 | maxItems: 2
|
66 | }
|
67 | ]
|
68 | }
|
69 | },
|
70 |
|
71 | create(context) {
|
72 |
|
73 | const OPT_OUT_PATTERN = /^[-[(/+`]/; // One of [(/+-`
|
74 | const options = context.options[1];
|
75 | const never = context.options[0] === "never";
|
76 | const exceptOneLine = Boolean(options && options.omitLastInOneLineBlock);
|
77 | const beforeStatementContinuationChars = (options && options.beforeStatementContinuationChars) || "any";
|
78 | const sourceCode = context.getSourceCode();
|
79 |
|
80 | //--------------------------------------------------------------------------
|
81 | // Helpers
|
82 | //--------------------------------------------------------------------------
|
83 |
|
84 | /**
|
85 | * Reports a semicolon error with appropriate location and message.
|
86 | * @param {ASTNode} node The node with an extra or missing semicolon.
|
87 | * @param {boolean} missing True if the semicolon is missing.
|
88 | * @returns {void}
|
89 | */
|
90 | function report(node, missing) {
|
91 | const lastToken = sourceCode.getLastToken(node);
|
92 | let message,
|
93 | fix,
|
94 | loc = lastToken.loc;
|
95 |
|
96 | if (!missing) {
|
97 | message = "Missing semicolon.";
|
98 | loc = loc.end;
|
99 | fix = function(fixer) {
|
100 | return fixer.insertTextAfter(lastToken, ";");
|
101 | };
|
102 | } else {
|
103 | message = "Extra semicolon.";
|
104 | loc = loc.start;
|
105 | fix = function(fixer) {
|
106 |
|
107 | /*
|
108 | * Expand the replacement range to include the surrounding
|
109 | * tokens to avoid conflicting with no-extra-semi.
|
110 | * https://github.com/eslint/eslint/issues/7928
|
111 | */
|
112 | return new FixTracker(fixer, sourceCode)
|
113 | .retainSurroundingTokens(lastToken)
|
114 | .remove(lastToken);
|
115 | };
|
116 | }
|
117 |
|
118 | context.report({
|
119 | node,
|
120 | loc,
|
121 | message,
|
122 | fix
|
123 | });
|
124 |
|
125 | }
|
126 |
|
127 | /**
|
128 | * Check whether a given semicolon token is redandant.
|
129 | * @param {Token} semiToken A semicolon token to check.
|
130 | * @returns {boolean} `true` if the next token is `;` or `}`.
|
131 | */
|
132 | function isRedundantSemi(semiToken) {
|
133 | const nextToken = sourceCode.getTokenAfter(semiToken);
|
134 |
|
135 | return (
|
136 | !nextToken ||
|
137 | astUtils.isClosingBraceToken(nextToken) ||
|
138 | astUtils.isSemicolonToken(nextToken)
|
139 | );
|
140 | }
|
141 |
|
142 | /**
|
143 | * Check whether a given token is the closing brace of an arrow function.
|
144 | * @param {Token} lastToken A token to check.
|
145 | * @returns {boolean} `true` if the token is the closing brace of an arrow function.
|
146 | */
|
147 | function isEndOfArrowBlock(lastToken) {
|
148 | if (!astUtils.isClosingBraceToken(lastToken)) {
|
149 | return false;
|
150 | }
|
151 | const node = sourceCode.getNodeByRangeIndex(lastToken.range[0]);
|
152 |
|
153 | return (
|
154 | node.type === "BlockStatement" &&
|
155 | node.parent.type === "ArrowFunctionExpression"
|
156 | );
|
157 | }
|
158 |
|
159 | /**
|
160 | * Check whether a given node is on the same line with the next token.
|
161 | * @param {Node} node A statement node to check.
|
162 | * @returns {boolean} `true` if the node is on the same line with the next token.
|
163 | */
|
164 | function isOnSameLineWithNextToken(node) {
|
165 | const prevToken = sourceCode.getLastToken(node, 1);
|
166 | const nextToken = sourceCode.getTokenAfter(node);
|
167 |
|
168 | return !!nextToken && astUtils.isTokenOnSameLine(prevToken, nextToken);
|
169 | }
|
170 |
|
171 | /**
|
172 | * Check whether a given node can connect the next line if the next line is unreliable.
|
173 | * @param {Node} node A statement node to check.
|
174 | * @returns {boolean} `true` if the node can connect the next line.
|
175 | */
|
176 | function maybeAsiHazardAfter(node) {
|
177 | const t = node.type;
|
178 |
|
179 | if (t === "DoWhileStatement" ||
|
180 | t === "BreakStatement" ||
|
181 | t === "ContinueStatement" ||
|
182 | t === "DebuggerStatement" ||
|
183 | t === "ImportDeclaration" ||
|
184 | t === "ExportAllDeclaration"
|
185 | ) {
|
186 | return false;
|
187 | }
|
188 | if (t === "ReturnStatement") {
|
189 | return Boolean(node.argument);
|
190 | }
|
191 | if (t === "ExportNamedDeclaration") {
|
192 | return Boolean(node.declaration);
|
193 | }
|
194 | if (isEndOfArrowBlock(sourceCode.getLastToken(node, 1))) {
|
195 | return false;
|
196 | }
|
197 |
|
198 | return true;
|
199 | }
|
200 |
|
201 | /**
|
202 | * Check whether a given token can connect the previous statement.
|
203 | * @param {Token} token A token to check.
|
204 | * @returns {boolean} `true` if the token is one of `[`, `(`, `/`, `+`, `-`, ```, `++`, and `--`.
|
205 | */
|
206 | function maybeAsiHazardBefore(token) {
|
207 | return (
|
208 | Boolean(token) &&
|
209 | OPT_OUT_PATTERN.test(token.value) &&
|
210 | token.value !== "++" &&
|
211 | token.value !== "--"
|
212 | );
|
213 | }
|
214 |
|
215 | /**
|
216 | * Check if the semicolon of a given node is unnecessary, only true if:
|
217 | * - next token is a valid statement divider (`;` or `}`).
|
218 | * - next token is on a new line and the node is not connectable to the new line.
|
219 | * @param {Node} node A statement node to check.
|
220 | * @returns {boolean} whether the semicolon is unnecessary.
|
221 | */
|
222 | function canRemoveSemicolon(node) {
|
223 | if (isRedundantSemi(sourceCode.getLastToken(node))) {
|
224 | return true; // `;;` or `;}`
|
225 | }
|
226 | if (isOnSameLineWithNextToken(node)) {
|
227 | return false; // One liner.
|
228 | }
|
229 | if (beforeStatementContinuationChars === "never" && !maybeAsiHazardAfter(node)) {
|
230 | return true; // ASI works. This statement doesn't connect to the next.
|
231 | }
|
232 | if (!maybeAsiHazardBefore(sourceCode.getTokenAfter(node))) {
|
233 | return true; // ASI works. The next token doesn't connect to this statement.
|
234 | }
|
235 |
|
236 | return false;
|
237 | }
|
238 |
|
239 | /**
|
240 | * Checks a node to see if it's in a one-liner block statement.
|
241 | * @param {ASTNode} node The node to check.
|
242 | * @returns {boolean} whether the node is in a one-liner block statement.
|
243 | */
|
244 | function isOneLinerBlock(node) {
|
245 | const parent = node.parent;
|
246 | const nextToken = sourceCode.getTokenAfter(node);
|
247 |
|
248 | if (!nextToken || nextToken.value !== "}") {
|
249 | return false;
|
250 | }
|
251 | return (
|
252 | !!parent &&
|
253 | parent.type === "BlockStatement" &&
|
254 | parent.loc.start.line === parent.loc.end.line
|
255 | );
|
256 | }
|
257 |
|
258 | /**
|
259 | * Checks a node to see if it's followed by a semicolon.
|
260 | * @param {ASTNode} node The node to check.
|
261 | * @returns {void}
|
262 | */
|
263 | function checkForSemicolon(node) {
|
264 | const isSemi = astUtils.isSemicolonToken(sourceCode.getLastToken(node));
|
265 |
|
266 | if (never) {
|
267 | if (isSemi && canRemoveSemicolon(node)) {
|
268 | report(node, true);
|
269 | } else if (!isSemi && beforeStatementContinuationChars === "always" && maybeAsiHazardBefore(sourceCode.getTokenAfter(node))) {
|
270 | report(node);
|
271 | }
|
272 | } else {
|
273 | const oneLinerBlock = (exceptOneLine && isOneLinerBlock(node));
|
274 |
|
275 | if (isSemi && oneLinerBlock) {
|
276 | report(node, true);
|
277 | } else if (!isSemi && !oneLinerBlock) {
|
278 | report(node);
|
279 | }
|
280 | }
|
281 | }
|
282 |
|
283 | /**
|
284 | * Checks to see if there's a semicolon after a variable declaration.
|
285 | * @param {ASTNode} node The node to check.
|
286 | * @returns {void}
|
287 | */
|
288 | function checkForSemicolonForVariableDeclaration(node) {
|
289 | const parent = node.parent;
|
290 |
|
291 | if ((parent.type !== "ForStatement" || parent.init !== node) &&
|
292 | (!/^For(?:In|Of)Statement/.test(parent.type) || parent.left !== node)
|
293 | ) {
|
294 | checkForSemicolon(node);
|
295 | }
|
296 | }
|
297 |
|
298 | //--------------------------------------------------------------------------
|
299 | // Public API
|
300 | //--------------------------------------------------------------------------
|
301 |
|
302 | return {
|
303 | VariableDeclaration: checkForSemicolonForVariableDeclaration,
|
304 | ExpressionStatement: checkForSemicolon,
|
305 | ReturnStatement: checkForSemicolon,
|
306 | ThrowStatement: checkForSemicolon,
|
307 | DoWhileStatement: checkForSemicolon,
|
308 | DebuggerStatement: checkForSemicolon,
|
309 | BreakStatement: checkForSemicolon,
|
310 | ContinueStatement: checkForSemicolon,
|
311 | ImportDeclaration: checkForSemicolon,
|
312 | ExportAllDeclaration: checkForSemicolon,
|
313 | ExportNamedDeclaration(node) {
|
314 | if (!node.declaration) {
|
315 | checkForSemicolon(node);
|
316 | }
|
317 | },
|
318 | ExportDefaultDeclaration(node) {
|
319 | if (!/(?:Class|Function)Declaration/.test(node.declaration.type)) {
|
320 | checkForSemicolon(node);
|
321 | }
|
322 | }
|
323 | };
|
324 |
|
325 | }
|
326 | };
|