Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keyword-spacing.js 21KB

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