Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: add fixer for no-unused-labels #7841

Merged
merged 2 commits into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/no-unused-labels.md
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions lib/rules/no-unused-labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module.exports = {
recommended: true
},

schema: []
schema: [],

fixable: "code"
},

create(context) {
Expand Down Expand Up @@ -49,7 +51,8 @@ module.exports = {
context.report({
node: node.label,
message: "'{{name}}:' is defined but never used.",
data: node.label
data: node.label,
fix: fixer => fixer.removeRange([node.range[0], node.body.range[0]])
});
}

Expand Down
42 changes: 35 additions & 7 deletions tests/lib/rules/no-unused-labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,41 @@ 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."]
}

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