UNPKG

21.3 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} pattern - A pattern of the previous token to check.
112 * @returns {void}
113 */
114 function expectSpaceBefore(token, pattern) {
115 const prevToken = sourceCode.getTokenBefore(token);
116
117 if (prevToken &&
118 (CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) &&
119 !isOpenParenOfTemplate(prevToken) &&
120 astUtils.isTokenOnSameLine(prevToken, token) &&
121 !sourceCode.isSpaceBetweenTokens(prevToken, token)
122 ) {
123 context.report({
124 loc: token.loc.start,
125 message: "Expected space(s) before \"{{value}}\".",
126 data: token,
127 fix(fixer) {
128 return fixer.insertTextBefore(token, " ");
129 }
130 });
131 }
132 }
133
134 /**
135 * Reports a given token if there are space(s) before the token.
136 *
137 * @param {Token} token - A token to report.
138 * @param {RegExp} pattern - A pattern of the previous token to check.
139 * @returns {void}
140 */
141 function unexpectSpaceBefore(token, pattern) {
142 const prevToken = sourceCode.getTokenBefore(token);
143
144 if (prevToken &&
145 (CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) &&
146 !isOpenParenOfTemplate(prevToken) &&
147 astUtils.isTokenOnSameLine(prevToken, token) &&
148 sourceCode.isSpaceBetweenTokens(prevToken, token)
149 ) {
150 context.report({
151 loc: token.loc.start,
152 message: "Unexpected space(s) before \"{{value}}\".",
153 data: token,
154 fix(fixer) {
155 return fixer.removeRange([prevToken.range[1], token.range[0]]);
156 }
157 });
158 }
159 }
160
161 /**
162 * Reports a given token if there are not space(s) after the token.
163 *
164 * @param {Token} token - A token to report.
165 * @param {RegExp} pattern - A pattern of the next token to check.
166 * @returns {void}
167 */
168 function expectSpaceAfter(token, pattern) {
169 const nextToken = sourceCode.getTokenAfter(token);
170
171 if (nextToken &&
172 (CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) &&
173 !isCloseParenOfTemplate(nextToken) &&
174 astUtils.isTokenOnSameLine(token, nextToken) &&
175 !sourceCode.isSpaceBetweenTokens(token, nextToken)
176 ) {
177 context.report({
178 loc: token.loc.start,
179 message: "Expected space(s) after \"{{value}}\".",
180 data: token,
181 fix(fixer) {
182 return fixer.insertTextAfter(token, " ");
183 }
184 });
185 }
186 }
187
188 /**
189 * Reports a given token if there are space(s) after the token.
190 *
191 * @param {Token} token - A token to report.
192 * @param {RegExp} pattern - A pattern of the next token to check.
193 * @returns {void}
194 */
195 function unexpectSpaceAfter(token, pattern) {
196 const nextToken = sourceCode.getTokenAfter(token);
197
198 if (nextToken &&
199 (CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) &&
200 !isCloseParenOfTemplate(nextToken) &&
201 astUtils.isTokenOnSameLine(token, nextToken) &&
202 sourceCode.isSpaceBetweenTokens(token, nextToken)
203 ) {
204 context.report({
205 loc: token.loc.start,
206 message: "Unexpected space(s) after \"{{value}}\".",
207 data: token,
208 fix(fixer) {
209 return fixer.removeRange([token.range[1], nextToken.range[0]]);
210 }
211 });
212 }
213 }
214
215 /**
216 * Parses the option object and determines check methods for each keyword.
217 *
218 * @param {Object|undefined} options - The option object to parse.
219 * @returns {Object} - Normalized option object.
220 * Keys are keywords (there are for every keyword).
221 * Values are instances of `{"before": function, "after": function}`.
222 */
223 function parseOptions(options) {
224 const before = !options || options.before !== false;
225 const after = !options || options.after !== false;
226 const defaultValue = {
227 before: before ? expectSpaceBefore : unexpectSpaceBefore,
228 after: after ? expectSpaceAfter : unexpectSpaceAfter
229 };
230 const overrides = (options && options.overrides) || {};
231 const retv = Object.create(null);
232
233 for (let i = 0; i < KEYS.length; ++i) {
234 const key = KEYS[i];
235 const override = overrides[key];
236
237 if (override) {
238 const thisBefore = ("before" in override) ? override.before : before;
239 const thisAfter = ("after" in override) ? override.after : after;
240
241 retv[key] = {
242 before: thisBefore ? expectSpaceBefore : unexpectSpaceBefore,
243 after: thisAfter ? expectSpaceAfter : unexpectSpaceAfter
244 };
245 } else {
246 retv[key] = defaultValue;
247 }
248 }
249
250 return retv;
251 }
252
253 const checkMethodMap = parseOptions(context.options[0]);
254
255 /**
256 * Reports a given token if usage of spacing followed by the token is
257 * invalid.
258 *
259 * @param {Token} token - A token to report.
260 * @param {RegExp|undefined} pattern - Optional. A pattern of the previous
261 * token to check.
262 * @returns {void}
263 */
264 function checkSpacingBefore(token, pattern) {
265 checkMethodMap[token.value].before(token, pattern || PREV_TOKEN);
266 }
267
268 /**
269 * Reports a given token if usage of spacing preceded by the token is
270 * invalid.
271 *
272 * @param {Token} token - A token to report.
273 * @param {RegExp|undefined} pattern - Optional. A pattern of the next
274 * token to check.
275 * @returns {void}
276 */
277 function checkSpacingAfter(token, pattern) {
278 checkMethodMap[token.value].after(token, pattern || NEXT_TOKEN);
279 }
280
281 /**
282 * Reports a given token if usage of spacing around the token is invalid.
283 *
284 * @param {Token} token - A token to report.
285 * @returns {void}
286 */
287 function checkSpacingAround(token) {
288 checkSpacingBefore(token);
289 checkSpacingAfter(token);
290 }
291
292 /**
293 * Reports the first token of a given node if the first token is a keyword
294 * and usage of spacing around the token is invalid.
295 *
296 * @param {ASTNode|null} node - A node to report.
297 * @returns {void}
298 */
299 function checkSpacingAroundFirstToken(node) {
300 const firstToken = node && sourceCode.getFirstToken(node);
301
302 if (firstToken && firstToken.type === "Keyword") {
303 checkSpacingAround(firstToken);
304 }
305 }
306
307 /**
308 * Reports the first token of a given node if the first token is a keyword
309 * and usage of spacing followed by the token is invalid.
310 *
311 * This is used for unary operators (e.g. `typeof`), `function`, and `super`.
312 * Other rules are handling usage of spacing preceded by those keywords.
313 *
314 * @param {ASTNode|null} node - A node to report.
315 * @returns {void}
316 */
317 function checkSpacingBeforeFirstToken(node) {
318 const firstToken = node && sourceCode.getFirstToken(node);
319
320 if (firstToken && firstToken.type === "Keyword") {
321 checkSpacingBefore(firstToken);
322 }
323 }
324
325 /**
326 * Reports the previous token of a given node if the token is a keyword and
327 * usage of spacing around the token is invalid.
328 *
329 * @param {ASTNode|null} node - A node to report.
330 * @returns {void}
331 */
332 function checkSpacingAroundTokenBefore(node) {
333 if (node) {
334 const token = sourceCode.getTokenBefore(node, astUtils.isKeywordToken);
335
336 checkSpacingAround(token);
337 }
338 }
339
340 /**
341 * Reports `async` or `function` keywords of a given node if usage of
342 * spacing around those keywords is invalid.
343 *
344 * @param {ASTNode} node - A node to report.
345 * @returns {void}
346 */
347 function checkSpacingForFunction(node) {
348 const firstToken = node && sourceCode.getFirstToken(node);
349
350 if (firstToken &&
351 ((firstToken.type === "Keyword" && firstToken.value === "function") ||
352 firstToken.value === "async")
353 ) {
354 checkSpacingBefore(firstToken);
355 }
356 }
357
358 /**
359 * Reports `class` and `extends` keywords of a given node if usage of
360 * spacing around those keywords is invalid.
361 *
362 * @param {ASTNode} node - A node to report.
363 * @returns {void}
364 */
365 function checkSpacingForClass(node) {
366 checkSpacingAroundFirstToken(node);
367 checkSpacingAroundTokenBefore(node.superClass);
368 }
369
370 /**
371 * Reports `if` and `else` keywords of a given node if usage of spacing
372 * around those keywords is invalid.
373 *
374 * @param {ASTNode} node - A node to report.
375 * @returns {void}
376 */
377 function checkSpacingForIfStatement(node) {
378 checkSpacingAroundFirstToken(node);
379 checkSpacingAroundTokenBefore(node.alternate);
380 }
381
382 /**
383 * Reports `try`, `catch`, and `finally` keywords of a given node if usage
384 * of spacing around those keywords is invalid.
385 *
386 * @param {ASTNode} node - A node to report.
387 * @returns {void}
388 */
389 function checkSpacingForTryStatement(node) {
390 checkSpacingAroundFirstToken(node);
391 checkSpacingAroundFirstToken(node.handler);
392 checkSpacingAroundTokenBefore(node.finalizer);
393 }
394
395 /**
396 * Reports `do` and `while` keywords of a given node if usage of spacing
397 * around those keywords is invalid.
398 *
399 * @param {ASTNode} node - A node to report.
400 * @returns {void}
401 */
402 function checkSpacingForDoWhileStatement(node) {
403 checkSpacingAroundFirstToken(node);
404 checkSpacingAroundTokenBefore(node.test);
405 }
406
407 /**
408 * Reports `for` and `in` 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 checkSpacingForForInStatement(node) {
415 checkSpacingAroundFirstToken(node);
416 checkSpacingAroundTokenBefore(node.right);
417 }
418
419 /**
420 * Reports `for` and `of` 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 checkSpacingForForOfStatement(node) {
427 if (node.await) {
428 checkSpacingBefore(sourceCode.getFirstToken(node, 0));
429 checkSpacingAfter(sourceCode.getFirstToken(node, 1));
430 } else {
431 checkSpacingAroundFirstToken(node);
432 }
433 checkSpacingAround(sourceCode.getTokenBefore(node.right, astUtils.isNotOpeningParenToken));
434 }
435
436 /**
437 * Reports `import`, `export`, `as`, and `from` keywords of a given node if
438 * usage of spacing around those keywords is invalid.
439 *
440 * This rule handles the `*` token in module declarations.
441 *
442 * import*as A from "./a"; /*error Expected space(s) after "import".
443 * error Expected space(s) before "as".
444 *
445 * @param {ASTNode} node - A node to report.
446 * @returns {void}
447 */
448 function checkSpacingForModuleDeclaration(node) {
449 const firstToken = sourceCode.getFirstToken(node);
450
451 checkSpacingBefore(firstToken, PREV_TOKEN_M);
452 checkSpacingAfter(firstToken, NEXT_TOKEN_M);
453
454 if (node.source) {
455 const fromToken = sourceCode.getTokenBefore(node.source);
456
457 checkSpacingBefore(fromToken, PREV_TOKEN_M);
458 checkSpacingAfter(fromToken, NEXT_TOKEN_M);
459 }
460 }
461
462 /**
463 * Reports `as` keyword of a given node if usage of spacing around this
464 * keyword is invalid.
465 *
466 * @param {ASTNode} node - A node to report.
467 * @returns {void}
468 */
469 function checkSpacingForImportNamespaceSpecifier(node) {
470 const asToken = sourceCode.getFirstToken(node, 1);
471
472 checkSpacingBefore(asToken, PREV_TOKEN_M);
473 }
474
475 /**
476 * Reports `static`, `get`, and `set` keywords of a given node if usage of
477 * spacing around those keywords is invalid.
478 *
479 * @param {ASTNode} node - A node to report.
480 * @returns {void}
481 */
482 function checkSpacingForProperty(node) {
483 if (node.static) {
484 checkSpacingAroundFirstToken(node);
485 }
486 if (node.kind === "get" ||
487 node.kind === "set" ||
488 (
489 (node.method || node.type === "MethodDefinition") &&
490 node.value.async
491 )
492 ) {
493 const token = sourceCode.getTokenBefore(
494 node.key,
495 tok => {
496 switch (tok.value) {
497 case "get":
498 case "set":
499 case "async":
500 return true;
501 default:
502 return false;
503 }
504 }
505 );
506
507 if (!token) {
508 throw new Error("Failed to find token get, set, or async beside method name");
509 }
510
511
512 checkSpacingAround(token);
513 }
514 }
515
516 /**
517 * Reports `await` keyword of a given node if usage of spacing before
518 * this keyword is invalid.
519 *
520 * @param {ASTNode} node - A node to report.
521 * @returns {void}
522 */
523 function checkSpacingForAwaitExpression(node) {
524 checkSpacingBefore(sourceCode.getFirstToken(node));
525 }
526
527 return {
528
529 // Statements
530 DebuggerStatement: checkSpacingAroundFirstToken,
531 WithStatement: checkSpacingAroundFirstToken,
532
533 // Statements - Control flow
534 BreakStatement: checkSpacingAroundFirstToken,
535 ContinueStatement: checkSpacingAroundFirstToken,
536 ReturnStatement: checkSpacingAroundFirstToken,
537 ThrowStatement: checkSpacingAroundFirstToken,
538 TryStatement: checkSpacingForTryStatement,
539
540 // Statements - Choice
541 IfStatement: checkSpacingForIfStatement,
542 SwitchStatement: checkSpacingAroundFirstToken,
543 SwitchCase: checkSpacingAroundFirstToken,
544
545 // Statements - Loops
546 DoWhileStatement: checkSpacingForDoWhileStatement,
547 ForInStatement: checkSpacingForForInStatement,
548 ForOfStatement: checkSpacingForForOfStatement,
549 ForStatement: checkSpacingAroundFirstToken,
550 WhileStatement: checkSpacingAroundFirstToken,
551
552 // Statements - Declarations
553 ClassDeclaration: checkSpacingForClass,
554 ExportNamedDeclaration: checkSpacingForModuleDeclaration,
555 ExportDefaultDeclaration: checkSpacingAroundFirstToken,
556 ExportAllDeclaration: checkSpacingForModuleDeclaration,
557 FunctionDeclaration: checkSpacingForFunction,
558 ImportDeclaration: checkSpacingForModuleDeclaration,
559 VariableDeclaration: checkSpacingAroundFirstToken,
560
561 // Expressions
562 ArrowFunctionExpression: checkSpacingForFunction,
563 AwaitExpression: checkSpacingForAwaitExpression,
564 ClassExpression: checkSpacingForClass,
565 FunctionExpression: checkSpacingForFunction,
566 NewExpression: checkSpacingBeforeFirstToken,
567 Super: checkSpacingBeforeFirstToken,
568 ThisExpression: checkSpacingBeforeFirstToken,
569 UnaryExpression: checkSpacingBeforeFirstToken,
570 YieldExpression: checkSpacingBeforeFirstToken,
571
572 // Others
573 ImportNamespaceSpecifier: checkSpacingForImportNamespaceSpecifier,
574 MethodDefinition: checkSpacingForProperty,
575 Property: checkSpacingForProperty
576 };
577 }
578};