no-extra-semi.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. module.exports = {
  15. meta: {
  16. type: "suggestion",
  17. docs: {
  18. description: "disallow unnecessary semicolons",
  19. category: "Possible Errors",
  20. recommended: true,
  21. url: "https://eslint.org/docs/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.getSourceCode();
  31. /**
  32. * Reports an unnecessary semicolon error.
  33. * @param {Node|Token} nodeOrToken A node or a token to be reported.
  34. * @returns {void}
  35. */
  36. function report(nodeOrToken) {
  37. context.report({
  38. node: nodeOrToken,
  39. messageId: "unexpected",
  40. fix(fixer) {
  41. /*
  42. * Expand the replacement range to include the surrounding
  43. * tokens to avoid conflicting with semi.
  44. * https://github.com/eslint/eslint/issues/7928
  45. */
  46. return new FixTracker(fixer, context.getSourceCode())
  47. .retainSurroundingTokens(nodeOrToken)
  48. .remove(nodeOrToken);
  49. }
  50. });
  51. }
  52. /**
  53. * Checks for a part of a class body.
  54. * This checks tokens from a specified token to a next MethodDefinition or the end of class body.
  55. * @param {Token} firstToken The first token to check.
  56. * @returns {void}
  57. */
  58. function checkForPartOfClassBody(firstToken) {
  59. for (let token = firstToken;
  60. token.type === "Punctuator" && !astUtils.isClosingBraceToken(token);
  61. token = sourceCode.getTokenAfter(token)
  62. ) {
  63. if (astUtils.isSemicolonToken(token)) {
  64. report(token);
  65. }
  66. }
  67. }
  68. return {
  69. /**
  70. * Reports this empty statement, except if the parent node is a loop.
  71. * @param {Node} node A EmptyStatement node to be reported.
  72. * @returns {void}
  73. */
  74. EmptyStatement(node) {
  75. const parent = node.parent,
  76. allowedParentTypes = [
  77. "ForStatement",
  78. "ForInStatement",
  79. "ForOfStatement",
  80. "WhileStatement",
  81. "DoWhileStatement",
  82. "IfStatement",
  83. "LabeledStatement",
  84. "WithStatement"
  85. ];
  86. if (allowedParentTypes.indexOf(parent.type) === -1) {
  87. report(node);
  88. }
  89. },
  90. /**
  91. * Checks tokens from the head of this class body to the first MethodDefinition or the end of this class body.
  92. * @param {Node} node A ClassBody node to check.
  93. * @returns {void}
  94. */
  95. ClassBody(node) {
  96. checkForPartOfClassBody(sourceCode.getFirstToken(node, 1)); // 0 is `{`.
  97. },
  98. /**
  99. * Checks tokens from this MethodDefinition to the next MethodDefinition or the end of this class body.
  100. * @param {Node} node A MethodDefinition node of the start point.
  101. * @returns {void}
  102. */
  103. MethodDefinition(node) {
  104. checkForPartOfClassBody(sourceCode.getTokenAfter(node));
  105. }
  106. };
  107. }
  108. };