Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[parser] Disallow numeric separator in unicode scape sequences #10468

Merged
merged 7 commits into from Sep 23, 2019
13 changes: 13 additions & 0 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -897,6 +897,19 @@ export default class Tokenizer extends LocationParser {

let total = 0;

// Called from readHexChar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than readCodePoint, readHexChar could also be called from readRadixNumber. Did you checked whether 0xf_f can be successfully parsed?

We could add an extra allowNumSeparator: boolean to readHexChar

readHexChar(len: number, throwOnInvalid: boolean, allowNumSeparator: boolean): number | null

and set allowNumSeparator = true when calling from readCodePoint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

if (radix === 16) {
const unicode = this.input.slice(this.state.pos, this.state.pos + len);

if (unicode.includes("_")) {
const numSeparatorPos = this.input.indexOf("_");
this.raise(
Copy link
Contributor

@JLHwung JLHwung Sep 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should throw an error like this if user does not enable numericSeparator parser plugin. Consider moving the error logic inside this.hasPlugin("numericSeparator") branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right.

numSeparatorPos,
"Numeric separators are not allowed inside unicode escape sequences",
);
}
}

for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
const code = this.input.charCodeAt(this.state.pos);
let val;
Expand Down
@@ -0,0 +1 @@
"\x1_2"
@@ -0,0 +1 @@
{ "throws": "Numeric separators are not allowed inside unicode escape sequences (1:4)" }
@@ -0,0 +1 @@
"\u12_34"
@@ -0,0 +1 @@
{ "throws": "Numeric separators are not allowed inside unicode escape sequences (1:5)" }
@@ -0,0 +1 @@
"\u{1F_639}"
@@ -0,0 +1 @@
{ "throws": "Numeric separators are not allowed inside unicode escape sequences (1:6)" }
458 changes: 450 additions & 8 deletions scripts/tests/test262/test262_whitelist.txt

Large diffs are not rendered by default.