mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(linter): incorrect fixer for no-unused-labels (#4123)
This commit is contained in:
parent
c4ee9f8ec6
commit
1b91d40da2
1 changed files with 18 additions and 9 deletions
|
|
@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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…
Reference in a new issue