Skip to content

Commit 6b3644b

Browse files
authored
fix(es/parser): Disallow NumericLiteralSeparator with BigInts (#11510)
**Description:** I'm using swc in my JS Engine [Yavashark](github.com/Sharktheone/yavashark). I noticed, some test262 tests that were crashing in swc internally. Example: [test262](https://github.com/tc39/test262/blob/main/test/language/literals/bigint/numeric-separators/numeric-separator-literal-bil-bd-nsl-bd-err.js), [result](https://yavashark.dev/test262/#/v/language/literals/bigint/numeric-separators/numeric-separator-literal-bil-bd-nsl-bd-err.js). These tests are panicking because there was a missing check whether the literal contains any separators / underscores which would cause a failure in the `num_bigint` parser. I've also added a fallback to not panic even when the `num_bigint` parse fails.
1 parent b94a178 commit 6b3644b

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

.changeset/pink-goats-learn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_parser: patch
3+
swc_core: patch
4+
---
5+
6+
fix(es/parser): Disallow NumericLiteralSeparator with BigInts

crates/swc_ecma_parser/src/lexer/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,21 @@ impl<'a> Lexer<'a> {
11601160
self.input_slice_str(lazy_integer.start, self.cur_pos())
11611161
};
11621162
if self.eat(b'n') {
1163-
let bigint_value = num_bigint::BigInt::parse_bytes(s.as_bytes(), RADIX as _).unwrap();
1163+
if s.starts_with('_') {
1164+
return Err(Error::new(
1165+
Span::new(lazy_integer.start, lazy_integer.end),
1166+
SyntaxError::NumericSeparatorIsAllowedOnlyBetweenTwoDigits,
1167+
));
1168+
}
1169+
1170+
let Some(bigint_value) = num_bigint::BigInt::parse_bytes(s.as_bytes(), RADIX as _)
1171+
else {
1172+
// just a fallback in case there is anything we did not catch
1173+
return Err(Error::new(
1174+
Span::new(lazy_integer.start, lazy_integer.end),
1175+
SyntaxError::ExpectedDigit { radix: RADIX },
1176+
));
1177+
};
11641178
return Ok(Either::Right(Box::new(bigint_value)));
11651179
}
11661180
let s = remove_underscore(s, has_underscore);

0 commit comments

Comments
 (0)