Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 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. * @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. * @returns {void}
  46. */
  47. function exitBreakableStatement() {
  48. scopeInfo = scopeInfo.upper;
  49. }
  50. /**
  51. * Creates a new scope with a labeled statement.
  52. *
  53. * This ignores it if the body is a breakable statement.
  54. * In this case it's handled in the `enterBreakableStatement` function.
  55. * @param {ASTNode} node A node to create. This is a LabeledStatement.
  56. * @returns {void}
  57. */
  58. function enterLabeledStatement(node) {
  59. if (!astUtils.isBreakableStatement(node.body)) {
  60. scopeInfo = {
  61. label: node.label,
  62. breakable: false,
  63. upper: scopeInfo
  64. };
  65. }
  66. }
  67. /**
  68. * Removes the top scope of the stack.
  69. *
  70. * This ignores it if the body is a breakable statement.
  71. * In this case it's handled in the `exitBreakableStatement` function.
  72. * @param {ASTNode} node A node. This is a LabeledStatement.
  73. * @returns {void}
  74. */
  75. function exitLabeledStatement(node) {
  76. if (!astUtils.isBreakableStatement(node.body)) {
  77. scopeInfo = scopeInfo.upper;
  78. }
  79. }
  80. /**
  81. * Reports a given control node if it's unnecessary.
  82. * @param {ASTNode} node A node. This is a BreakStatement or a
  83. * ContinueStatement.
  84. * @returns {void}
  85. */
  86. function reportIfUnnecessary(node) {
  87. if (!node.label) {
  88. return;
  89. }
  90. const labelNode = node.label;
  91. for (let info = scopeInfo; info !== null; info = info.upper) {
  92. if (info.breakable || info.label && info.label.name === labelNode.name) {
  93. if (info.breakable && info.label && info.label.name === labelNode.name) {
  94. context.report({
  95. node: labelNode,
  96. messageId: "unexpected",
  97. data: labelNode,
  98. fix(fixer) {
  99. const breakOrContinueToken = sourceCode.getFirstToken(node);
  100. if (sourceCode.commentsExistBetween(breakOrContinueToken, labelNode)) {
  101. return null;
  102. }
  103. return fixer.removeRange([breakOrContinueToken.range[1], labelNode.range[1]]);
  104. }
  105. });
  106. }
  107. return;
  108. }
  109. }
  110. }
  111. return {
  112. WhileStatement: enterBreakableStatement,
  113. "WhileStatement:exit": exitBreakableStatement,
  114. DoWhileStatement: enterBreakableStatement,
  115. "DoWhileStatement:exit": exitBreakableStatement,
  116. ForStatement: enterBreakableStatement,
  117. "ForStatement:exit": exitBreakableStatement,
  118. ForInStatement: enterBreakableStatement,
  119. "ForInStatement:exit": exitBreakableStatement,
  120. ForOfStatement: enterBreakableStatement,
  121. "ForOfStatement:exit": exitBreakableStatement,
  122. SwitchStatement: enterBreakableStatement,
  123. "SwitchStatement:exit": exitBreakableStatement,
  124. LabeledStatement: enterLabeledStatement,
  125. "LabeledStatement:exit": exitLabeledStatement,
  126. BreakStatement: reportIfUnnecessary,
  127. ContinueStatement: reportIfUnnecessary
  128. };
  129. }
  130. };