Skip to content

Commit

Permalink
Report single error for invalid num seps in unicode escapes (#14338)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 8, 2022
1 parent 0951316 commit 24f0944
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 23 deletions.
42 changes: 19 additions & 23 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -51,7 +51,7 @@ const VALID_REGEX_FLAGS = new Set([
// an immediate sibling of NumericLiteralSeparator _

const forbiddenNumericSeparatorSiblings = {
decBinOct: [
decBinOct: new Set([
charCodes.dot,
charCodes.uppercaseB,
charCodes.uppercaseE,
Expand All @@ -60,22 +60,22 @@ const forbiddenNumericSeparatorSiblings = {
charCodes.lowercaseB,
charCodes.lowercaseE,
charCodes.lowercaseO,
],
hex: [
]),
hex: new Set([
charCodes.dot,
charCodes.uppercaseX,
charCodes.underscore, // multiple separators are not allowed
charCodes.lowercaseX,
],
]),
};

const allowedNumericSeparatorSiblings = {};
allowedNumericSeparatorSiblings.bin = [
allowedNumericSeparatorSiblings.bin = new Set([
// 0 - 1
charCodes.digit0,
charCodes.digit1,
];
allowedNumericSeparatorSiblings.oct = [
]);
allowedNumericSeparatorSiblings.oct = new Set([
// 0 - 7
...allowedNumericSeparatorSiblings.bin,

Expand All @@ -85,16 +85,16 @@ allowedNumericSeparatorSiblings.oct = [
charCodes.digit5,
charCodes.digit6,
charCodes.digit7,
];
allowedNumericSeparatorSiblings.dec = [
]);
allowedNumericSeparatorSiblings.dec = new Set([
// 0 - 9
...allowedNumericSeparatorSiblings.oct,

charCodes.digit8,
charCodes.digit9,
];
]);

allowedNumericSeparatorSiblings.hex = [
allowedNumericSeparatorSiblings.hex = new Set([
// 0 - 9, A - F, a - f,
...allowedNumericSeparatorSiblings.dec,

Expand All @@ -111,7 +111,7 @@ allowedNumericSeparatorSiblings.hex = [
charCodes.lowercaseD,
charCodes.lowercaseE,
charCodes.lowercaseF,
];
]);

// Object type used to represent tokens. Note that normally, tokens
// simply exist as properties on the parser object. This is only
Expand Down Expand Up @@ -1199,26 +1199,22 @@ export default class Tokenizer extends CommentsParser {
if (code === charCodes.underscore && allowNumSeparator !== "bail") {
const prev = this.input.charCodeAt(this.state.pos - 1);
const next = this.input.charCodeAt(this.state.pos + 1);
if (allowedSiblings.indexOf(next) === -1) {
this.raise(Errors.UnexpectedNumericSeparator, {
if (!allowNumSeparator) {
this.raise(Errors.NumericSeparatorInEscapeSequence, {
at: this.state.curPosition(),
});
} else if (
forbiddenSiblings.indexOf(prev) > -1 ||
forbiddenSiblings.indexOf(next) > -1 ||
Number.isNaN(next)
Number.isNaN(next) ||
!allowedSiblings.has(next) ||
forbiddenSiblings.has(prev) ||
forbiddenSiblings.has(next)
) {
this.raise(Errors.UnexpectedNumericSeparator, {
at: this.state.curPosition(),
});
}
if (!allowNumSeparator) {
this.raise(Errors.NumericSeparatorInEscapeSequence, {
at: this.state.curPosition(),
});
}
// Ignore this _ character
++this.state.pos;
continue;
Expand Down
@@ -0,0 +1 @@
\u{_0061};
@@ -0,0 +1,25 @@
{
"type": "File",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}},
"errors": [
"SyntaxError: Numeric separators are not allowed inside unicode escape sequences or hex escape sequences. (1:3)"
],
"program": {
"type": "Program",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}},
"expression": {
"type": "Identifier",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9},"identifierName":"a"},
"name": "a"
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
\u{00__61};
@@ -0,0 +1,26 @@
{
"type": "File",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"errors": [
"SyntaxError: Numeric separators are not allowed inside unicode escape sequences or hex escape sequences. (1:5)",
"SyntaxError: Numeric separators are not allowed inside unicode escape sequences or hex escape sequences. (1:6)"
],
"program": {
"type": "Program",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"expression": {
"type": "Identifier",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10},"identifierName":"a"},
"name": "a"
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
\u_0061;
@@ -0,0 +1,26 @@
{
"type": "File",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}},
"errors": [
"SyntaxError: Numeric separators are not allowed inside unicode escape sequences or hex escape sequences. (1:2)",
"SyntaxError: Invalid Unicode escape. (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}},
"expression": {
"type": "Identifier",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7},"identifierName":"\u00061"},
"name": "\u00061"
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
\u00__61;
@@ -0,0 +1,27 @@
{
"type": "File",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}},
"errors": [
"SyntaxError: Numeric separators are not allowed inside unicode escape sequences or hex escape sequences. (1:4)",
"SyntaxError: Numeric separators are not allowed inside unicode escape sequences or hex escape sequences. (1:5)",
"SyntaxError: Invalid Unicode escape. (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}},
"expression": {
"type": "Identifier",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8},"identifierName":"\u000061"},
"name": "\u000061"
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
\u{0061_};
@@ -0,0 +1,25 @@
{
"type": "File",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}},
"errors": [
"SyntaxError: Numeric separators are not allowed inside unicode escape sequences or hex escape sequences. (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}},
"expression": {
"type": "Identifier",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9},"identifierName":"a"},
"name": "a"
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
\u0061_;
@@ -0,0 +1,22 @@
{
"type": "File",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}},
"program": {
"type": "Program",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}},
"expression": {
"type": "Identifier",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7},"identifierName":"a_"},
"name": "a_"
}
}
],
"directives": []
}
}

0 comments on commit 24f0944

Please sign in to comment.