Skip to content

Commit 1e3d4c6

Browse files
Update: add fixer for no-unused-labels (#7841)
* Update: add fixer for no-unused-labels * Don't do a fix if comments interfere
1 parent f47fb98 commit 1e3d4c6

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

docs/rules/no-unused-labels.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Disallow Unused Labels (no-unused-labels)
22

3+
(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.
4+
35
Labels that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring.
46

57
```js

lib/rules/no-unused-labels.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ module.exports = {
1717
recommended: true
1818
},
1919

20-
schema: []
20+
schema: [],
21+
22+
fixable: "code"
2123
},
2224

2325
create(context) {
26+
const sourceCode = context.getSourceCode();
2427
let scopeInfo = null;
2528

2629
/**
@@ -49,7 +52,19 @@ module.exports = {
4952
context.report({
5053
node: node.label,
5154
message: "'{{name}}:' is defined but never used.",
52-
data: node.label
55+
data: node.label,
56+
fix(fixer) {
57+
58+
/*
59+
* Only perform a fix if there are no comments between the label and the body. This will be the case
60+
* when there is exactly one token/comment (the ":") between the label and the body.
61+
*/
62+
if (sourceCode.getTokenOrCommentAfter(node.label) === sourceCode.getTokenOrCommentBefore(node.body)) {
63+
return fixer.removeRange([node.range[0], node.body.range[0]]);
64+
}
65+
66+
return null;
67+
}
5368
});
5469
}
5570

tests/lib/rules/no-unused-labels.js

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,51 @@ ruleTester.run("no-unused-labels", rule, {
2929
"A: { var A = 0; console.log(A); break A; console.log(A); }"
3030
],
3131
invalid: [
32-
{ code: "A: var foo = 0;", errors: ["'A:' is defined but never used."] },
33-
{ code: "A: { foo(); bar(); }", errors: ["'A:' is defined but never used."] },
34-
{ code: "A: if (a) { foo(); bar(); }", errors: ["'A:' is defined but never used."] },
35-
{ code: "A: for (var i = 0; i < 10; ++i) { foo(); if (a) break; bar(); }", errors: ["'A:' is defined but never used."] },
36-
{ code: "A: for (var i = 0; i < 10; ++i) { foo(); if (a) continue; bar(); }", errors: ["'A:' is defined but never used."] },
37-
{ code: "A: for (var i = 0; i < 10; ++i) { B: break A; }", errors: ["'B:' is defined but never used."] },
38-
{ code: "A: { var A = 0; console.log(A); }", errors: ["'A:' is defined but never used."] }
32+
{
33+
code: "A: var foo = 0;",
34+
output: "var foo = 0;",
35+
errors: ["'A:' is defined but never used."]
36+
},
37+
{
38+
code: "A: { foo(); bar(); }",
39+
output: "{ foo(); bar(); }",
40+
errors: ["'A:' is defined but never used."]
41+
},
42+
{
43+
code: "A: if (a) { foo(); bar(); }",
44+
output: "if (a) { foo(); bar(); }",
45+
errors: ["'A:' is defined but never used."]
46+
},
47+
{
48+
code: "A: for (var i = 0; i < 10; ++i) { foo(); if (a) break; bar(); }",
49+
output: "for (var i = 0; i < 10; ++i) { foo(); if (a) break; bar(); }",
50+
errors: ["'A:' is defined but never used."]
51+
},
52+
{
53+
code: "A: for (var i = 0; i < 10; ++i) { foo(); if (a) continue; bar(); }",
54+
output: "for (var i = 0; i < 10; ++i) { foo(); if (a) continue; bar(); }",
55+
errors: ["'A:' is defined but never used."]
56+
},
57+
{
58+
code: "A: for (var i = 0; i < 10; ++i) { B: break A; }",
59+
output: "A: for (var i = 0; i < 10; ++i) { break A; }",
60+
errors: ["'B:' is defined but never used."]
61+
},
62+
{
63+
code: "A: { var A = 0; console.log(A); }",
64+
output: "{ var A = 0; console.log(A); }",
65+
errors: ["'A:' is defined but never used."]
66+
},
67+
{
68+
code: "A: /* comment */ foo",
69+
output: "A: /* comment */ foo",
70+
errors: ["'A:' is defined but never used."]
71+
},
72+
{
73+
code: "A /* comment */: foo",
74+
output: "A /* comment */: foo",
75+
errors: ["'A:' is defined but never used."]
76+
}
3977

4078
// Below is fatal errors.
4179
// "A: break B",

0 commit comments

Comments
 (0)