Fix: TopLevelAwait should respect await identifiers defined in… (#10947)
This commit is contained in:
parent
e5048053aa
commit
6ee8c97e6a
@ -39,6 +39,7 @@ import {
|
||||
SCOPE_DIRECT_SUPER,
|
||||
SCOPE_SUPER,
|
||||
SCOPE_PROGRAM,
|
||||
SCOPE_ASYNC,
|
||||
} from "../util/scopeflags";
|
||||
|
||||
export default class ExpressionParser extends LValParser {
|
||||
@ -96,7 +97,11 @@ export default class ExpressionParser extends LValParser {
|
||||
|
||||
// Convenience method to parse an Expression only
|
||||
getExpression(): N.Expression {
|
||||
this.scope.enter(SCOPE_PROGRAM);
|
||||
let scopeFlags = SCOPE_PROGRAM;
|
||||
if (this.hasPlugin("topLevelAwait") && this.inModule) {
|
||||
scopeFlags |= SCOPE_ASYNC;
|
||||
}
|
||||
this.scope.enter(scopeFlags);
|
||||
this.nextToken();
|
||||
const expr = this.parseExpression();
|
||||
if (!this.match(tt.eof)) {
|
||||
@ -2186,7 +2191,9 @@ export default class ExpressionParser extends LValParser {
|
||||
isAwaitAllowed(): boolean {
|
||||
if (this.scope.inFunction) return this.scope.inAsync;
|
||||
if (this.options.allowAwaitOutsideFunction) return true;
|
||||
if (this.hasPlugin("topLevelAwait")) return this.inModule;
|
||||
if (this.hasPlugin("topLevelAwait")) {
|
||||
return this.inModule && this.scope.inAsync;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ import type { File, JSXOpeningElement } from "../types";
|
||||
import type { PluginList } from "../plugin-utils";
|
||||
import { getOptions } from "../options";
|
||||
import StatementParser from "./statement";
|
||||
import { SCOPE_PROGRAM } from "../util/scopeflags";
|
||||
import { SCOPE_ASYNC, SCOPE_PROGRAM } from "../util/scopeflags";
|
||||
import ScopeHandler from "../util/scope";
|
||||
|
||||
export type PluginsMap = Map<string, { [string]: any }>;
|
||||
@ -35,7 +35,11 @@ export default class Parser extends StatementParser {
|
||||
}
|
||||
|
||||
parse(): File {
|
||||
this.scope.enter(SCOPE_PROGRAM);
|
||||
let scopeFlags = SCOPE_PROGRAM;
|
||||
if (this.hasPlugin("topLevelAwait") && this.inModule) {
|
||||
scopeFlags |= SCOPE_ASYNC;
|
||||
}
|
||||
this.scope.enter(scopeFlags);
|
||||
const file = this.startNode();
|
||||
const program = this.startNode();
|
||||
this.nextToken();
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
export class C {
|
||||
p = await 0;
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
{
|
||||
"plugins": [
|
||||
"topLevelAwait",
|
||||
"classProperties"
|
||||
],
|
||||
"sourceType": "module",
|
||||
"throws": "Unexpected token, expected \";\" (2:12)"
|
||||
}
|
||||
3
packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/input.ts
vendored
Normal file
3
packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/input.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
namespace N {
|
||||
const x = await 42;
|
||||
}
|
||||
8
packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/options.json
vendored
Normal file
8
packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/options.json
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": [
|
||||
"typescript",
|
||||
"topLevelAwait"
|
||||
],
|
||||
"throws": "Unexpected token, expected \";\" (2:20)"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user