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-unused-labels.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @fileoverview Rule to disallow unused labels.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow unused labels",
  14. category: "Best Practices",
  15. recommended: true,
  16. url: "https://eslint.org/docs/rules/no-unused-labels"
  17. },
  18. schema: [],
  19. fixable: "code"
  20. },
  21. create(context) {
  22. const sourceCode = context.getSourceCode();
  23. let scopeInfo = null;
  24. /**
  25. * Adds a scope info to the stack.
  26. *
  27. * @param {ASTNode} node - A node to add. This is a LabeledStatement.
  28. * @returns {void}
  29. */
  30. function enterLabeledScope(node) {
  31. scopeInfo = {
  32. label: node.label.name,
  33. used: false,
  34. upper: scopeInfo
  35. };
  36. }
  37. /**
  38. * Removes the top of the stack.
  39. * At the same time, this reports the label if it's never used.
  40. *
  41. * @param {ASTNode} node - A node to report. This is a LabeledStatement.
  42. * @returns {void}
  43. */
  44. function exitLabeledScope(node) {
  45. if (!scopeInfo.used) {
  46. context.report({
  47. node: node.label,
  48. message: "'{{name}}:' is defined but never used.",
  49. data: node.label,
  50. fix(fixer) {
  51. /*
  52. * Only perform a fix if there are no comments between the label and the body. This will be the case
  53. * when there is exactly one token/comment (the ":") between the label and the body.
  54. */
  55. if (sourceCode.getTokenAfter(node.label, { includeComments: true }) ===
  56. sourceCode.getTokenBefore(node.body, { includeComments: true })) {
  57. return fixer.removeRange([node.range[0], node.body.range[0]]);
  58. }
  59. return null;
  60. }
  61. });
  62. }
  63. scopeInfo = scopeInfo.upper;
  64. }
  65. /**
  66. * Marks the label of a given node as used.
  67. *
  68. * @param {ASTNode} node - A node to mark. This is a BreakStatement or
  69. * ContinueStatement.
  70. * @returns {void}
  71. */
  72. function markAsUsed(node) {
  73. if (!node.label) {
  74. return;
  75. }
  76. const label = node.label.name;
  77. let info = scopeInfo;
  78. while (info) {
  79. if (info.label === label) {
  80. info.used = true;
  81. break;
  82. }
  83. info = info.upper;
  84. }
  85. }
  86. return {
  87. LabeledStatement: enterLabeledScope,
  88. "LabeledStatement:exit": exitLabeledScope,
  89. BreakStatement: markAsUsed,
  90. ContinueStatement: markAsUsed
  91. };
  92. }
  93. };