diff --git a/crates/swc_ecma_parser/src/lexer/number.rs b/crates/swc_ecma_parser/src/lexer/number.rs index febdd44277ca..3d3117b3866a 100644 --- a/crates/swc_ecma_parser/src/lexer/number.rs +++ b/crates/swc_ecma_parser/src/lexer/number.rs @@ -137,13 +137,22 @@ impl<'a, I: Input> Lexer<'a, I> { }; let exp = self.read_number_no_dot(10)?; - let flag = if positive { '+' } else { '-' }; - raw_val.push(flag); - write!(raw_val, "{}", exp).unwrap(); + val = if exp == f64::INFINITY { + if positive && val != 0.0 { + f64::INFINITY + } else { + 0.0 + } + } else { + let flag = if positive { '+' } else { '-' }; + + raw_val.push(flag); + write!(raw_val, "{}", exp).unwrap(); - // TODO: - val = raw_val.parse().expect("failed to parse float literal"); + // TODO: + raw_val.parse().expect("failed to parse float literal") + } } self.ensure_not_ident()?; @@ -478,6 +487,51 @@ mod tests { assert_eq!(1e30, num("1e30")); } + #[test] + fn num_very_big_exp() { + const LARGE_POSITIVE_EXP: &str = + "1e100000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000"; + const LARGE_NEGATIVE_EXP: &str = + "1e-10000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000"; + const ZERO_WITH_LARGE_POSITIVE_EXP: &str = + "0e100000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000"; + const ZERO_WITH_LARGE_NEGATIVE_EXP: &str = + "0e-10000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000"; + const LARGE_MANTISSA_WITH_LARGE_NEGATIVE_EXP: &str = + "10000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000\ + e-100000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 00000000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000"; + + assert_eq!(num(LARGE_POSITIVE_EXP), INFINITY); + assert_eq!(num(LARGE_NEGATIVE_EXP), 0.0); + assert_eq!(num(ZERO_WITH_LARGE_POSITIVE_EXP), 0.0); + assert_eq!(num(ZERO_WITH_LARGE_NEGATIVE_EXP), 0.0); + assert_eq!(num(LARGE_MANTISSA_WITH_LARGE_NEGATIVE_EXP), 0.0); + } + #[test] fn num_big_many_zero() { assert_eq!(