项目原始demo,不改动
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.
 
 
 
 

579 rader
21 KiB

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