From d07a9b0fb38d8d03e0773e760251c05fe0342be1 Mon Sep 17 00:00:00 2001 From: dalaoshu Date: Wed, 16 Oct 2024 04:48:11 +0800 Subject: [PATCH] fix(linter): panic in `no-zero-fractions` (#6607) closes #6584 Only fixed character index errors on the linter side. --- crates/oxc_linter/src/rules/unicorn/no_zero_fractions.rs | 5 +++-- crates/oxc_linter/src/snapshots/no_zero_fractions.snap | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/oxc_linter/src/rules/unicorn/no_zero_fractions.rs b/crates/oxc_linter/src/rules/unicorn/no_zero_fractions.rs index f3e81ec59..6ffebc418 100644 --- a/crates/oxc_linter/src/rules/unicorn/no_zero_fractions.rs +++ b/crates/oxc_linter/src/rules/unicorn/no_zero_fractions.rs @@ -88,9 +88,8 @@ impl Rule for NoZeroFractions { // Handle special cases where a space is needed after certain keywords // to prevent the number from being interpreted as a property access - let start = number_literal.span.start.saturating_sub(6); let end = number_literal.span.start; - let token = ctx.source_range(oxc_span::Span::new(start, end)).to_string(); + let token = ctx.source_range(oxc_span::Span::new(0, end)); if token.ends_with("return") || token.ends_with("throw") || token.ends_with("typeof") @@ -173,6 +172,7 @@ fn test() { r"function foo(){return.0}", r"function foo(){return.0.toString()}", r"function foo(){return.0+.1}", + "ôTest(0.)", ]; let fix = vec![ @@ -210,6 +210,7 @@ fn test() { (r"void.0", r"void 0"), (r"function foo(){void.0.toString()}", r"function foo(){void (0).toString()}"), (r"function foo(){void.0+.1;}", r"function foo(){void 0+.1;}"), + ("ôTest(0.)", "ôTest(0)"), ]; Tester::new(NoZeroFractions::NAME, pass, fail).expect_fix(fix).test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/no_zero_fractions.snap b/crates/oxc_linter/src/snapshots/no_zero_fractions.snap index 9dbb67fb6..08408e8a2 100644 --- a/crates/oxc_linter/src/snapshots/no_zero_fractions.snap +++ b/crates/oxc_linter/src/snapshots/no_zero_fractions.snap @@ -168,3 +168,10 @@ source: crates/oxc_linter/src/tester.rs · ── ╰──── help: Replace the number literal with `0` + + ⚠ eslint-plugin-unicorn(no-zero-fractions): Don't use a dangling dot in the number. + ╭─[no_zero_fractions.tsx:1:8] + 1 │ ôTest(0.) + · ── + ╰──── + help: Replace the number literal with `0`