diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index 7db7b14eda96f..c26c14f61b84e 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -600,22 +600,27 @@ impl<'a> Codegen<'a> { candidates.push(format!("0x{:x}", num as u128)); } + // create `1e-2` if s.starts_with(".0") { - // create `1e-2` if let Some((i, _)) = s[1..].bytes().enumerate().find(|(_, c)| *c != b'0') { let len = i + 1; // `+1` to include the dot. let digits = &s[len..]; candidates.push(format!("{digits}e-{}", digits.len() + len - 1)); } - } else if s.ends_with('0') { - // create 1e2 + } + + // create 1e2 + if s.ends_with('0') { if let Some((len, _)) = s.bytes().rev().enumerate().find(|(_, c)| *c != b'0') { candidates.push(format!("{}e{len}", &s[0..s.len() - len])); } - } else if let Some((integer, point, exponent)) = + } + + // `1.2e101` -> ("1", "2", "101") + // `1.3415205933077406e300` -> `13415205933077406e284;` + if let Some((integer, point, exponent)) = s.split_once('.').and_then(|(a, b)| b.split_once('e').map(|e| (a, e.0, e.1))) { - // `1.2e101` -> ("1", "2", "101") candidates.push(format!( "{integer}{point}e{}", exponent.parse::().unwrap() - point.len() as isize diff --git a/crates/oxc_codegen/tests/integration/esbuild.rs b/crates/oxc_codegen/tests/integration/esbuild.rs index 444205e38744a..189653afce97f 100644 --- a/crates/oxc_codegen/tests/integration/esbuild.rs +++ b/crates/oxc_codegen/tests/integration/esbuild.rs @@ -298,6 +298,8 @@ fn test_call() { fn test_member() { test("x.y[z]", "x.y[z];\n"); test("((x+1).y+1)[z]", "((x + 1).y + 1)[z];\n"); + + test_minify("1.3415205933077406e300", "13415205933077406e284;"); } #[test]