no-unused-labels.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. messages: {
  21. unused: "'{{name}}:' is defined but never used."
  22. }
  23. },
  24. create(context) {
  25. const sourceCode = context.getSourceCode();
  26. let scopeInfo = null;
  27. /**
  28. * Adds a scope info to the stack.
  29. *
  30. * @param {ASTNode} node - A node to add. This is a LabeledStatement.
  31. * @returns {void}
  32. */
  33. function enterLabeledScope(node) {
  34. scopeInfo = {
  35. label: node.label.name,
  36. used: false,
  37. upper: scopeInfo
  38. };
  39. }
  40. /**
  41. * Removes the top of the stack.
  42. * At the same time, this reports the label if it's never used.
  43. *
  44. * @param {ASTNode} node - A node to report. This is a LabeledStatement.
  45. * @returns {void}
  46. */
  47. function exitLabeledScope(node) {
  48. if (!scopeInfo.used) {
  49. context.report({
  50. node: node.label,
  51. messageId: "unused",
  52. data: node.label,
  53. fix(fixer) {
  54. /*
  55. * Only perform a fix if there are no comments between the label and the body. This will be the case
  56. * when there is exactly one token/comment (the ":") between the label and the body.
  57. */
  58. if (sourceCode.getTokenAfter(node.label, { includeComments: true }) ===
  59. sourceCode.getTokenBefore(node.body, { includeComments: true })) {
  60. return fixer.removeRange([node.range[0], node.body.range[0]]);
  61. }
  62. return null;
  63. }
  64. });
  65. }
  66. scopeInfo = scopeInfo.upper;
  67. }
  68. /**
  69. * Marks the label of a given node as used.
  70. *
  71. * @param {ASTNode} node - A node to mark. This is a BreakStatement or
  72. * ContinueStatement.
  73. * @returns {void}
  74. */
  75. function markAsUsed(node) {
  76. if (!node.label) {
  77. return;
  78. }
  79. const label = node.label.name;
  80. let info = scopeInfo;
  81. while (info) {
  82. if (info.label === label) {
  83. info.used = true;
  84. break;
  85. }
  86. info = info.upper;
  87. }
  88. }
  89. return {
  90. LabeledStatement: enterLabeledScope,
  91. "LabeledStatement:exit": exitLabeledScope,
  92. BreakStatement: markAsUsed,
  93. ContinueStatement: markAsUsed
  94. };
  95. }
  96. };