no-extra-label.js 4.2 KB

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