Skip to content

Commit

Permalink
Update: add fixer for no-unused-labels (#7841)
Browse files Browse the repository at this point in the history
* Update: add fixer for no-unused-labels

* Don't do a fix if comments interfere
  • Loading branch information
not-an-aardvark committed Feb 7, 2017
1 parent f47fb98 commit 1e3d4c6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/rules/no-unused-labels.md
@@ -1,5 +1,7 @@
# Disallow Unused Labels (no-unused-labels)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.

Labels that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring.

```js
Expand Down
19 changes: 17 additions & 2 deletions lib/rules/no-unused-labels.js
Expand Up @@ -17,10 +17,13 @@ module.exports = {
recommended: true
},

schema: []
schema: [],

fixable: "code"
},

create(context) {
const sourceCode = context.getSourceCode();
let scopeInfo = null;

/**
Expand Down Expand Up @@ -49,7 +52,19 @@ module.exports = {
context.report({
node: node.label,
message: "'{{name}}:' is defined but never used.",
data: node.label
data: node.label,
fix(fixer) {

/*
* Only perform a fix if there are no comments between the label and the body. This will be the case
* when there is exactly one token/comment (the ":") between the label and the body.
*/
if (sourceCode.getTokenOrCommentAfter(node.label) === sourceCode.getTokenOrCommentBefore(node.body)) {
return fixer.removeRange([node.range[0], node.body.range[0]]);
}

return null;
}
});
}

Expand Down
52 changes: 45 additions & 7 deletions tests/lib/rules/no-unused-labels.js
Expand Up @@ -29,13 +29,51 @@ ruleTester.run("no-unused-labels", rule, {
"A: { var A = 0; console.log(A); break A; console.log(A); }"
],
invalid: [
{ code: "A: var foo = 0;", errors: ["'A:' is defined but never used."] },
{ code: "A: { foo(); bar(); }", errors: ["'A:' is defined but never used."] },
{ code: "A: if (a) { foo(); bar(); }", errors: ["'A:' is defined but never used."] },
{ code: "A: for (var i = 0; i < 10; ++i) { foo(); if (a) break; bar(); }", errors: ["'A:' is defined but never used."] },
{ code: "A: for (var i = 0; i < 10; ++i) { foo(); if (a) continue; bar(); }", errors: ["'A:' is defined but never used."] },
{ code: "A: for (var i = 0; i < 10; ++i) { B: break A; }", errors: ["'B:' is defined but never used."] },
{ code: "A: { var A = 0; console.log(A); }", errors: ["'A:' is defined but never used."] }
{
code: "A: var foo = 0;",
output: "var foo = 0;",
errors: ["'A:' is defined but never used."]
},
{
code: "A: { foo(); bar(); }",
output: "{ foo(); bar(); }",
errors: ["'A:' is defined but never used."]
},
{
code: "A: if (a) { foo(); bar(); }",
output: "if (a) { foo(); bar(); }",
errors: ["'A:' is defined but never used."]
},
{
code: "A: for (var i = 0; i < 10; ++i) { foo(); if (a) break; bar(); }",
output: "for (var i = 0; i < 10; ++i) { foo(); if (a) break; bar(); }",
errors: ["'A:' is defined but never used."]
},
{
code: "A: for (var i = 0; i < 10; ++i) { foo(); if (a) continue; bar(); }",
output: "for (var i = 0; i < 10; ++i) { foo(); if (a) continue; bar(); }",
errors: ["'A:' is defined but never used."]
},
{
code: "A: for (var i = 0; i < 10; ++i) { B: break A; }",
output: "A: for (var i = 0; i < 10; ++i) { break A; }",
errors: ["'B:' is defined but never used."]
},
{
code: "A: { var A = 0; console.log(A); }",
output: "{ var A = 0; console.log(A); }",
errors: ["'A:' is defined but never used."]
},
{
code: "A: /* comment */ foo",
output: "A: /* comment */ foo",
errors: ["'A:' is defined but never used."]
},
{
code: "A /* comment */: foo",
output: "A /* comment */: foo",
errors: ["'A:' is defined but never used."]
}

// Below is fatal errors.
// "A: break B",
Expand Down

0 comments on commit 1e3d4c6

Please sign in to comment.