Enable top-level await parsing by default (#13387)

This commit is contained in:
Nicolò Ribaudo
2021-08-04 00:00:04 +02:00
committed by GitHub
parent b707842dd0
commit ddaf0d4296
89 changed files with 101 additions and 224 deletions

View File

@@ -39,14 +39,12 @@ import {
SCOPE_DIRECT_SUPER,
SCOPE_FUNCTION,
SCOPE_SUPER,
SCOPE_PROGRAM,
} from "../util/scopeflags";
import { ExpressionErrors } from "./util";
import {
PARAM_AWAIT,
PARAM_IN,
PARAM_RETURN,
PARAM,
functionFlags,
} from "../util/production-parameter";
import {
@@ -152,12 +150,7 @@ export default class ExpressionParser extends LValParser {
// Convenience method to parse an Expression only
getExpression(): N.Expression & N.ParserOutput {
let paramFlags = PARAM;
if (this.hasPlugin("topLevelAwait") && this.inModule) {
paramFlags |= PARAM_AWAIT;
}
this.scope.enter(SCOPE_PROGRAM);
this.prodParam.enter(paramFlags);
this.enterInitialScopes();
this.nextToken();
const expr = this.parseExpression();
if (!this.match(tt.eof)) {
@@ -615,12 +608,7 @@ export default class ExpressionParser extends LValParser {
? this.state.type.startsExpr
: this.state.type.startsExpr && !this.match(tt.modulo);
if (startsExpr && !this.isAmbiguousAwait()) {
this.raiseOverwrite(
startPos,
this.hasPlugin("topLevelAwait")
? Errors.AwaitNotInAsyncContext
: Errors.AwaitNotInAsyncFunction,
);
this.raiseOverwrite(startPos, Errors.AwaitNotInAsyncContext);
return this.parseAwait(startPos, startLoc);
}
}

View File

@@ -395,7 +395,7 @@ export default class UtilParser extends Tokenizer {
enterInitialScopes() {
let paramFlags = PARAM;
if (this.hasPlugin("topLevelAwait") && this.inModule) {
if (this.inModule) {
paramFlags |= PARAM_AWAIT;
}
this.scope.enter(SCOPE_PROGRAM);

View File

@@ -1,3 +0,0 @@
{
"plugins": ["topLevelAwait"]
}

View File

@@ -1,3 +1,4 @@
{
"sourceType": "module"
"sourceType": "module",
"throws": "Unexpected token (1:6)"
}

View File

@@ -1,40 +0,0 @@
{
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"errors": [
"SyntaxError: Unexpected reserved word 'await'. (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"expression": {
"type": "AssignmentExpression",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"operator": "=",
"left": {
"type": "Identifier",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5},"identifierName":"await"},
"name": "await"
},
"right": {
"type": "CallExpression",
"start":8,"end":13,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":13}},
"callee": {
"type": "Identifier",
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11},"identifierName":"foo"},
"name": "foo"
},
"arguments": []
}
}
}
],
"directives": []
}
}

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: Unexpected reserved word 'await'. (1:6)"
"SyntaxError: Can not use 'await' as identifier inside an async function. (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: Unexpected reserved word 'await'. (1:8)"
"SyntaxError: Can not use 'await' as identifier inside an async function. (1:8)"
],
"program": {
"type": "Program",

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: Unexpected reserved word 'await'. (1:9)"
"SyntaxError: Can not use 'await' as identifier inside an async function. (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: Unexpected reserved word 'await'. (1:6)"
"SyntaxError: Can not use 'await' as identifier inside an async function. (1:6)"
],
"program": {
"type": "Program",

View File

@@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":40}},
"errors": [
"SyntaxError: 'await' is only allowed within async functions. (1:24)"
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (1:24)"
],
"program": {
"type": "Program",

View File

@@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: 'await' is only allowed within async functions. (2:9)"
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (2:9)"
],
"program": {
"type": "Program",

View File

@@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"errors": [
"SyntaxError: 'await' is only allowed within async functions. (1:8)"
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (1:8)"
],
"program": {
"type": "Program",

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: 'await' is only allowed within async functions. (1:11)",
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (1:11)",
"SyntaxError: 'await' is not allowed in async function parameters. (1:11)"
],
"program": {

View File

@@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: 'await' is only allowed within async functions. (2:19)",
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (2:19)",
"SyntaxError: 'await' is not allowed in async function parameters. (2:19)"
],
"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: 'await' is only allowed within async functions. (2:2)"
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (2:2)"
],
"program": {
"type": "Program",

View File

@@ -3,7 +3,7 @@
"start":0,"end":50,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":50}},
"errors": [
"SyntaxError: Missing semicolon. (1:25)",
"SyntaxError: 'await' is only allowed within async functions. (1:41)"
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (1:41)"
],
"program": {
"type": "Program",

View File

@@ -3,7 +3,7 @@
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"errors": [
"SyntaxError: Missing semicolon. (1:10)",
"SyntaxError: 'await' is only allowed within async functions. (1:18)"
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (1:18)"
],
"program": {
"type": "Program",

View File

@@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: 'await' is only allowed within async functions. (2:6)"
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (2:6)"
],
"program": {
"type": "Program",

View File

@@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \"(\" (1:4)"
}

View File

@@ -0,0 +1,3 @@
{
"sourceType": "script"
}

View File

@@ -0,0 +1,3 @@
{
"allowAwaitOutsideFunction": true
}

View File

@@ -0,0 +1,3 @@
{
"plugins": ["v8intrinsic"]
}

View File

@@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":95,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}},
"errors": [
"SyntaxError: 'await' is only allowed within async functions. (5:6)"
"SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (5:6)"
],
"program": {
"type": "Program",

View File

@@ -1,4 +1,5 @@
{
"sourceType": "script",
"plugins": ["moduleBlocks"]
"plugins": ["moduleBlocks"],
"throws": "Unexpected token (1:14)"
}

View File

@@ -1,55 +0,0 @@
{
"type": "File",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"errors": [
"SyntaxError: Unexpected reserved word 'await'. (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"expression": {
"type": "ModuleExpression",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"body": {
"type": "Program",
"start":9,"end":19,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":19}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "LabeledStatement",
"start":9,"end":17,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":17}},
"body": {
"type": "ExpressionStatement",
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}},
"expression": {
"type": "NumericLiteral",
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}},
"extra": {
"rawValue": 3,
"raw": "3"
},
"value": 3
}
},
"label": {
"type": "Identifier",
"start":9,"end":14,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":14},"identifierName":"await"},
"name": "await"
}
}
],
"directives": []
}
}
}
],
"directives": []
}
}

View File

@@ -1,4 +0,0 @@
{
"sourceType": "module",
"plugins": ["moduleBlocks", "topLevelAwait"]
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "module"
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "module"
}

View File

@@ -1,5 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "script",
"throws": "Unexpected token, expected \"(\" (1:4)"
}

View File

@@ -1,6 +0,0 @@
{
"plugins": [
"topLevelAwait"
],
"sourceType": "module"
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "module"
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwati"],
"sourceType": "module"
}

View File

@@ -1,6 +0,0 @@
{
"plugins": [
"topLevelAwait"
],
"sourceType": "module"
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "module"
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "module"
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "script"
}

View File

@@ -1,5 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "unambiguous",
"allowAwaitOutsideFunction": true
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "unambiguous"
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwait", "v8intrinsic"],
"sourceType": "unambiguous"
}

View File

@@ -1,4 +0,0 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "unambiguous"
}