semi.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. module.exports = {
  15. meta: {
  16. type: "layout",
  17. docs: {
  18. description: "require or disallow semicolons instead of ASI",
  19. category: "Stylistic Issues",
  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. },
  65. create(context) {
  66. const OPT_OUT_PATTERN = /^[-[(/+`]/u; // One of [(/+-`
  67. const options = context.options[1];
  68. const never = context.options[0] === "never";
  69. const exceptOneLine = Boolean(options && options.omitLastInOneLineBlock);
  70. const beforeStatementContinuationChars = options && options.beforeStatementContinuationChars || "any";
  71. const sourceCode = context.getSourceCode();
  72. //--------------------------------------------------------------------------
  73. // Helpers
  74. //--------------------------------------------------------------------------
  75. /**
  76. * Reports a semicolon error with appropriate location and message.
  77. * @param {ASTNode} node The node with an extra or missing semicolon.
  78. * @param {boolean} missing True if the semicolon is missing.
  79. * @returns {void}
  80. */
  81. function report(node, missing) {
  82. const lastToken = sourceCode.getLastToken(node);
  83. let message,
  84. fix,
  85. loc;
  86. if (!missing) {
  87. message = "Missing semicolon.";
  88. loc = {
  89. start: lastToken.loc.end,
  90. end: astUtils.getNextLocation(sourceCode, lastToken.loc.end)
  91. };
  92. fix = function(fixer) {
  93. return fixer.insertTextAfter(lastToken, ";");
  94. };
  95. } else {
  96. message = "Extra semicolon.";
  97. loc = lastToken.loc;
  98. fix = function(fixer) {
  99. /*
  100. * Expand the replacement range to include the surrounding
  101. * tokens to avoid conflicting with no-extra-semi.
  102. * https://github.com/eslint/eslint/issues/7928
  103. */
  104. return new FixTracker(fixer, sourceCode)
  105. .retainSurroundingTokens(lastToken)
  106. .remove(lastToken);
  107. };
  108. }
  109. context.report({
  110. node,
  111. loc,
  112. message,
  113. fix
  114. });
  115. }
  116. /**
  117. * Check whether a given semicolon token is redandant.
  118. * @param {Token} semiToken A semicolon token to check.
  119. * @returns {boolean} `true` if the next token is `;` or `}`.
  120. */
  121. function isRedundantSemi(semiToken) {
  122. const nextToken = sourceCode.getTokenAfter(semiToken);
  123. return (
  124. !nextToken ||
  125. astUtils.isClosingBraceToken(nextToken) ||
  126. astUtils.isSemicolonToken(nextToken)
  127. );
  128. }
  129. /**
  130. * Check whether a given token is the closing brace of an arrow function.
  131. * @param {Token} lastToken A token to check.
  132. * @returns {boolean} `true` if the token is the closing brace of an arrow function.
  133. */
  134. function isEndOfArrowBlock(lastToken) {
  135. if (!astUtils.isClosingBraceToken(lastToken)) {
  136. return false;
  137. }
  138. const node = sourceCode.getNodeByRangeIndex(lastToken.range[0]);
  139. return (
  140. node.type === "BlockStatement" &&
  141. node.parent.type === "ArrowFunctionExpression"
  142. );
  143. }
  144. /**
  145. * Check whether a given node is on the same line with the next token.
  146. * @param {Node} node A statement node to check.
  147. * @returns {boolean} `true` if the node is on the same line with the next token.
  148. */
  149. function isOnSameLineWithNextToken(node) {
  150. const prevToken = sourceCode.getLastToken(node, 1);
  151. const nextToken = sourceCode.getTokenAfter(node);
  152. return !!nextToken && astUtils.isTokenOnSameLine(prevToken, nextToken);
  153. }
  154. /**
  155. * Check whether a given node can connect the next line if the next line is unreliable.
  156. * @param {Node} node A statement node to check.
  157. * @returns {boolean} `true` if the node can connect the next line.
  158. */
  159. function maybeAsiHazardAfter(node) {
  160. const t = node.type;
  161. if (t === "DoWhileStatement" ||
  162. t === "BreakStatement" ||
  163. t === "ContinueStatement" ||
  164. t === "DebuggerStatement" ||
  165. t === "ImportDeclaration" ||
  166. t === "ExportAllDeclaration"
  167. ) {
  168. return false;
  169. }
  170. if (t === "ReturnStatement") {
  171. return Boolean(node.argument);
  172. }
  173. if (t === "ExportNamedDeclaration") {
  174. return Boolean(node.declaration);
  175. }
  176. if (isEndOfArrowBlock(sourceCode.getLastToken(node, 1))) {
  177. return false;
  178. }
  179. return true;
  180. }
  181. /**
  182. * Check whether a given token can connect the previous statement.
  183. * @param {Token} token A token to check.
  184. * @returns {boolean} `true` if the token is one of `[`, `(`, `/`, `+`, `-`, ```, `++`, and `--`.
  185. */
  186. function maybeAsiHazardBefore(token) {
  187. return (
  188. Boolean(token) &&
  189. OPT_OUT_PATTERN.test(token.value) &&
  190. token.value !== "++" &&
  191. token.value !== "--"
  192. );
  193. }
  194. /**
  195. * Check if the semicolon of a given node is unnecessary, only true if:
  196. * - next token is a valid statement divider (`;` or `}`).
  197. * - next token is on a new line and the node is not connectable to the new line.
  198. * @param {Node} node A statement node to check.
  199. * @returns {boolean} whether the semicolon is unnecessary.
  200. */
  201. function canRemoveSemicolon(node) {
  202. if (isRedundantSemi(sourceCode.getLastToken(node))) {
  203. return true; // `;;` or `;}`
  204. }
  205. if (isOnSameLineWithNextToken(node)) {
  206. return false; // One liner.
  207. }
  208. if (beforeStatementContinuationChars === "never" && !maybeAsiHazardAfter(node)) {
  209. return true; // ASI works. This statement doesn't connect to the next.
  210. }
  211. if (!maybeAsiHazardBefore(sourceCode.getTokenAfter(node))) {
  212. return true; // ASI works. The next token doesn't connect to this statement.
  213. }
  214. return false;
  215. }
  216. /**
  217. * Checks a node to see if it's in a one-liner block statement.
  218. * @param {ASTNode} node The node to check.
  219. * @returns {boolean} whether the node is in a one-liner block statement.
  220. */
  221. function isOneLinerBlock(node) {
  222. const parent = node.parent;
  223. const nextToken = sourceCode.getTokenAfter(node);
  224. if (!nextToken || nextToken.value !== "}") {
  225. return false;
  226. }
  227. return (
  228. !!parent &&
  229. parent.type === "BlockStatement" &&
  230. parent.loc.start.line === parent.loc.end.line
  231. );
  232. }
  233. /**
  234. * Checks a node to see if it's followed by a semicolon.
  235. * @param {ASTNode} node The node to check.
  236. * @returns {void}
  237. */
  238. function checkForSemicolon(node) {
  239. const isSemi = astUtils.isSemicolonToken(sourceCode.getLastToken(node));
  240. if (never) {
  241. if (isSemi && canRemoveSemicolon(node)) {
  242. report(node, true);
  243. } else if (!isSemi && beforeStatementContinuationChars === "always" && maybeAsiHazardBefore(sourceCode.getTokenAfter(node))) {
  244. report(node);
  245. }
  246. } else {
  247. const oneLinerBlock = (exceptOneLine && isOneLinerBlock(node));
  248. if (isSemi && oneLinerBlock) {
  249. report(node, true);
  250. } else if (!isSemi && !oneLinerBlock) {
  251. report(node);
  252. }
  253. }
  254. }
  255. /**
  256. * Checks to see if there's a semicolon after a variable declaration.
  257. * @param {ASTNode} node The node to check.
  258. * @returns {void}
  259. */
  260. function checkForSemicolonForVariableDeclaration(node) {
  261. const parent = node.parent;
  262. if ((parent.type !== "ForStatement" || parent.init !== node) &&
  263. (!/^For(?:In|Of)Statement/u.test(parent.type) || parent.left !== node)
  264. ) {
  265. checkForSemicolon(node);
  266. }
  267. }
  268. //--------------------------------------------------------------------------
  269. // Public API
  270. //--------------------------------------------------------------------------
  271. return {
  272. VariableDeclaration: checkForSemicolonForVariableDeclaration,
  273. ExpressionStatement: checkForSemicolon,
  274. ReturnStatement: checkForSemicolon,
  275. ThrowStatement: checkForSemicolon,
  276. DoWhileStatement: checkForSemicolon,
  277. DebuggerStatement: checkForSemicolon,
  278. BreakStatement: checkForSemicolon,
  279. ContinueStatement: checkForSemicolon,
  280. ImportDeclaration: checkForSemicolon,
  281. ExportAllDeclaration: checkForSemicolon,
  282. ExportNamedDeclaration(node) {
  283. if (!node.declaration) {
  284. checkForSemicolon(node);
  285. }
  286. },
  287. ExportDefaultDeclaration(node) {
  288. if (!/(?:Class|Function)Declaration/u.test(node.declaration.type)) {
  289. checkForSemicolon(node);
  290. }
  291. }
  292. };
  293. }
  294. };