Skip to content

Commit

Permalink
Expose wether a module has TLA or not as .extra.async (#16480)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored Jul 26, 2024
1 parent 814b31e commit 5dc3b44
Show file tree
Hide file tree
Showing 1,735 changed files with 7,220 additions and 1,900 deletions.
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 @@ -604,7 +604,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 @@ -2867,12 +2867,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 @@ -493,7 +495,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 @@ -915,8 +917,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 @@ -944,7 +947,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
2 changes: 2 additions & 0 deletions packages/babel-parser/src/tokenizer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ export default class State {
// that must be reported if the template is not tagged.
firstInvalidTemplateEscapePos: null | Position = null;

@bit accessor hasTopLevelAwait = false;

// 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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{
"type": "ImportDeclaration",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":31,"index":31}},
"importKind": "type",
"specifiers": [
{
"type": "ImportSpecifier",
Expand All @@ -27,7 +28,6 @@
}
}
],
"importKind": "type",
"source": {
"type": "StringLiteral",
"start":25,"end":30,"loc":{"start":{"line":1,"column":25,"index":25},"end":{"line":1,"column":30,"index":30}},
Expand All @@ -41,6 +41,7 @@
{
"type": "ExportNamedDeclaration",
"start":32,"end":52,"loc":{"start":{"line":2,"column":0,"index":32},"end":{"line":2,"column":20,"index":52}},
"exportKind": "type",
"specifiers": [
{
"type": "ExportSpecifier",
Expand All @@ -58,7 +59,6 @@
}
],
"source": null,
"exportKind": "type",
"declaration": null
},
{
Expand All @@ -76,6 +76,9 @@
}
}
],
"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
}
}
}
Loading

0 comments on commit 5dc3b44

Please sign in to comment.