1 | /**
|
---|
2 | * @fileoverview Rule to disallow unused labels.
|
---|
3 | * @author Toru Nagashima
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | //------------------------------------------------------------------------------
|
---|
9 | // Requirements
|
---|
10 | //------------------------------------------------------------------------------
|
---|
11 |
|
---|
12 | const astUtils = require("./utils/ast-utils");
|
---|
13 |
|
---|
14 | //------------------------------------------------------------------------------
|
---|
15 | // Rule Definition
|
---|
16 | //------------------------------------------------------------------------------
|
---|
17 |
|
---|
18 | /** @type {import('../shared/types').Rule} */
|
---|
19 | module.exports = {
|
---|
20 | meta: {
|
---|
21 | type: "suggestion",
|
---|
22 |
|
---|
23 | docs: {
|
---|
24 | description: "Disallow unused labels",
|
---|
25 | recommended: true,
|
---|
26 | url: "https://eslint.org/docs/latest/rules/no-unused-labels"
|
---|
27 | },
|
---|
28 |
|
---|
29 | schema: [],
|
---|
30 |
|
---|
31 | fixable: "code",
|
---|
32 |
|
---|
33 | messages: {
|
---|
34 | unused: "'{{name}}:' is defined but never used."
|
---|
35 | }
|
---|
36 | },
|
---|
37 |
|
---|
38 | create(context) {
|
---|
39 | const sourceCode = context.sourceCode;
|
---|
40 | let scopeInfo = null;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Adds a scope info to the stack.
|
---|
44 | * @param {ASTNode} node A node to add. This is a LabeledStatement.
|
---|
45 | * @returns {void}
|
---|
46 | */
|
---|
47 | function enterLabeledScope(node) {
|
---|
48 | scopeInfo = {
|
---|
49 | label: node.label.name,
|
---|
50 | used: false,
|
---|
51 | upper: scopeInfo
|
---|
52 | };
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Checks if a `LabeledStatement` node is fixable.
|
---|
57 | * For a node to be fixable, there must be no comments between the label and the body.
|
---|
58 | * Furthermore, is must be possible to remove the label without turning the body statement into a
|
---|
59 | * directive after other fixes are applied.
|
---|
60 | * @param {ASTNode} node The node to evaluate.
|
---|
61 | * @returns {boolean} Whether or not the node is fixable.
|
---|
62 | */
|
---|
63 | function isFixable(node) {
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Only perform a fix if there are no comments between the label and the body. This will be the case
|
---|
67 | * when there is exactly one token/comment (the ":") between the label and the body.
|
---|
68 | */
|
---|
69 | if (sourceCode.getTokenAfter(node.label, { includeComments: true }) !==
|
---|
70 | sourceCode.getTokenBefore(node.body, { includeComments: true })) {
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | // Looking for the node's deepest ancestor which is not a `LabeledStatement`.
|
---|
75 | let ancestor = node.parent;
|
---|
76 |
|
---|
77 | while (ancestor.type === "LabeledStatement") {
|
---|
78 | ancestor = ancestor.parent;
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (ancestor.type === "Program" ||
|
---|
82 | (ancestor.type === "BlockStatement" && astUtils.isFunction(ancestor.parent))) {
|
---|
83 | const { body } = node;
|
---|
84 |
|
---|
85 | if (body.type === "ExpressionStatement" &&
|
---|
86 | ((body.expression.type === "Literal" && typeof body.expression.value === "string") ||
|
---|
87 | astUtils.isStaticTemplateLiteral(body.expression))) {
|
---|
88 | return false; // potential directive
|
---|
89 | }
|
---|
90 | }
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Removes the top of the stack.
|
---|
96 | * At the same time, this reports the label if it's never used.
|
---|
97 | * @param {ASTNode} node A node to report. This is a LabeledStatement.
|
---|
98 | * @returns {void}
|
---|
99 | */
|
---|
100 | function exitLabeledScope(node) {
|
---|
101 | if (!scopeInfo.used) {
|
---|
102 | context.report({
|
---|
103 | node: node.label,
|
---|
104 | messageId: "unused",
|
---|
105 | data: node.label,
|
---|
106 | fix: isFixable(node) ? fixer => fixer.removeRange([node.range[0], node.body.range[0]]) : null
|
---|
107 | });
|
---|
108 | }
|
---|
109 |
|
---|
110 | scopeInfo = scopeInfo.upper;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Marks the label of a given node as used.
|
---|
115 | * @param {ASTNode} node A node to mark. This is a BreakStatement or
|
---|
116 | * ContinueStatement.
|
---|
117 | * @returns {void}
|
---|
118 | */
|
---|
119 | function markAsUsed(node) {
|
---|
120 | if (!node.label) {
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | const label = node.label.name;
|
---|
125 | let info = scopeInfo;
|
---|
126 |
|
---|
127 | while (info) {
|
---|
128 | if (info.label === label) {
|
---|
129 | info.used = true;
|
---|
130 | break;
|
---|
131 | }
|
---|
132 | info = info.upper;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | return {
|
---|
137 | LabeledStatement: enterLabeledScope,
|
---|
138 | "LabeledStatement:exit": exitLabeledScope,
|
---|
139 | BreakStatement: markAsUsed,
|
---|
140 | ContinueStatement: markAsUsed
|
---|
141 | };
|
---|
142 | }
|
---|
143 | };
|
---|