Polish parser errors (#12258)

* polish: update AwaitNotInAsyncFunction message

* polish: update YieldInParameter error message

* Apply suggestions from code review

Co-authored-by: Brian Ng <bng412@gmail.com>

* update test fixtures

Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
Huáng Jùnliàng 2020-10-26 20:55:04 -04:00 committed by GitHub
parent 2782a549e9
commit 5b48f40a08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 56 additions and 46 deletions

View File

@ -16,8 +16,9 @@ export const ErrorMessages = Object.freeze({
"Can not use 'await' as identifier inside an async function",
AwaitExpressionFormalParameter:
"await is not allowed in async function parameters",
AwaitNotInAsyncFunction:
"Can not use keyword 'await' outside an async function",
AwaitNotInAsyncContext:
"'await' is only allowed within async functions and at the top levels of modules",
AwaitNotInAsyncFunction: "'await' is only allowed within async functions",
BadGetterArity: "getter must not have any formal parameters",
BadSetterArity: "setter must have exactly one formal parameter",
BadSetterRestParameter:
@ -210,7 +211,7 @@ export const ErrorMessages = Object.freeze({
VarRedeclaration: "Identifier '%0' has already been declared",
YieldBindingIdentifier:
"Can not use 'yield' as identifier inside a generator",
YieldInParameter: "yield is not allowed in generator parameters",
YieldInParameter: "Yield expression is not allowed in formal parameters",
ZeroDigitNumericSeparator:
"Numeric separator can not be used after leading 0",
});

View File

@ -2343,7 +2343,12 @@ export default class ExpressionParser extends LValParser {
if (reservedTest(word, this.inModule)) {
if (!this.prodParam.hasAwait && word === "await") {
this.raise(startLoc, Errors.AwaitNotInAsyncFunction);
this.raise(
startLoc,
this.hasPlugin("topLevelAwait")
? Errors.AwaitNotInAsyncContext
: Errors.AwaitNotInAsyncFunction,
);
} else {
this.raise(startLoc, Errors.UnexpectedReservedWord, word);
}

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"errors": [
"SyntaxError: Can not use keyword 'await' outside an async function (1:0)"
"SyntaxError: 'await' is only allowed within async functions (1:0)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"errors": [
"SyntaxError: Can not use keyword 'await' outside an async function (1:6)"
"SyntaxError: 'await' is only allowed within async functions (1:6)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}},
"errors": [
"SyntaxError: Can not use keyword 'await' outside an async function (1:8)"
"SyntaxError: 'await' is only allowed within async functions (1:8)"
],
"program": {
"type": "Program",
@ -24,6 +24,9 @@
{
"type": "ObjectProperty",
"start":8,"end":13,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":13}},
"extra": {
"shorthand": true
},
"method": false,
"key": {
"type": "Identifier",
@ -36,9 +39,6 @@
"type": "Identifier",
"start":8,"end":13,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":13},"identifierName":"await"},
"name": "await"
},
"extra": {
"shorthand": true
}
}
]

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"errors": [
"SyntaxError: Can not use keyword 'await' outside an async function (1:15)"
"SyntaxError: 'await' is only allowed within async functions (1:15)"
],
"program": {
"type": "Program",
@ -28,6 +28,9 @@
{
"type": "ObjectProperty",
"start":15,"end":20,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":20}},
"extra": {
"shorthand": true
},
"method": false,
"key": {
"type": "Identifier",
@ -40,9 +43,6 @@
"type": "Identifier",
"start":15,"end":20,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":20},"identifierName":"await"},
"name": "await"
},
"extra": {
"shorthand": true
}
}
]

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"errors": [
"SyntaxError: Can not use keyword 'await' outside an async function (1:9)"
"SyntaxError: 'await' is only allowed within async functions (1:9)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"errors": [
"SyntaxError: Can not use keyword 'await' outside an async function (1:6)"
"SyntaxError: 'await' is only allowed within async functions (1:6)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (2:7)"
"SyntaxError: Yield expression is not allowed in formal parameters (2:7)"
],
"program": {
"type": "Program",
@ -66,4 +66,4 @@
],
"directives": []
}
}
}

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (2:15)"
"SyntaxError: Yield expression is not allowed in formal parameters (2:15)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (2:7)"
"SyntaxError: Yield expression is not allowed in formal parameters (2:7)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (2:17)"
"SyntaxError: Yield expression is not allowed in formal parameters (2:17)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (2:8)",
"SyntaxError: Yield expression is not allowed in formal parameters (2:8)",
"SyntaxError: Binding invalid left-hand side in function parameter list (2:8)"
],
"program": {

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":41,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (2:8)"
"SyntaxError: Yield expression is not allowed in formal parameters (2:8)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (1:15)"
"SyntaxError: Yield expression is not allowed in formal parameters (1:15)"
],
"program": {
"type": "Program",
@ -16,6 +16,10 @@
"expression": {
"type": "ObjectExpression",
"start":1,"end":26,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":26}},
"extra": {
"parenthesized": true,
"parenStart": 0
},
"properties": [
{
"type": "ObjectMethod",
@ -55,11 +59,7 @@
"directives": []
}
}
],
"extra": {
"parenthesized": true,
"parenStart": 0
}
]
}
}
],

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (1:17)"
"SyntaxError: Yield expression is not allowed in formal parameters (1:17)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (2:3)",
"SyntaxError: Yield expression is not allowed in formal parameters (2:3)",
"SyntaxError: Binding invalid left-hand side in function parameter list (2:3)"
],
"program": {

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (2:3)",
"SyntaxError: Yield expression is not allowed in formal parameters (2:3)",
"SyntaxError: Binding invalid left-hand side in function parameter list (2:3)"
],
"program": {

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":41,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (2:9)",
"SyntaxError: Yield expression is not allowed in formal parameters (2:9)",
"SyntaxError: Binding invalid left-hand side in function parameter list (2:9)"
],
"program": {

View File

@ -2,8 +2,8 @@
"type": "File",
"start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":33}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (1:17)",
"SyntaxError: yield is not allowed in generator parameters (1:24)"
"SyntaxError: Yield expression is not allowed in formal parameters (1:17)",
"SyntaxError: Yield expression is not allowed in formal parameters (1:24)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":38}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (1:21)"
"SyntaxError: Yield expression is not allowed in formal parameters (1:21)"
],
"program": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (1:16)",
"SyntaxError: Yield expression is not allowed in formal parameters (1:16)",
"SyntaxError: Binding invalid left-hand side in function parameter list (1:16)"
],
"program": {

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":39}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (1:25)",
"SyntaxError: Yield expression is not allowed in formal parameters (1:25)",
"SyntaxError: Binding invalid left-hand side in function parameter list (1:25)"
],
"program": {

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":35}},
"errors": [
"SyntaxError: yield is not allowed in generator parameters (1:21)"
"SyntaxError: Yield expression is not allowed in formal parameters (1:21)"
],
"program": {
"type": "Program",

View File

@ -1,6 +1,8 @@
{
"plugins": ["topLevelAwait"],
"plugins": [
"topLevelAwait"
],
"sourceType": "module",
"errorRecovery": false,
"throws": "Can not use keyword 'await' outside an async function (1:6)"
}
"throws": "'await' is only allowed within async functions and at the top levels of modules (1:6)"
}

View File

@ -1,6 +1,8 @@
{
"plugins": ["topLevelAwait"],
"plugins": [
"topLevelAwait"
],
"sourceType": "module",
"errorRecovery": false,
"throws": "Can not use keyword 'await' outside an async function (2:2)"
}
"throws": "'await' is only allowed within async functions and at the top levels of modules (2:2)"
}

View File

@ -6,5 +6,5 @@
"supportsTopLevelAwait": false
},
"presets": ["env"],
"throws": "Can not use keyword 'await' outside an async function (1:0)"
"throws": "'await' is only allowed within async functions (1:0)"
}