keyword-spacing.js 21 KB

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