UNPKG

21.6 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to enforce spacing before and after keywords.
3 * @author Toru Nagashima
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const astUtils = require("../ast-utils"),
13 keywords = require("../util/keywords");
14
15//------------------------------------------------------------------------------
16// Constants
17//------------------------------------------------------------------------------
18
19const PREV_TOKEN = /^[)\]}>]$/;
20const NEXT_TOKEN = /^(?:[([{<~!]|\+\+?|--?)$/;
21const PREV_TOKEN_M = /^[)\]}>*]$/;
22const NEXT_TOKEN_M = /^[{*]$/;
23const TEMPLATE_OPEN_PAREN = /\$\{$/;
24const TEMPLATE_CLOSE_PAREN = /^\}/;
25const CHECK_TYPE = /^(?:JSXElement|RegularExpression|String|Template)$/;
26const KEYS = keywords.concat(["as", "async", "await", "from", "get", "let", "of", "set", "yield"]);
27
28// check duplications.
29(function() {
30 KEYS.sort();
31 for (let i = 1; i < KEYS.length; ++i) {
32 if (KEYS[i] === KEYS[i - 1]) {
33 throw new Error(`Duplication was found in the keyword list: ${KEYS[i]}`);
34 }
35 }
36}());
37
38//------------------------------------------------------------------------------
39// Helpers
40//------------------------------------------------------------------------------
41
42/**
43 * Checks whether or not a given token is a "Template" token ends with "${".
44 *
45 * @param {Token} token - A token to check.
46 * @returns {boolean} `true` if the token is a "Template" token ends with "${".
47 */
48function isOpenParenOfTemplate(token) {
49 return token.type === "Template" && TEMPLATE_OPEN_PAREN.test(token.value);
50}
51
52/**
53 * Checks whether or not a given token is a "Template" token starts with "}".
54 *
55 * @param {Token} token - A token to check.
56 * @returns {boolean} `true` if the token is a "Template" token starts with "}".
57 */
58function isCloseParenOfTemplate(token) {
59 return token.type === "Template" && TEMPLATE_CLOSE_PAREN.test(token.value);
60}
61
62//------------------------------------------------------------------------------
63// Rule Definition
64//------------------------------------------------------------------------------
65
66module.exports = {
67 meta: {
68 docs: {
69 description: "enforce consistent spacing before and after keywords",
70 category: "Stylistic Issues",
71 recommended: false,
72 url: "https://eslint.org/docs/rules/keyword-spacing"
73 },
74
75 fixable: "whitespace",
76
77 schema: [
78 {
79 type: "object",
80 properties: {
81 before: { type: "boolean" },
82 after: { type: "boolean" },
83 overrides: {
84 type: "object",
85 properties: KEYS.reduce((retv, key) => {
86 retv[key] = {
87 type: "object",
88 properties: {
89 before: { type: "boolean" },
90 after: { type: "boolean" }
91 },
92 additionalProperties: false
93 };
94 return retv;
95 }, {}),
96 additionalProperties: false
97 }
98 },
99 additionalProperties: false
100 }
101 ]
102 },
103
104 create(context) {
105 const sourceCode = context.getSourceCode();
106
107 /**
108 * Reports a given token if there are not space(s) before the token.
109 *
110 * @param {Token} token - A token to report.
111 * @param {RegExp|undefined} pattern - Optional. A pattern of the previous
112 * token to check.
113 * @returns {void}
114 */
115 function expectSpaceBefore(token, pattern) {
116 pattern = pattern || PREV_TOKEN;
117
118 const prevToken = sourceCode.getTokenBefore(token);
119
120 if (prevToken &&
121 (CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) &&
122 !isOpenParenOfTemplate(prevToken) &&
123 astUtils.isTokenOnSameLine(prevToken, token) &&
124 !sourceCode.isSpaceBetweenTokens(prevToken, token)
125 ) {
126 context.report({
127 loc: token.loc.start,
128 message: "Expected space(s) before \"{{value}}\".",
129 data: token,
130 fix(fixer) {
131 return fixer.insertTextBefore(token, " ");
132 }
133 });
134 }
135 }
136
137 /**
138 * Reports a given token if there are space(s) before the token.
139 *
140 * @param {Token} token - A token to report.
141 * @param {RegExp|undefined} pattern - Optional. A pattern of the previous
142 * token to check.
143 * @returns {void}
144 */
145 function unexpectSpaceBefore(token, pattern) {
146 pattern = pattern || PREV_TOKEN;
147
148 const prevToken = sourceCode.getTokenBefore(token);
149
150 if (prevToken &&
151 (CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) &&
152 !isOpenParenOfTemplate(prevToken) &&
153 astUtils.isTokenOnSameLine(prevToken, token) &&
154 sourceCode.isSpaceBetweenTokens(prevToken, token)
155 ) {
156 context.report({
157 loc: token.loc.start,
158 message: "Unexpected space(s) before \"{{value}}\".",
159 data: token,
160 fix(fixer) {
161 return fixer.removeRange([prevToken.range[1], token.range[0]]);
162 }
163 });
164 }
165 }
166
167 /**
168 * Reports a given token if there are not space(s) after the token.
169 *
170 * @param {Token} token - A token to report.
171 * @param {RegExp|undefined} pattern - Optional. A pattern of the next
172 * token to check.
173 * @returns {void}
174 */
175 function expectSpaceAfter(token, pattern) {
176 pattern = pattern || NEXT_TOKEN;
177
178 const nextToken = sourceCode.getTokenAfter(token);
179
180 if (nextToken &&
181 (CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) &&
182 !isCloseParenOfTemplate(nextToken) &&
183 astUtils.isTokenOnSameLine(token, nextToken) &&
184 !sourceCode.isSpaceBetweenTokens(token, nextToken)
185 ) {
186 context.report({
187 loc: token.loc.start,
188 message: "Expected space(s) after \"{{value}}\".",
189 data: token,
190 fix(fixer) {
191 return fixer.insertTextAfter(token, " ");
192 }
193 });
194 }
195 }
196
197 /**
198 * Reports a given token if there are space(s) after the token.
199 *
200 * @param {Token} token - A token to report.
201 * @param {RegExp|undefined} pattern - Optional. A pattern of the next
202 * token to check.
203 * @returns {void}
204 */
205 function unexpectSpaceAfter(token, pattern) {
206 pattern = pattern || NEXT_TOKEN;
207
208 const nextToken = sourceCode.getTokenAfter(token);
209
210 if (nextToken &&
211 (CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) &&
212 !isCloseParenOfTemplate(nextToken) &&
213 astUtils.isTokenOnSameLine(token, nextToken) &&
214 sourceCode.isSpaceBetweenTokens(token, nextToken)
215 ) {
216 context.report({
217 loc: token.loc.start,
218 message: "Unexpected space(s) after \"{{value}}\".",
219 data: token,
220 fix(fixer) {
221 return fixer.removeRange([token.range[1], nextToken.range[0]]);
222 }
223 });
224 }
225 }
226
227 /**
228 * Parses the option object and determines check methods for each keyword.
229 *
230 * @param {Object|undefined} options - The option object to parse.
231 * @returns {Object} - Normalized option object.
232 * Keys are keywords (there are for every keyword).
233 * Values are instances of `{"before": function, "after": function}`.
234 */
235 function parseOptions(options) {
236 const before = !options || options.before !== false;
237 const after = !options || options.after !== false;
238 const defaultValue = {
239 before: before ? expectSpaceBefore : unexpectSpaceBefore,
240 after: after ? expectSpaceAfter : unexpectSpaceAfter
241 };
242 const overrides = (options && options.overrides) || {};
243 const retv = Object.create(null);
244
245 for (let i = 0; i < KEYS.length; ++i) {
246 const key = KEYS[i];
247 const override = overrides[key];
248
249 if (override) {
250 const thisBefore = ("before" in override) ? override.before : before;
251 const thisAfter = ("after" in override) ? override.after : after;
252
253 retv[key] = {
254 before: thisBefore ? expectSpaceBefore : unexpectSpaceBefore,
255 after: thisAfter ? expectSpaceAfter : unexpectSpaceAfter
256 };
257 } else {
258 retv[key] = defaultValue;
259 }
260 }
261
262 return retv;
263 }
264
265 const checkMethodMap = parseOptions(context.options[0]);
266
267 /**
268 * Reports a given token if usage of spacing followed by the token is
269 * invalid.
270 *
271 * @param {Token} token - A token to report.
272 * @param {RegExp|undefined} pattern - Optional. A pattern of the previous
273 * token to check.
274 * @returns {void}
275 */
276 function checkSpacingBefore(token, pattern) {
277 checkMethodMap[token.value].before(token, pattern);
278 }
279
280 /**
281 * Reports a given token if usage of spacing preceded by the token is
282 * invalid.
283 *
284 * @param {Token} token - A token to report.
285 * @param {RegExp|undefined} pattern - Optional. A pattern of the next
286 * token to check.
287 * @returns {void}
288 */
289 function checkSpacingAfter(token, pattern) {
290 checkMethodMap[token.value].after(token, pattern);
291 }
292
293 /**
294 * Reports a given token if usage of spacing around the token is invalid.
295 *
296 * @param {Token} token - A token to report.
297 * @returns {void}
298 */
299 function checkSpacingAround(token) {
300 checkSpacingBefore(token);
301 checkSpacingAfter(token);
302 }
303
304 /**
305 * Reports the first token of a given node if the first token is a keyword
306 * and usage of spacing around the token is invalid.
307 *
308 * @param {ASTNode|null} node - A node to report.
309 * @returns {void}
310 */
311 function checkSpacingAroundFirstToken(node) {
312 const firstToken = node && sourceCode.getFirstToken(node);
313
314 if (firstToken && firstToken.type === "Keyword") {
315 checkSpacingAround(firstToken);
316 }
317 }
318
319 /**
320 * Reports the first token of a given node if the first token is a keyword
321 * and usage of spacing followed by the token is invalid.
322 *
323 * This is used for unary operators (e.g. `typeof`), `function`, and `super`.
324 * Other rules are handling usage of spacing preceded by those keywords.
325 *
326 * @param {ASTNode|null} node - A node to report.
327 * @returns {void}
328 */
329 function checkSpacingBeforeFirstToken(node) {
330 const firstToken = node && sourceCode.getFirstToken(node);
331
332 if (firstToken && firstToken.type === "Keyword") {
333 checkSpacingBefore(firstToken);
334 }
335 }
336
337 /**
338 * Reports the previous token of a given node if the token is a keyword and
339 * usage of spacing around the token is invalid.
340 *
341 * @param {ASTNode|null} node - A node to report.
342 * @returns {void}
343 */
344 function checkSpacingAroundTokenBefore(node) {
345 if (node) {
346 const token = sourceCode.getTokenBefore(node, astUtils.isKeywordToken);
347
348 checkSpacingAround(token);
349 }
350 }
351
352 /**
353 * Reports `async` or `function` keywords of a given node if usage of
354 * spacing around those keywords is invalid.
355 *
356 * @param {ASTNode} node - A node to report.
357 * @returns {void}
358 */
359 function checkSpacingForFunction(node) {
360 const firstToken = node && sourceCode.getFirstToken(node);
361
362 if (firstToken &&
363 ((firstToken.type === "Keyword" && firstToken.value === "function") ||
364 firstToken.value === "async")
365 ) {
366 checkSpacingBefore(firstToken);
367 }
368 }
369
370 /**
371 * Reports `class` and `extends` keywords of a given node if usage of
372 * spacing around those keywords is invalid.
373 *
374 * @param {ASTNode} node - A node to report.
375 * @returns {void}
376 */
377 function checkSpacingForClass(node) {
378 checkSpacingAroundFirstToken(node);
379 checkSpacingAroundTokenBefore(node.superClass);
380 }
381
382 /**
383 * Reports `if` and `else` keywords of a given node if usage of spacing
384 * around those keywords is invalid.
385 *
386 * @param {ASTNode} node - A node to report.
387 * @returns {void}
388 */
389 function checkSpacingForIfStatement(node) {
390 checkSpacingAroundFirstToken(node);
391 checkSpacingAroundTokenBefore(node.alternate);
392 }
393
394 /**
395 * Reports `try`, `catch`, and `finally` keywords of a given node if usage
396 * of spacing around those keywords is invalid.
397 *
398 * @param {ASTNode} node - A node to report.
399 * @returns {void}
400 */
401 function checkSpacingForTryStatement(node) {
402 checkSpacingAroundFirstToken(node);
403 checkSpacingAroundFirstToken(node.handler);
404 checkSpacingAroundTokenBefore(node.finalizer);
405 }
406
407 /**
408 * Reports `do` and `while` keywords of a given node if usage of spacing
409 * around those keywords is invalid.
410 *
411 * @param {ASTNode} node - A node to report.
412 * @returns {void}
413 */
414 function checkSpacingForDoWhileStatement(node) {
415 checkSpacingAroundFirstToken(node);
416 checkSpacingAroundTokenBefore(node.test);
417 }
418
419 /**
420 * Reports `for` and `in` keywords of a given node if usage of spacing
421 * around those keywords is invalid.
422 *
423 * @param {ASTNode} node - A node to report.
424 * @returns {void}
425 */
426 function checkSpacingForForInStatement(node) {
427 checkSpacingAroundFirstToken(node);
428 checkSpacingAroundTokenBefore(node.right);
429 }
430
431 /**
432 * Reports `for` and `of` keywords of a given node if usage of spacing
433 * around those keywords is invalid.
434 *
435 * @param {ASTNode} node - A node to report.
436 * @returns {void}
437 */
438 function checkSpacingForForOfStatement(node) {
439 if (node.await) {
440 checkSpacingBefore(sourceCode.getFirstToken(node, 0));
441 checkSpacingAfter(sourceCode.getFirstToken(node, 1));
442 } else {
443 checkSpacingAroundFirstToken(node);
444 }
445 checkSpacingAround(sourceCode.getTokenBefore(node.right, astUtils.isNotOpeningParenToken));
446 }
447
448 /**
449 * Reports `import`, `export`, `as`, and `from` keywords of a given node if
450 * usage of spacing around those keywords is invalid.
451 *
452 * This rule handles the `*` token in module declarations.
453 *
454 * import*as A from "./a"; /*error Expected space(s) after "import".
455 * error Expected space(s) before "as".
456 *
457 * @param {ASTNode} node - A node to report.
458 * @returns {void}
459 */
460 function checkSpacingForModuleDeclaration(node) {
461 const firstToken = sourceCode.getFirstToken(node);
462
463 checkSpacingBefore(firstToken, PREV_TOKEN_M);
464 checkSpacingAfter(firstToken, NEXT_TOKEN_M);
465
466 if (node.source) {
467 const fromToken = sourceCode.getTokenBefore(node.source);
468
469 checkSpacingBefore(fromToken, PREV_TOKEN_M);
470 checkSpacingAfter(fromToken, NEXT_TOKEN_M);
471 }
472 }
473
474 /**
475 * Reports `as` keyword of a given node if usage of spacing around this
476 * keyword is invalid.
477 *
478 * @param {ASTNode} node - A node to report.
479 * @returns {void}
480 */
481 function checkSpacingForImportNamespaceSpecifier(node) {
482 const asToken = sourceCode.getFirstToken(node, 1);
483
484 checkSpacingBefore(asToken, PREV_TOKEN_M);
485 }
486
487 /**
488 * Reports `static`, `get`, and `set` keywords of a given node if usage of
489 * spacing around those keywords is invalid.
490 *
491 * @param {ASTNode} node - A node to report.
492 * @returns {void}
493 */
494 function checkSpacingForProperty(node) {
495 if (node.static) {
496 checkSpacingAroundFirstToken(node);
497 }
498 if (node.kind === "get" ||
499 node.kind === "set" ||
500 (
501 (node.method || node.type === "MethodDefinition") &&
502 node.value.async
503 )
504 ) {
505 const token = sourceCode.getTokenBefore(
506 node.key,
507 tok => {
508 switch (tok.value) {
509 case "get":
510 case "set":
511 case "async":
512 return true;
513 default:
514 return false;
515 }
516 }
517 );
518
519 if (!token) {
520 throw new Error("Failed to find token get, set, or async beside method name");
521 }
522
523
524 checkSpacingAround(token);
525 }
526 }
527
528 /**
529 * Reports `await` keyword of a given node if usage of spacing before
530 * this keyword is invalid.
531 *
532 * @param {ASTNode} node - A node to report.
533 * @returns {void}
534 */
535 function checkSpacingForAwaitExpression(node) {
536 checkSpacingBefore(sourceCode.getFirstToken(node));
537 }
538
539 return {
540
541 // Statements
542 DebuggerStatement: checkSpacingAroundFirstToken,
543 WithStatement: checkSpacingAroundFirstToken,
544
545 // Statements - Control flow
546 BreakStatement: checkSpacingAroundFirstToken,
547 ContinueStatement: checkSpacingAroundFirstToken,
548 ReturnStatement: checkSpacingAroundFirstToken,
549 ThrowStatement: checkSpacingAroundFirstToken,
550 TryStatement: checkSpacingForTryStatement,
551
552 // Statements - Choice
553 IfStatement: checkSpacingForIfStatement,
554 SwitchStatement: checkSpacingAroundFirstToken,
555 SwitchCase: checkSpacingAroundFirstToken,
556
557 // Statements - Loops
558 DoWhileStatement: checkSpacingForDoWhileStatement,
559 ForInStatement: checkSpacingForForInStatement,
560 ForOfStatement: checkSpacingForForOfStatement,
561 ForStatement: checkSpacingAroundFirstToken,
562 WhileStatement: checkSpacingAroundFirstToken,
563
564 // Statements - Declarations
565 ClassDeclaration: checkSpacingForClass,
566 ExportNamedDeclaration: checkSpacingForModuleDeclaration,
567 ExportDefaultDeclaration: checkSpacingAroundFirstToken,
568 ExportAllDeclaration: checkSpacingForModuleDeclaration,
569 FunctionDeclaration: checkSpacingForFunction,
570 ImportDeclaration: checkSpacingForModuleDeclaration,
571 VariableDeclaration: checkSpacingAroundFirstToken,
572
573 // Expressions
574 ArrowFunctionExpression: checkSpacingForFunction,
575 AwaitExpression: checkSpacingForAwaitExpression,
576 ClassExpression: checkSpacingForClass,
577 FunctionExpression: checkSpacingForFunction,
578 NewExpression: checkSpacingBeforeFirstToken,
579 Super: checkSpacingBeforeFirstToken,
580 ThisExpression: checkSpacingBeforeFirstToken,
581 UnaryExpression: checkSpacingBeforeFirstToken,
582 YieldExpression: checkSpacingBeforeFirstToken,
583
584 // Others
585 ImportNamespaceSpecifier: checkSpacingForImportNamespaceSpecifier,
586 MethodDefinition: checkSpacingForProperty,
587 Property: checkSpacingForProperty
588 };
589 }
590};