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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
89 changed files with 101 additions and 224 deletions

View File

@ -1,6 +1,6 @@
FLOW_COMMIT = a1f9a4c709dcebb27a5084acf47755fbae699c25
FLOW_COMMIT = 92bbb5e9dacb8185aa73ea343954d0434b42c40b
TEST262_COMMIT = f9efc461420e3729a8fe24ac2472fad9d6357a70
TYPESCRIPT_COMMIT = 3de706a8525c2ded782fc032fa4afe2e485100d3
TYPESCRIPT_COMMIT = e34b2adcaed2ff12761f7cbf87a299f5082c4e63
# Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967
export FORCE_COLOR = true
@ -134,7 +134,7 @@ test-ci-coverage:
bootstrap-flow:
rm -rf build/flow
mkdir -p build
git clone --single-branch --shallow-since=2018-11-01 https://github.com/facebook/flow.git build/flow
git clone --single-branch --shallow-since=2021-05-01 https://github.com/facebook/flow.git build/flow
cd build/flow && git checkout -q $(FLOW_COMMIT)
test-flow:
@ -146,7 +146,7 @@ test-flow-update-allowlist:
bootstrap-typescript:
rm -rf ./build/typescript
mkdir -p ./build
git clone --single-branch --shallow-since=2019-09-01 https://github.com/microsoft/TypeScript.git ./build/typescript
git clone --single-branch --shallow-since=2021-05-01 https://github.com/microsoft/TypeScript.git ./build/typescript
cd build/typescript && git checkout -q $(TYPESCRIPT_COMMIT)
test-typescript:
@ -158,7 +158,7 @@ test-typescript-update-allowlist:
bootstrap-test262:
rm -rf build/test262
mkdir -p build
git clone --single-branch --shallow-since=2019-12-01 https://github.com/tc39/test262.git build/test262
git clone --single-branch --shallow-since=2021-05-01 https://github.com/tc39/test262.git build/test262
cd build/test262 && git checkout -q $(TEST262_COMMIT)
test-test262:

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"
}

View File

@ -1,9 +0,0 @@
{
"caller": {
"name": "test-fixture",
"supportsStaticESM": true,
"supportsDynamicImport": true,
"supportsTopLevelAwait": true
},
"presets": ["env"]
}

View File

@ -1,10 +0,0 @@
{
"caller": {
"name": "test-fixture",
"supportsStaticESM": true,
"supportsDynamicImport": true,
"supportsTopLevelAwait": false
},
"presets": ["env"],
"throws": "Unexpected reserved word 'await'. (1:0)"
}

View File

@ -0,0 +1,42 @@
import env from "..";
import * as babel from "@babel/core";
describe("supportsTopLevelAwait enables the parser plugin for old parser versions", () => {
function extractParserOptions(api, { ref }) {
return {
manipulateOptions(opts, parserOpts) {
ref.parserOpts = parserOpts;
},
visitor: {},
};
}
it("supported", () => {
const ref = {};
babel.transformSync("", {
configFile: false,
presets: [env],
plugins: [[extractParserOptions, { ref }]],
caller: {
name: "test",
supportsTopLevelAwait: true,
},
});
expect(ref.parserOpts.plugins).toContain("topLevelAwait");
});
it("unsupported", () => {
const ref = {};
babel.transformSync("", {
configFile: false,
presets: [env],
plugins: [[extractParserOptions, { ref }]],
caller: {
name: "test",
},
});
expect(ref.parserOpts.plugins).not.toContain("topLevelAwait");
});
});

View File

@ -5,6 +5,7 @@ JSX/invalid_unpaired_rcurly.js
JSX_invalid/migrated_0000.js
arrow_function/object_return_type.js
arrow_function_invalid/migrated_0002.js
async_arrow_functions/with_type_parameters_types_disabled.js
async_await/migrated_0020.js
async_await/migrated_0024.js
async_await/migrated_0027.js
@ -14,9 +15,23 @@ class_properties/migrated_0003.js
class_properties/migrated_0008.js
class_properties/migrated_0021.js
class_properties/migrated_0026.js
comment_interning/class_method.js
comment_interning/class_property.js
comment_interning/declare_export_declaration.js
comment_interning/declare_interface.js
comment_interning/declare_type_alias.js
comment_interning/decorator.js
comment_interning/function_declaration.js
comment_interning/import.js
comment_interning/interface.js
comment_interning/object_type.js
comment_interning/remove_type_trailing_comments.js
comment_interning/super.js
comment_interning/type_alias.js
decorators/migrated_0003.js
export_import_reserved_words/migrated_0003.js
export_statements/export_trailing_comma.js
for_await_loops/migrated_0000.js
nullish_coalescing/missing-plugin.js
optional_chaining/missing-plugin.js
types/member/reserved_words.js

View File

@ -135,6 +135,7 @@ const ignoredFeatures = [
"Symbol.unscopables",
"tail-call-optimization",
"template",
"top-level-await",
"TypedArray",
"TypedArray.prototype.at",
"TypedArray.prototype.item",
@ -153,7 +154,6 @@ const ignoredTests = ["built-ins/RegExp/", "language/literals/regexp/"];
const featuresToPlugins = {
"class-static-block": "classStaticBlock",
"top-level-await": "topLevelAwait",
"import-assertions": "importAssertions",
};

View File

@ -109,6 +109,7 @@ declarationEmitInterfaceWithNonEntityNameExpressionHeritage.ts
declarationEmitMixinPrivateProtected.ts
declarationEmitPrefersPathKindBasedOnBundling.ts
declarationEmitPrefersPathKindBasedOnBundling2.ts
declarationEmitPrivatePromiseLikeInterface.ts
declarationEmitPrivateSymbolCausesVarDeclarationEmit2.ts
declarationEmitReadonlyComputedProperty.ts
declarationEmitStringEnumUsedInNonlocalSpread.ts
@ -221,6 +222,7 @@ exportSpecifierAndLocalMemberDeclaration.ts
exportStarFromEmptyModule.ts
exportStarNotElided.ts
expressionsForbiddenInParameterInitializers.ts
extendedUnicodePlaneIdentifiers.ts
extendingClassFromAliasAndUsageInIndexer.ts
extendsClauseAlreadySeen.ts
extendsClauseAlreadySeen2.ts