Skip to content

Commit 8810bd5

Browse files
committed
fix(linter): prevent conflicting fixes between prefer_number_properties and prefer_numeric_literals (#16113)
When both rules apply to parseInt calls with radix 2, 8, or 16, their fixes conflict and produce invalid code like `Number.0o111`. prefer_number_properties now skips emitting a fix for parseInt calls that would be better handled by prefer_numeric_literals (which converts them to numeric literals like 0b10, 0o17, 0x1F).
1 parent 39fd864 commit 8810bd5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

crates/oxc_linter/src/rules/unicorn/prefer_number_properties.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,13 @@ impl Rule for PreferNumberProperties {
188188

189189
let fixer = |fixer: RuleFixer<'_, 'a>| match &call_expr.callee {
190190
Expression::Identifier(ident) => {
191-
fixer.insert_text_before(&ident.span, "Number.")
191+
// Use replace on the full call expression span instead of insert_text_before.
192+
// This ensures the fix span overlaps with prefer_numeric_literals fixes,
193+
// so the fixer's conflict resolution will skip one of them instead of
194+
// applying both and producing invalid code like `Number.0o111`.
195+
let args_span = Span::new(ident.span.end, call_expr.span.end);
196+
let args_text = ctx.source_range(args_span);
197+
fixer.replace(call_expr.span, format!("Number.{ident_name}{args_text}"))
192198
}
193199
match_member_expression!(Expression) => {
194200
let member_expr = call_expr.callee.to_member_expression();
@@ -550,6 +556,7 @@ function inner() {
550556
"let a = +(Number.POSITIVE_INFINITY)",
551557
Some(json!([{"checkInfinity":true}])),
552558
),
559+
(r#"parseInt("111", 8)"#, r#"Number.parseInt("111", 8)"#, None),
553560
];
554561

555562
Tester::new(PreferNumberProperties::NAME, PreferNumberProperties::PLUGIN, pass, fail)

0 commit comments

Comments
 (0)