Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-extra-semi.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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("../util/fix-tracker");
  10. const astUtils = require("../util/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. *
  56. * @param {Token} firstToken - The first token to check.
  57. * @returns {void}
  58. */
  59. function checkForPartOfClassBody(firstToken) {
  60. for (let token = firstToken;
  61. token.type === "Punctuator" && !astUtils.isClosingBraceToken(token);
  62. token = sourceCode.getTokenAfter(token)
  63. ) {
  64. if (astUtils.isSemicolonToken(token)) {
  65. report(token);
  66. }
  67. }
  68. }
  69. return {
  70. /**
  71. * Reports this empty statement, except if the parent node is a loop.
  72. * @param {Node} node - A EmptyStatement node to be reported.
  73. * @returns {void}
  74. */
  75. EmptyStatement(node) {
  76. const parent = node.parent,
  77. allowedParentTypes = [
  78. "ForStatement",
  79. "ForInStatement",
  80. "ForOfStatement",
  81. "WhileStatement",
  82. "DoWhileStatement",
  83. "IfStatement",
  84. "LabeledStatement",
  85. "WithStatement"
  86. ];
  87. if (allowedParentTypes.indexOf(parent.type) === -1) {
  88. report(node);
  89. }
  90. },
  91. /**
  92. * Checks tokens from the head of this class body to the first MethodDefinition or the end of this class body.
  93. * @param {Node} node - A ClassBody node to check.
  94. * @returns {void}
  95. */
  96. ClassBody(node) {
  97. checkForPartOfClassBody(sourceCode.getFirstToken(node, 1)); // 0 is `{`.
  98. },
  99. /**
  100. * Checks tokens from this MethodDefinition to the next MethodDefinition or the end of this class body.
  101. * @param {Node} node - A MethodDefinition node of the start point.
  102. * @returns {void}
  103. */
  104. MethodDefinition(node) {
  105. checkForPartOfClassBody(sourceCode.getTokenAfter(node));
  106. }
  107. };
  108. }
  109. };