Skip to content

Commit

Permalink
[parser] Report escapes in kws only if they won't be used as i… (#10455)
Browse files Browse the repository at this point in the history
We can do it by tokenizing escaped keywords as normal identifiers, so that they won't
be accidentally used as real keywords.
  • Loading branch information
nicolo-ribaudo committed Sep 17, 2019
1 parent 45a484f commit cb881e6
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 447 deletions.
5 changes: 5 additions & 0 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -2066,6 +2066,11 @@ export default class ExpressionParser extends LValParser {

if (this.match(tt.name)) {
name = this.state.value;

// An escaped identifier whose value is the same as a keyword
if (!liberal && this.state.containsEsc && isKeyword(name)) {
this.raise(this.state.pos, `Escape sequence in keyword ${name}`);
}
} else if (this.state.type.keyword) {
name = this.state.type.keyword;

Expand Down
6 changes: 1 addition & 5 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -1326,11 +1326,7 @@ export default class Tokenizer extends LocationParser {

readWord(): void {
const word = this.readWord1();
const type = keywordTypes.get(word) || tt.name;

if (type.keyword && this.state.containsEsc) {
this.raise(this.state.pos, `Escape sequence in keyword ${word}`);
}
const type = (!this.state.containsEsc && keywordTypes.get(word)) || tt.name;

// Allow @@iterator and @@asyncIterator as a identifier only inside type
if (
Expand Down
@@ -0,0 +1,3 @@
var a = {
br\u{65}ak
};
@@ -0,0 +1,3 @@
{
"throws": "Unexpected keyword 'break' (2:2)"
}
@@ -0,0 +1,3 @@
var a = {
br\u{65}ak: 2
};
@@ -0,0 +1,157 @@
{
"type": "File",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 2
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 2
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
},
"identifierName": "a"
},
"name": "a"
},
"init": {
"type": "ObjectExpression",
"start": 8,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 3,
"column": 1
}
},
"properties": [
{
"type": "ObjectProperty",
"start": 12,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 15
}
},
"method": false,
"key": {
"type": "Identifier",
"start": 12,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 12
},
"identifierName": "break"
},
"name": "break"
},
"computed": false,
"shorthand": false,
"value": {
"type": "NumericLiteral",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 15
}
},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
}
}
]
}
}
],
"kind": "var"
}
],
"directives": []
}
}

0 comments on commit cb881e6

Please sign in to comment.