Skip to content

Commit

Permalink
fix(linter): panic in no-zero-fractions (#6607)
Browse files Browse the repository at this point in the history
closes #6584 

Only fixed character index errors on the linter side.
  • Loading branch information
shulaoda authored Oct 15, 2024
1 parent 5213ba7 commit d07a9b0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions crates/oxc_linter/src/rules/unicorn/no_zero_fractions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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![
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_linter/src/snapshots/no_zero_fractions.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`

0 comments on commit d07a9b0

Please sign in to comment.