[parser] Disallow numeric separator in unicode scape sequences (#10468)
* [parser] Disallow numeric separator in unicode scape sequences (#10460) * raise error only when numeric separator plugin is set * Adds argument for checking numeric separator * Fix condition for readability * Add test for hex escape sequence and rephrase error message * Remove exposure for allowNumSeparator in readHexChar method
This commit is contained in:
committed by
Nicolò Ribaudo
parent
3069747a81
commit
f339d2d034
@@ -880,7 +880,11 @@ export default class Tokenizer extends LocationParser {
|
||||
// were read, the integer value otherwise. When `len` is given, this
|
||||
// will return `null` unless the integer has exactly `len` digits.
|
||||
|
||||
readInt(radix: number, len?: number): number | null {
|
||||
readInt(
|
||||
radix: number,
|
||||
len?: number,
|
||||
allowNumSeparator: boolean = true,
|
||||
): number | null {
|
||||
const start = this.state.pos;
|
||||
const forbiddenSiblings =
|
||||
radix === 16
|
||||
@@ -917,6 +921,13 @@ export default class Tokenizer extends LocationParser {
|
||||
this.raise(this.state.pos, "Invalid or unexpected token");
|
||||
}
|
||||
|
||||
if (!allowNumSeparator) {
|
||||
this.raise(
|
||||
this.state.pos,
|
||||
"Numeric separators are not allowed inside unicode escape sequences or hex escape sequences",
|
||||
);
|
||||
}
|
||||
|
||||
// Ignore this _ character
|
||||
++this.state.pos;
|
||||
continue;
|
||||
@@ -1252,7 +1263,7 @@ export default class Tokenizer extends LocationParser {
|
||||
|
||||
readHexChar(len: number, throwOnInvalid: boolean): number | null {
|
||||
const codePos = this.state.pos;
|
||||
const n = this.readInt(16, len);
|
||||
const n = this.readInt(16, len, false);
|
||||
if (n === null) {
|
||||
if (throwOnInvalid) {
|
||||
this.raise(codePos, "Bad character escape sequence");
|
||||
|
||||
1
packages/babel-parser/test/fixtures/experimental/numeric-separator/invalid-hex/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/experimental/numeric-separator/invalid-hex/input.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
"\x1_0";
|
||||
1
packages/babel-parser/test/fixtures/experimental/numeric-separator/invalid-hex/options.json
vendored
Normal file
1
packages/babel-parser/test/fixtures/experimental/numeric-separator/invalid-hex/options.json
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{ "throws": "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences (1:4)" }
|
||||
@@ -0,0 +1 @@
|
||||
"\u12_34"
|
||||
@@ -0,0 +1 @@
|
||||
{ "throws": "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences (1:5)" }
|
||||
1
packages/babel-parser/test/fixtures/experimental/numeric-separator/invalid-unicode/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/experimental/numeric-separator/invalid-unicode/input.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
"\u{1F_639}"
|
||||
@@ -0,0 +1 @@
|
||||
{ "throws": "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences (1:6)" }
|
||||
Reference in New Issue
Block a user