Skip to content

Commit

Permalink
fix: add SCOPE_ASYNC to program scope when topLevelAwait is enable
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Dec 31, 2019
1 parent 26eb891 commit 0c66c47
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
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;
}
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)"
}

0 comments on commit 0c66c47

Please sign in to comment.