Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: TopLevelAwait should respect await identifiers defined in sub scope. #10947

Merged
merged 1 commit into from Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -39,6 +39,7 @@ import {
SCOPE_DIRECT_SUPER,
SCOPE_SUPER,
SCOPE_PROGRAM,
SCOPE_ASYNC,
} from "../util/scopeflags";

export default class ExpressionParser extends LValParser {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since at this point this.scope.inFunction is false, doesn't this.inModule already imply this.scope.inAsync?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. For example

class C {
  p = await 0;
}

Even top level await is enabled, await cant not be allowed here because it is not allowed in field initializers.

I think the idea of top level await is that the whole module is wrapped in an async function, which is what I do here to assign SCOPE_ASYNC to the program scope. Then we will need to handle cases where await is not allowed.

}
return false;
}

Expand Down
8 changes: 6 additions & 2 deletions packages/babel-parser/src/parser/index.js
Expand Up @@ -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 }>;
Expand Down Expand Up @@ -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();
Expand Down
@@ -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)"
}
@@ -0,0 +1,3 @@
namespace N {
const x = await 42;
}
@@ -0,0 +1,8 @@
{
"sourceType": "module",
"plugins": [
"typescript",
"topLevelAwait"
],
"throws": "Unexpected token, expected \";\" (2:20)"
}