项目原始demo,不改动
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
Questo repository è archiviato. Puoi vedere i file e clonarli, ma non puoi effettuare richieste di pushj o aprire problemi/richieste di pull.
 
 
 
 

146 righe
4.7 KiB

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