no-extra-semi.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @fileoverview Rule to flag use of unnecessary 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: "suggestion",
  18. docs: {
  19. description: "Disallow unnecessary semicolons",
  20. recommended: true,
  21. url: "https://eslint.org/docs/latest/rules/no-extra-semi"
  22. },
  23. fixable: "code",
  24. schema: [],
  25. messages: {
  26. unexpected: "Unnecessary semicolon."
  27. }
  28. },
  29. create(context) {
  30. const sourceCode = context.sourceCode;
  31. /**
  32. * Checks if a node or token is fixable.
  33. * A node is fixable if it can be removed without turning a subsequent statement into a directive after fixing other nodes.
  34. * @param {Token} nodeOrToken The node or token to check.
  35. * @returns {boolean} Whether or not the node is fixable.
  36. */
  37. function isFixable(nodeOrToken) {
  38. const nextToken = sourceCode.getTokenAfter(nodeOrToken);
  39. if (!nextToken || nextToken.type !== "String") {
  40. return true;
  41. }
  42. const stringNode = sourceCode.getNodeByRangeIndex(nextToken.range[0]);
  43. return !astUtils.isTopLevelExpressionStatement(stringNode.parent);
  44. }
  45. /**
  46. * Reports an unnecessary semicolon error.
  47. * @param {Node|Token} nodeOrToken A node or a token to be reported.
  48. * @returns {void}
  49. */
  50. function report(nodeOrToken) {
  51. context.report({
  52. node: nodeOrToken,
  53. messageId: "unexpected",
  54. fix: isFixable(nodeOrToken)
  55. ? fixer =>
  56. /*
  57. * Expand the replacement range to include the surrounding
  58. * tokens to avoid conflicting with semi.
  59. * https://github.com/eslint/eslint/issues/7928
  60. */
  61. new FixTracker(fixer, context.sourceCode)
  62. .retainSurroundingTokens(nodeOrToken)
  63. .remove(nodeOrToken)
  64. : null
  65. });
  66. }
  67. /**
  68. * Checks for a part of a class body.
  69. * This checks tokens from a specified token to a next MethodDefinition or the end of class body.
  70. * @param {Token} firstToken The first token to check.
  71. * @returns {void}
  72. */
  73. function checkForPartOfClassBody(firstToken) {
  74. for (let token = firstToken;
  75. token.type === "Punctuator" && !astUtils.isClosingBraceToken(token);
  76. token = sourceCode.getTokenAfter(token)
  77. ) {
  78. if (astUtils.isSemicolonToken(token)) {
  79. report(token);
  80. }
  81. }
  82. }
  83. return {
  84. /**
  85. * Reports this empty statement, except if the parent node is a loop.
  86. * @param {Node} node A EmptyStatement node to be reported.
  87. * @returns {void}
  88. */
  89. EmptyStatement(node) {
  90. const parent = node.parent,
  91. allowedParentTypes = [
  92. "ForStatement",
  93. "ForInStatement",
  94. "ForOfStatement",
  95. "WhileStatement",
  96. "DoWhileStatement",
  97. "IfStatement",
  98. "LabeledStatement",
  99. "WithStatement"
  100. ];
  101. if (!allowedParentTypes.includes(parent.type)) {
  102. report(node);
  103. }
  104. },
  105. /**
  106. * Checks tokens from the head of this class body to the first MethodDefinition or the end of this class body.
  107. * @param {Node} node A ClassBody node to check.
  108. * @returns {void}
  109. */
  110. ClassBody(node) {
  111. checkForPartOfClassBody(sourceCode.getFirstToken(node, 1)); // 0 is `{`.
  112. },
  113. /**
  114. * Checks tokens from this MethodDefinition to the next MethodDefinition or the end of this class body.
  115. * @param {Node} node A MethodDefinition node of the start point.
  116. * @returns {void}
  117. */
  118. "MethodDefinition, PropertyDefinition, StaticBlock"(node) {
  119. checkForPartOfClassBody(sourceCode.getTokenAfter(node));
  120. }
  121. };
  122. }
  123. };