semi.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. * @fileoverview Rule to flag missing semicolons.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const FixTracker = require("./utils/fix-tracker");
  10. const astUtils = require("./utils/ast-utils");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. /** @type {import('../shared/types').Rule} */
  15. module.exports = {
  16. meta: {
  17. type: "layout",
  18. docs: {
  19. description: "Require or disallow semicolons instead of ASI",
  20. recommended: false,
  21. url: "https://eslint.org/docs/rules/semi"
  22. },
  23. fixable: "code",
  24. schema: {
  25. anyOf: [
  26. {
  27. type: "array",
  28. items: [
  29. {
  30. enum: ["never"]
  31. },
  32. {
  33. type: "object",
  34. properties: {
  35. beforeStatementContinuationChars: {
  36. enum: ["always", "any", "never"]
  37. }
  38. },
  39. additionalProperties: false
  40. }
  41. ],
  42. minItems: 0,
  43. maxItems: 2
  44. },
  45. {
  46. type: "array",
  47. items: [
  48. {
  49. enum: ["always"]
  50. },
  51. {
  52. type: "object",
  53. properties: {
  54. omitLastInOneLineBlock: { type: "boolean" }
  55. },
  56. additionalProperties: false
  57. }
  58. ],
  59. minItems: 0,
  60. maxItems: 2
  61. }
  62. ]
  63. },
  64. messages: {
  65. missingSemi: "Missing semicolon.",
  66. extraSemi: "Extra semicolon."
  67. }
  68. },
  69. create(context) {
  70. const OPT_OUT_PATTERN = /^[-[(/+`]/u; // One of [(/+-`
  71. const unsafeClassFieldNames = new Set(["get", "set", "static"]);
  72. const unsafeClassFieldFollowers = new Set(["*", "in", "instanceof"]);
  73. const options = context.options[1];
  74. const never = context.options[0] === "never";
  75. const exceptOneLine = Boolean(options && options.omitLastInOneLineBlock);
  76. const beforeStatementContinuationChars = options && options.beforeStatementContinuationChars || "any";
  77. const sourceCode = context.getSourceCode();
  78. //--------------------------------------------------------------------------
  79. // Helpers
  80. //--------------------------------------------------------------------------
  81. /**
  82. * Reports a semicolon error with appropriate location and message.
  83. * @param {ASTNode} node The node with an extra or missing semicolon.
  84. * @param {boolean} missing True if the semicolon is missing.
  85. * @returns {void}
  86. */
  87. function report(node, missing) {
  88. const lastToken = sourceCode.getLastToken(node);
  89. let messageId,
  90. fix,
  91. loc;
  92. if (!missing) {
  93. messageId = "missingSemi";
  94. loc = {
  95. start: lastToken.loc.end,
  96. end: astUtils.getNextLocation(sourceCode, lastToken.loc.end)
  97. };
  98. fix = function(fixer) {
  99. return fixer.insertTextAfter(lastToken, ";");
  100. };
  101. } else {
  102. messageId = "extraSemi";
  103. loc = lastToken.loc;
  104. fix = function(fixer) {
  105. /*
  106. * Expand the replacement range to include the surrounding
  107. * tokens to avoid conflicting with no-extra-semi.
  108. * https://github.com/eslint/eslint/issues/7928
  109. */
  110. return new FixTracker(fixer, sourceCode)
  111. .retainSurroundingTokens(lastToken)
  112. .remove(lastToken);
  113. };
  114. }
  115. context.report({
  116. node,
  117. loc,
  118. messageId,
  119. fix
  120. });
  121. }
  122. /**
  123. * Check whether a given semicolon token is redundant.
  124. * @param {Token} semiToken A semicolon token to check.
  125. * @returns {boolean} `true` if the next token is `;` or `}`.
  126. */
  127. function isRedundantSemi(semiToken) {
  128. const nextToken = sourceCode.getTokenAfter(semiToken);
  129. return (
  130. !nextToken ||
  131. astUtils.isClosingBraceToken(nextToken) ||
  132. astUtils.isSemicolonToken(nextToken)
  133. );
  134. }
  135. /**
  136. * Check whether a given token is the closing brace of an arrow function.
  137. * @param {Token} lastToken A token to check.
  138. * @returns {boolean} `true` if the token is the closing brace of an arrow function.
  139. */
  140. function isEndOfArrowBlock(lastToken) {
  141. if (!astUtils.isClosingBraceToken(lastToken)) {
  142. return false;
  143. }
  144. const node = sourceCode.getNodeByRangeIndex(lastToken.range[0]);
  145. return (
  146. node.type === "BlockStatement" &&
  147. node.parent.type === "ArrowFunctionExpression"
  148. );
  149. }
  150. /**
  151. * Checks if a given PropertyDefinition node followed by a semicolon
  152. * can safely remove that semicolon. It is not to safe to remove if
  153. * the class field name is "get", "set", or "static", or if
  154. * followed by a generator method.
  155. * @param {ASTNode} node The node to check.
  156. * @returns {boolean} `true` if the node cannot have the semicolon
  157. * removed.
  158. */
  159. function maybeClassFieldAsiHazard(node) {
  160. if (node.type !== "PropertyDefinition") {
  161. return false;
  162. }
  163. /*
  164. * Computed property names and non-identifiers are always safe
  165. * as they can be distinguished from keywords easily.
  166. */
  167. const needsNameCheck = !node.computed && node.key.type === "Identifier";
  168. /*
  169. * Certain names are problematic unless they also have a
  170. * a way to distinguish between keywords and property
  171. * names.
  172. */
  173. if (needsNameCheck && unsafeClassFieldNames.has(node.key.name)) {
  174. /*
  175. * Special case: If the field name is `static`,
  176. * it is only valid if the field is marked as static,
  177. * so "static static" is okay but "static" is not.
  178. */
  179. const isStaticStatic = node.static && node.key.name === "static";
  180. /*
  181. * For other unsafe names, we only care if there is no
  182. * initializer. No initializer = hazard.
  183. */
  184. if (!isStaticStatic && !node.value) {
  185. return true;
  186. }
  187. }
  188. const followingToken = sourceCode.getTokenAfter(node);
  189. return unsafeClassFieldFollowers.has(followingToken.value);
  190. }
  191. /**
  192. * Check whether a given node is on the same line with the next token.
  193. * @param {Node} node A statement node to check.
  194. * @returns {boolean} `true` if the node is on the same line with the next token.
  195. */
  196. function isOnSameLineWithNextToken(node) {
  197. const prevToken = sourceCode.getLastToken(node, 1);
  198. const nextToken = sourceCode.getTokenAfter(node);
  199. return !!nextToken && astUtils.isTokenOnSameLine(prevToken, nextToken);
  200. }
  201. /**
  202. * Check whether a given node can connect the next line if the next line is unreliable.
  203. * @param {Node} node A statement node to check.
  204. * @returns {boolean} `true` if the node can connect the next line.
  205. */
  206. function maybeAsiHazardAfter(node) {
  207. const t = node.type;
  208. if (t === "DoWhileStatement" ||
  209. t === "BreakStatement" ||
  210. t === "ContinueStatement" ||
  211. t === "DebuggerStatement" ||
  212. t === "ImportDeclaration" ||
  213. t === "ExportAllDeclaration"
  214. ) {
  215. return false;
  216. }
  217. if (t === "ReturnStatement") {
  218. return Boolean(node.argument);
  219. }
  220. if (t === "ExportNamedDeclaration") {
  221. return Boolean(node.declaration);
  222. }
  223. if (isEndOfArrowBlock(sourceCode.getLastToken(node, 1))) {
  224. return false;
  225. }
  226. return true;
  227. }
  228. /**
  229. * Check whether a given token can connect the previous statement.
  230. * @param {Token} token A token to check.
  231. * @returns {boolean} `true` if the token is one of `[`, `(`, `/`, `+`, `-`, ```, `++`, and `--`.
  232. */
  233. function maybeAsiHazardBefore(token) {
  234. return (
  235. Boolean(token) &&
  236. OPT_OUT_PATTERN.test(token.value) &&
  237. token.value !== "++" &&
  238. token.value !== "--"
  239. );
  240. }
  241. /**
  242. * Check if the semicolon of a given node is unnecessary, only true if:
  243. * - next token is a valid statement divider (`;` or `}`).
  244. * - next token is on a new line and the node is not connectable to the new line.
  245. * @param {Node} node A statement node to check.
  246. * @returns {boolean} whether the semicolon is unnecessary.
  247. */
  248. function canRemoveSemicolon(node) {
  249. if (isRedundantSemi(sourceCode.getLastToken(node))) {
  250. return true; // `;;` or `;}`
  251. }
  252. if (maybeClassFieldAsiHazard(node)) {
  253. return false;
  254. }
  255. if (isOnSameLineWithNextToken(node)) {
  256. return false; // One liner.
  257. }
  258. // continuation characters should not apply to class fields
  259. if (
  260. node.type !== "PropertyDefinition" &&
  261. beforeStatementContinuationChars === "never" &&
  262. !maybeAsiHazardAfter(node)
  263. ) {
  264. return true; // ASI works. This statement doesn't connect to the next.
  265. }
  266. if (!maybeAsiHazardBefore(sourceCode.getTokenAfter(node))) {
  267. return true; // ASI works. The next token doesn't connect to this statement.
  268. }
  269. return false;
  270. }
  271. /**
  272. * Checks a node to see if it's the last item in a one-liner block.
  273. * Block is any `BlockStatement` or `StaticBlock` node. Block is a one-liner if its
  274. * braces (and consequently everything between them) are on the same line.
  275. * @param {ASTNode} node The node to check.
  276. * @returns {boolean} whether the node is the last item in a one-liner block.
  277. */
  278. function isLastInOneLinerBlock(node) {
  279. const parent = node.parent;
  280. const nextToken = sourceCode.getTokenAfter(node);
  281. if (!nextToken || nextToken.value !== "}") {
  282. return false;
  283. }
  284. if (parent.type === "BlockStatement") {
  285. return parent.loc.start.line === parent.loc.end.line;
  286. }
  287. if (parent.type === "StaticBlock") {
  288. const openingBrace = sourceCode.getFirstToken(parent, { skip: 1 }); // skip the `static` token
  289. return openingBrace.loc.start.line === parent.loc.end.line;
  290. }
  291. return false;
  292. }
  293. /**
  294. * Checks a node to see if it's followed by a semicolon.
  295. * @param {ASTNode} node The node to check.
  296. * @returns {void}
  297. */
  298. function checkForSemicolon(node) {
  299. const isSemi = astUtils.isSemicolonToken(sourceCode.getLastToken(node));
  300. if (never) {
  301. if (isSemi && canRemoveSemicolon(node)) {
  302. report(node, true);
  303. } else if (
  304. !isSemi && beforeStatementContinuationChars === "always" &&
  305. node.type !== "PropertyDefinition" &&
  306. maybeAsiHazardBefore(sourceCode.getTokenAfter(node))
  307. ) {
  308. report(node);
  309. }
  310. } else {
  311. const oneLinerBlock = (exceptOneLine && isLastInOneLinerBlock(node));
  312. if (isSemi && oneLinerBlock) {
  313. report(node, true);
  314. } else if (!isSemi && !oneLinerBlock) {
  315. report(node);
  316. }
  317. }
  318. }
  319. /**
  320. * Checks to see if there's a semicolon after a variable declaration.
  321. * @param {ASTNode} node The node to check.
  322. * @returns {void}
  323. */
  324. function checkForSemicolonForVariableDeclaration(node) {
  325. const parent = node.parent;
  326. if ((parent.type !== "ForStatement" || parent.init !== node) &&
  327. (!/^For(?:In|Of)Statement/u.test(parent.type) || parent.left !== node)
  328. ) {
  329. checkForSemicolon(node);
  330. }
  331. }
  332. //--------------------------------------------------------------------------
  333. // Public API
  334. //--------------------------------------------------------------------------
  335. return {
  336. VariableDeclaration: checkForSemicolonForVariableDeclaration,
  337. ExpressionStatement: checkForSemicolon,
  338. ReturnStatement: checkForSemicolon,
  339. ThrowStatement: checkForSemicolon,
  340. DoWhileStatement: checkForSemicolon,
  341. DebuggerStatement: checkForSemicolon,
  342. BreakStatement: checkForSemicolon,
  343. ContinueStatement: checkForSemicolon,
  344. ImportDeclaration: checkForSemicolon,
  345. ExportAllDeclaration: checkForSemicolon,
  346. ExportNamedDeclaration(node) {
  347. if (!node.declaration) {
  348. checkForSemicolon(node);
  349. }
  350. },
  351. ExportDefaultDeclaration(node) {
  352. if (!/(?:Class|Function)Declaration/u.test(node.declaration.type)) {
  353. checkForSemicolon(node);
  354. }
  355. },
  356. PropertyDefinition: checkForSemicolon
  357. };
  358. }
  359. };