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

fix(linter): incorrect fixer for no-unused-labels #4123

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions crates/oxc_linter/src/rules/eslint/no_unused_labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ impl Rule for NoUnusedLabels {
}
for id in ctx.semantic().unused_labels() {
let node = ctx.semantic().nodes().get_node(*id);
if let AstKind::LabeledStatement(stmt) = node.kind() {
// TODO: Ignore fix where comments exist between label and statement
// e.g. A: /* Comment */ function foo(){}
ctx.diagnostic_with_fix(
no_unused_labels_diagnostic(stmt.label.name.as_str(), stmt.label.span),
|fixer| fixer.delete_range(stmt.label.span),
);
}
let AstKind::LabeledStatement(stmt) = node.kind() else {
continue;
};
ctx.diagnostic_with_fix(
no_unused_labels_diagnostic(stmt.label.name.as_str(), stmt.label.span),
|fixer| fixer.replace_with(stmt, &stmt.body),
);
}
}
}
Expand Down Expand Up @@ -85,6 +84,16 @@ fn test() {
("A: /* comment */ foo", None),
("A /* comment */: foo", None),
];
let fix = vec![
("A: var foo = 0;", "var foo = 0;", None),
("A: /* comment */ foo", "foo", None),
("A /* comment */: foo", "foo", None),
(
"A: for (var i = 0; i < 10; ++i) { B: break A; }",
"A: for (var i = 0; i < 10; ++i) { break A; }",
None,
),
];

Tester::new(NoUnusedLabels::NAME, pass, fail).test_and_snapshot();
Tester::new(NoUnusedLabels::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}
Loading