keyword-spacing.js 21 KB

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