Skip to content

Commit 3d60293

Browse files
authored
Handle binding elements while looking for invalid await and yield (microsoft#19972)
* Handle omitting a node in addCustomPrologue, account for binding elemnts in isInParameterInitializerBeforeContainingFunction * Use append * Fix lint
1 parent 26ef7e5 commit 3d60293

7 files changed

Lines changed: 132 additions & 2 deletions

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13472,10 +13472,14 @@ namespace ts {
1347213472
}
1347313473

1347413474
function isInParameterInitializerBeforeContainingFunction(node: Node) {
13475+
let inBindingInitializer = false;
1347513476
while (node.parent && !isFunctionLike(node.parent)) {
13476-
if (node.parent.kind === SyntaxKind.Parameter && (<ParameterDeclaration>node.parent).initializer === node) {
13477+
if (isParameter(node.parent) && (inBindingInitializer || node.parent.initializer === node)) {
1347713478
return true;
1347813479
}
13480+
if (isBindingElement(node.parent) && node.parent.initializer === node) {
13481+
inBindingInitializer = true;
13482+
}
1347913483

1348013484
node = node.parent;
1348113485
}

src/compiler/factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3709,7 +3709,7 @@ namespace ts {
37093709
while (statementOffset < numStatements) {
37103710
const statement = source[statementOffset];
37113711
if (getEmitFlags(statement) & EmitFlags.CustomPrologue) {
3712-
target.push(visitor ? visitNode(statement, visitor, isStatement) : statement);
3712+
append(target, visitor ? visitNode(statement, visitor, isStatement) : statement);
37133713
}
37143714
else {
37153715
break;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/bar.ts(1,35): error TS2524: 'await' expressions cannot be used in a parameter initializer.
2+
tests/cases/compiler/bar.ts(4,31): error TS2523: 'yield' expressions cannot be used in a parameter initializer.
3+
4+
5+
==== tests/cases/compiler/bar.ts (2 errors) ====
6+
export async function foo({ foo = await import("./bar") }) {
7+
~~~~~~~~~~~~~~~~~~~~~
8+
!!! error TS2524: 'await' expressions cannot be used in a parameter initializer.
9+
}
10+
11+
export function* foo2({ foo = yield "a" }) {
12+
~~~~~~~~~
13+
!!! error TS2523: 'yield' expressions cannot be used in a parameter initializer.
14+
}
15+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//// [bar.ts]
2+
export async function foo({ foo = await import("./bar") }) {
3+
}
4+
5+
export function* foo2({ foo = yield "a" }) {
6+
}
7+
8+
9+
//// [bar.js]
10+
"use strict";
11+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12+
return new (P || (P = Promise))(function (resolve, reject) {
13+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
14+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
15+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
16+
step((generator = generator.apply(thisArg, _arguments || [])).next());
17+
});
18+
};
19+
var __generator = (this && this.__generator) || function (thisArg, body) {
20+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
21+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
22+
function verb(n) { return function (v) { return step([n, v]); }; }
23+
function step(op) {
24+
if (f) throw new TypeError("Generator is already executing.");
25+
while (_) try {
26+
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
27+
if (y = 0, t) op = [0, t.value];
28+
switch (op[0]) {
29+
case 0: case 1: t = op; break;
30+
case 4: _.label++; return { value: op[1], done: false };
31+
case 5: _.label++; y = op[1]; op = [0]; continue;
32+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
33+
default:
34+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
35+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
36+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
37+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
38+
if (t[2]) _.ops.pop();
39+
_.trys.pop(); continue;
40+
}
41+
op = body.call(thisArg, _);
42+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
43+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
44+
}
45+
};
46+
exports.__esModule = true;
47+
function foo(_a) {
48+
var _b = _a.foo, foo = _b === void 0 ? yield Promise.resolve().then(function () { return require("./bar"); }) : _b;
49+
return __awaiter(this, void 0, void 0, function () {
50+
return __generator(this, function (_c) {
51+
return [2 /*return*/];
52+
});
53+
});
54+
}
55+
exports.foo = foo;
56+
function foo2(_a) {
57+
var _b, foo, _c;
58+
return __generator(this, function (_d) {
59+
switch (_d.label) {
60+
case 0:
61+
_b = _a.foo;
62+
if (!(_b === void 0)) return [3 /*break*/, 2];
63+
return [4 /*yield*/, "a"];
64+
case 1:
65+
_c = _d.sent();
66+
return [3 /*break*/, 3];
67+
case 2:
68+
_c = _b;
69+
_d.label = 3;
70+
case 3:
71+
foo = _c;
72+
return [2 /*return*/];
73+
}
74+
});
75+
}
76+
exports.foo2 = foo2;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/bar.ts ===
2+
export async function foo({ foo = await import("./bar") }) {
3+
>foo : Symbol(foo, Decl(bar.ts, 0, 0))
4+
>foo : Symbol(foo, Decl(bar.ts, 0, 27))
5+
>"./bar" : Symbol("tests/cases/compiler/bar", Decl(bar.ts, 0, 0))
6+
}
7+
8+
export function* foo2({ foo = yield "a" }) {
9+
>foo2 : Symbol(foo2, Decl(bar.ts, 1, 1))
10+
>foo : Symbol(foo, Decl(bar.ts, 3, 23))
11+
}
12+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/bar.ts ===
2+
export async function foo({ foo = await import("./bar") }) {
3+
>foo : ({ foo }: { foo?: typeof "tests/cases/compiler/bar"; }) => Promise<void>
4+
>foo : typeof "tests/cases/compiler/bar"
5+
>await import("./bar") : typeof "tests/cases/compiler/bar"
6+
>import("./bar") : Promise<typeof "tests/cases/compiler/bar">
7+
>"./bar" : "./bar"
8+
}
9+
10+
export function* foo2({ foo = yield "a" }) {
11+
>foo2 : ({ foo }: { foo?: any; }) => IterableIterator<any>
12+
>foo : any
13+
>yield "a" : any
14+
>"a" : "a"
15+
}
16+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @lib: es6
2+
// @filename: bar.ts
3+
export async function foo({ foo = await import("./bar") }) {
4+
}
5+
6+
export function* foo2({ foo = yield "a" }) {
7+
}

0 commit comments

Comments
 (0)