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

Expose wether a module has TLA or not as .extra.async #16480

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 4 additions & 1 deletion packages/babel-core/test/fixtures/parse/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@
}
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
},
"comments": []
}
18 changes: 12 additions & 6 deletions packages/babel-parser/src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ export default abstract class ExpressionParser extends LValParser {
const startLoc = this.state.startLoc;
const isAwait = this.isContextual(tt._await);

if (isAwait && this.isAwaitAllowed()) {
if (isAwait && this.recordAwaitIfAllowed()) {
this.next();
const expr = this.parseAwait(startLoc);
if (!sawUnary) this.checkExponentialAfterUnary(expr);
Expand Down Expand Up @@ -2869,12 +2869,18 @@ export default abstract class ExpressionParser extends LValParser {
}
}

isAwaitAllowed(): boolean {
if (this.prodParam.hasAwait) return true;
if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
return true;
// Returns wether `await` is allowed or not in this context, and if it is
// keeps track of it to determine whether a module uses top-level await.
recordAwaitIfAllowed(): boolean {
const isAwaitAllowed =
this.prodParam.hasAwait ||
(this.options.allowAwaitOutsideFunction && !this.scope.inFunction);

if (isAwaitAllowed && !this.scope.inFunction) {
this.state.hasTopLevelAwait = true;
}
return false;

return isAwaitAllowed;
}

// Parses await expression inside async function.
Expand Down
25 changes: 14 additions & 11 deletions packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,16 @@ export default abstract class StatementParser extends ExpressionParser {
program.sourceType = sourceType;
program.interpreter = this.parseInterpreterDirective();
this.parseBlockBody(program, true, true, end);
if (
this.inModule &&
!this.options.allowUndeclaredExports &&
this.scope.undefinedExports.size > 0
) {
for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
this.raise(Errors.ModuleExportUndefined, at, { localName });
if (this.inModule) {
if (
!this.options.allowUndeclaredExports &&
this.scope.undefinedExports.size > 0
) {
for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
this.raise(Errors.ModuleExportUndefined, at, { localName });
}
}
this.addExtra(program, "topLevelAwait", this.state.hasTopLevelAwait);
}
let finishedProgram: N.Program;
if (end === tt.eof) {
Expand Down Expand Up @@ -490,7 +492,7 @@ export default abstract class StatementParser extends ExpressionParser {
case tt._await:
// [+Await] await [no LineTerminator here] using [no LineTerminator here] BindingList[+Using]
if (!this.state.containsEsc && this.startsAwaitUsing()) {
if (!this.isAwaitAllowed()) {
if (!this.recordAwaitIfAllowed()) {
this.raise(Errors.AwaitUsingNotInAsyncContext, node);
} else if (!allowDeclaration) {
this.raise(Errors.UnexpectedLexicalDeclaration, node);
Expand Down Expand Up @@ -912,8 +914,9 @@ export default abstract class StatementParser extends ExpressionParser {

let awaitAt = null;

if (this.isAwaitAllowed() && this.eatContextual(tt._await)) {
awaitAt = this.state.lastTokStartLoc;
if (this.isContextual(tt._await) && this.recordAwaitIfAllowed()) {
awaitAt = this.state.startLoc;
this.next();
}
this.scope.enter(ScopeFlag.OTHER);
this.expect(tt.parenL);
Expand Down Expand Up @@ -941,7 +944,7 @@ export default abstract class StatementParser extends ExpressionParser {
let kind;
if (startsWithAwaitUsing) {
kind = "await using";
if (!this.isAwaitAllowed()) {
if (!this.recordAwaitIfAllowed()) {
this.raise(Errors.AwaitUsingNotInAsyncContext, this.state.startLoc);
}
this.next(); // eat 'await'
Expand Down
14 changes: 13 additions & 1 deletion packages/babel-parser/src/tokenizer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const enum StateFlags {
inFSharpPipelineDirectBody = 1 << 9,
canStartJSXElement = 1 << 10,
containsEsc = 1 << 11,
hasTopLevelAwait = 1 << 12,
}

export const enum LoopLabelKind {
Expand All @@ -45,7 +46,7 @@ export const enum LoopLabelKind {
}

export default class State {
flags: number = StateFlags.canStartJSXElement;
flags: StateFlags = StateFlags.canStartJSXElement;

get strict(): boolean {
return (this.flags & StateFlags.Strict) > 0;
Expand Down Expand Up @@ -261,6 +262,17 @@ export default class State {
// that must be reported if the template is not tagged.
firstInvalidTemplateEscapePos: null | Position = null;

get hasTopLevelAwait(): boolean {
return (this.flags & StateFlags.hasTopLevelAwait) > 0;
}
set hasTopLevelAwait(value: boolean) {
if (value) {
this.flags |= StateFlags.hasTopLevelAwait;
} else {
this.flags &= ~StateFlags.hasTopLevelAwait;
}
}

// This property is used to track the following errors
// - StrictNumericEscape
// - StrictOctalLiteral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@
]
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
},
"comments": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@
]
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
},
"comments": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@
]
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
},
"comments": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@
]
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
},
"comments": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
}
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@
]
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
},
"comments": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"declaration": null
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
}
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
"directives": []
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
}
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"declaration": null
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
}
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
"declaration": null
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"declaration": null
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"declaration": null
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"declaration": null
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
}
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
"alternate": null
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
"declaration": null
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"declaration": null
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"kind": "var"
}
],
"directives": []
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}