Skip to content

Commit

Permalink
fix(linter): incorrect fixer for no-unused-labels (#4123)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Jul 8, 2024
1 parent c4ee9f8 commit 1b91d40
Showing 1 changed file with 18 additions and 9 deletions.
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();
}

0 comments on commit 1b91d40

Please sign in to comment.