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-label.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @fileoverview Rule to disallow unnecessary labels
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "suggestion",
  16. docs: {
  17. description: "disallow unnecessary labels",
  18. category: "Best Practices",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/no-extra-label"
  21. },
  22. schema: [],
  23. fixable: "code",
  24. messages: {
  25. unexpected: "This label '{{name}}' is unnecessary."
  26. }
  27. },
  28. create(context) {
  29. const sourceCode = context.getSourceCode();
  30. let scopeInfo = null;
  31. /**
  32. * Creates a new scope with a breakable statement.
  33. *
  34. * @param {ASTNode} node - A node to create. This is a BreakableStatement.
  35. * @returns {void}
  36. */
  37. function enterBreakableStatement(node) {
  38. scopeInfo = {
  39. label: node.parent.type === "LabeledStatement" ? node.parent.label : null,
  40. breakable: true,
  41. upper: scopeInfo
  42. };
  43. }
  44. /**
  45. * Removes the top scope of the stack.
  46. *
  47. * @returns {void}
  48. */
  49. function exitBreakableStatement() {
  50. scopeInfo = scopeInfo.upper;
  51. }
  52. /**
  53. * Creates a new scope with a labeled statement.
  54. *
  55. * This ignores it if the body is a breakable statement.
  56. * In this case it's handled in the `enterBreakableStatement` function.
  57. *
  58. * @param {ASTNode} node - A node to create. This is a LabeledStatement.
  59. * @returns {void}
  60. */
  61. function enterLabeledStatement(node) {
  62. if (!astUtils.isBreakableStatement(node.body)) {
  63. scopeInfo = {
  64. label: node.label,
  65. breakable: false,
  66. upper: scopeInfo
  67. };
  68. }
  69. }
  70. /**
  71. * Removes the top scope of the stack.
  72. *
  73. * This ignores it if the body is a breakable statement.
  74. * In this case it's handled in the `exitBreakableStatement` function.
  75. *
  76. * @param {ASTNode} node - A node. This is a LabeledStatement.
  77. * @returns {void}
  78. */
  79. function exitLabeledStatement(node) {
  80. if (!astUtils.isBreakableStatement(node.body)) {
  81. scopeInfo = scopeInfo.upper;
  82. }
  83. }
  84. /**
  85. * Reports a given control node if it's unnecessary.
  86. *
  87. * @param {ASTNode} node - A node. This is a BreakStatement or a
  88. * ContinueStatement.
  89. * @returns {void}
  90. */
  91. function reportIfUnnecessary(node) {
  92. if (!node.label) {
  93. return;
  94. }
  95. const labelNode = node.label;
  96. for (let info = scopeInfo; info !== null; info = info.upper) {
  97. if (info.breakable || info.label && info.label.name === labelNode.name) {
  98. if (info.breakable && info.label && info.label.name === labelNode.name) {
  99. context.report({
  100. node: labelNode,
  101. messageId: "unexpected",
  102. data: labelNode,
  103. fix: fixer => fixer.removeRange([sourceCode.getFirstToken(node).range[1], labelNode.range[1]])
  104. });
  105. }
  106. return;
  107. }
  108. }
  109. }
  110. return {
  111. WhileStatement: enterBreakableStatement,
  112. "WhileStatement:exit": exitBreakableStatement,
  113. DoWhileStatement: enterBreakableStatement,
  114. "DoWhileStatement:exit": exitBreakableStatement,
  115. ForStatement: enterBreakableStatement,
  116. "ForStatement:exit": exitBreakableStatement,
  117. ForInStatement: enterBreakableStatement,
  118. "ForInStatement:exit": exitBreakableStatement,
  119. ForOfStatement: enterBreakableStatement,
  120. "ForOfStatement:exit": exitBreakableStatement,
  121. SwitchStatement: enterBreakableStatement,
  122. "SwitchStatement:exit": exitBreakableStatement,
  123. LabeledStatement: enterLabeledStatement,
  124. "LabeledStatement:exit": exitLabeledStatement,
  125. BreakStatement: reportIfUnnecessary,
  126. ContinueStatement: reportIfUnnecessary
  127. };
  128. }
  129. };