Skip to content

Fix top-level await parsing (#38483) #38518

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4150,8 +4150,15 @@ namespace ts {
return true;
}

// This handles cases like `await (async () => {})()`
// One drawback of this implementation is that we cannot use this form in top-level
// if there is an "await" identifier as we cannot differentiate this two cases
const isCallLike = () => {
return (nextToken() === SyntaxKind.OpenParenToken && !identifiers.has("await")) && !scanner.hasPrecedingLineBreak();
Copy link
Member

Choose a reason for hiding this comment

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

The problem with this approach is function hoisting. Your example has this:

function await(fn) {
  return () => {};
}
await (async () => {})(); // equivalent to: await((async () => {})())

However, it should also fail in this case:

await (async () => {})(); // equivalent to: await((async () => {})())
function await(fn) {
  return () => {};
}

I don't think this is the correct solution to the underlying problem.

};

// here we are using similar heuristics as 'isYieldExpression'
return lookAhead(nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine);
return lookAhead(isCallLike) || lookAhead(nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
tests/cases/conformance/externalModules/topLevelAwait.ts(2,1): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
tests/cases/conformance/externalModules/topLevelAwait.ts(4,1): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.


==== tests/cases/conformance/externalModules/topLevelAwait.ts (1 errors) ====
==== tests/cases/conformance/externalModules/topLevelAwait.ts (2 errors) ====
export const x = 1;
await x;
~~~~~
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.


await (async () => {})();
~~~~~
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
//// [topLevelAwait.ts]
export const x = 1;
await x;


await (async () => {})();

//// [topLevelAwait.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
export const x = 1;
await x;
await (() => __awaiter(void 0, void 0, void 0, function* () { }))();
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const x = 1;
await x;
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

await (async () => {})();
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ await x;
>await x : 1
>x : 1

await (async () => {})();
>await (async () => {})() : void
>(async () => {})() : Promise<void>
>(async () => {}) : () => Promise<void>
>async () => {} : () => Promise<void>

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//// [topLevelAwait.ts]
export const x = 1;
await x;


await (async () => {})();

//// [topLevelAwait.js]
export const x = 1;
await x;
await (async () => { })();
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const x = 1;
await x;
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

await (async () => {})();
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ await x;
>await x : 1
>x : 1

await (async () => {})();
>await (async () => {})() : void
>(async () => {})() : Promise<void>
>(async () => {}) : () => Promise<void>
>async () => {} : () => Promise<void>

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
tests/cases/conformance/externalModules/topLevelAwait.ts(2,1): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
tests/cases/conformance/externalModules/topLevelAwait.ts(4,1): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.


==== tests/cases/conformance/externalModules/topLevelAwait.ts (1 errors) ====
==== tests/cases/conformance/externalModules/topLevelAwait.ts (2 errors) ====
export const x = 1;
await x;
~~~~~
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.


await (async () => {})();
~~~~~
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
//// [topLevelAwait.ts]
export const x = 1;
await x;


await (async () => {})();

//// [topLevelAwait.js]
System.register([], function (exports_1, context_1) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var x;
var __moduleName = context_1 && context_1.id;
return {
setters: [],
execute: async function () {
exports_1("x", x = 1);
await x;
await (() => __awaiter(void 0, void 0, void 0, function* () { }))();
}
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const x = 1;
await x;
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

await (async () => {})();
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ await x;
>await x : 1
>x : 1

await (async () => {})();
>await (async () => {})() : void
>(async () => {})() : Promise<void>
>(async () => {}) : () => Promise<void>
>async () => {} : () => Promise<void>

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//// [topLevelAwait.ts]
export const x = 1;
await x;


await (async () => {})();

//// [topLevelAwait.js]
System.register([], function (exports_1, context_1) {
Expand All @@ -13,6 +14,7 @@ System.register([], function (exports_1, context_1) {
execute: async function () {
exports_1("x", x = 1);
await x;
await (async () => { })();
}
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const x = 1;
await x;
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

await (async () => {})();
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ await x;
>await x : 1
>x : 1

await (async () => {})();
>await (async () => {})() : void
>(async () => {})() : Promise<void>
>(async () => {}) : () => Promise<void>
>async () => {} : () => Promise<void>

2 changes: 2 additions & 0 deletions tests/cases/conformance/externalModules/topLevelAwait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
// @module: esnext,system
export const x = 1;
await x;

await (async () => {})();