Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/oxc_formatter/src/formatter/token/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ pub fn format_trimmed_number(text: &str, options: NumberFormatOptions) -> Cow<'_
use FormatNumberLiteralState::{DecimalPart, Exponent, IntegerPart};

let text = text.cow_to_ascii_lowercase();

// Bail out for numbers with numeric separators (underscores).
// Prettier's regex-based approach preserves them implicitly because `\d` doesn't match `_`.
// The only transformation that still applies is adding a leading zero (`.1_1` → `0.1_1`).
if text.contains('_') {
return if text.starts_with('.') { Cow::Owned(format!("0{text}")) } else { text };
}

let mut copied_or_ignored_chars = 0usize;
let mut iter = text.bytes().enumerate();
let mut curr = iter.next();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Issue #18968 - numeric separators should be preserved
const x = 123.000_000_000_000_000_000_000_0;
const y = 1_000_000;
const z = 0.000_1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
// Issue #18968 - numeric separators should be preserved
const x = 123.000_000_000_000_000_000_000_0;
const y = 1_000_000;
const z = 0.000_1;

==================== Output ====================
------------------
{ printWidth: 80 }
------------------
// Issue #18968 - numeric separators should be preserved
const x = 123.000_000_000_000_000_000_000_0;
const y = 1_000_000;
const z = 0.000_1;

-------------------
{ printWidth: 100 }
-------------------
// Issue #18968 - numeric separators should be preserved
const x = 123.000_000_000_000_000_000_000_0;
const y = 1_000_000;
const z = 0.000_1;

===================== End =====================
Loading