Skip to content

Commit

Permalink
[parser] Disallow numeric separator in unicode scape sequences (#10468)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
ivandevp authored and nicolo-ribaudo committed Sep 23, 2019
1 parent 3069747 commit f339d2d
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 10 deletions.
15 changes: 13 additions & 2 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
@@ -0,0 +1 @@
"\x1_0";
@@ -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)" }
@@ -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)" }
8 changes: 0 additions & 8 deletions scripts/tests/test262/test262_whitelist.txt
Expand Up @@ -104,10 +104,6 @@ language/expressions/object/method-definition/private-name-early-error-gen-insid
language/expressions/object/method-definition/private-name-early-error-gen-inside-class.js(strict mode)
language/expressions/object/method-definition/private-name-early-error-method-inside-class.js(default)
language/expressions/object/method-definition/private-name-early-error-method-inside-class.js(strict mode)
language/expressions/template-literal/unicode-escape-nls-err.js(default)
language/expressions/template-literal/unicode-escape-nls-err.js(strict mode)
language/identifiers/unicode-escape-nls-err.js(default)
language/identifiers/unicode-escape-nls-err.js(strict mode)
language/literals/bigint/non-octal-like-invalid-0008n.js(default)
language/literals/bigint/non-octal-like-invalid-012348n.js(default)
language/literals/bigint/non-octal-like-invalid-08n.js(default)
Expand All @@ -128,10 +124,6 @@ language/literals/numeric/numeric-separators/numeric-separator-literal-nonoctal-
language/literals/numeric/numeric-separators/numeric-separator-literal-nonoctal-0_8-err.js(default)
language/literals/numeric/numeric-separators/numeric-separator-literal-nonoctal-0_9-err.js(default)
language/literals/numeric/numeric-separators/numeric-separator-literal-nzd-nsl-dds-leading-zero-err.js(default)
language/literals/string/unicode-escape-nls-err-double.js(default)
language/literals/string/unicode-escape-nls-err-double.js(strict mode)
language/literals/string/unicode-escape-nls-err-single.js(default)
language/literals/string/unicode-escape-nls-err-single.js(strict mode)
language/module-code/privatename-not-valid-earlyerr-module-1.js(default)
language/module-code/privatename-not-valid-earlyerr-module-1.js(strict mode)
language/module-code/privatename-not-valid-earlyerr-module-2.js(default)
Expand Down

0 comments on commit f339d2d

Please sign in to comment.