[parser] Report escapes in kws only if they won't be used as i… (#10455)

We can do it by tokenizing escaped keywords as normal identifiers, so that they won't
be accidentally used as real keywords.
This commit is contained in:
Nicolò Ribaudo
2019-09-18 00:13:51 +02:00
committed by GitHub
parent 45a484f0a2
commit cb881e6b3f
9 changed files with 172 additions and 447 deletions

View File

@@ -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;

View File

@@ -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 (

View File

@@ -0,0 +1,3 @@
var a = {
br\u{65}ak
};

View File

@@ -0,0 +1,3 @@
{
"throws": "Unexpected keyword 'break' (2:2)"
}

View File

@@ -0,0 +1,3 @@
var a = {
br\u{65}ak: 2
};

View File

@@ -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": []
}
}