Skip to content

Commit 788239f

Browse files
authored
do not report use-before-declaration error in async like IIFE initial… (#55279)
1 parent 4d7753a commit 788239f

6 files changed

+160
-2
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ import {
452452
isAssignmentOperator,
453453
isAssignmentPattern,
454454
isAssignmentTarget,
455-
isAsyncFunction,
456455
isAutoAccessorPropertyDeclaration,
457456
isAwaitExpression,
458457
isBinaryExpression,
@@ -3874,7 +3873,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
38743873
function isSameScopeDescendentOf(initial: Node, parent: Node | undefined, stopAt: Node): boolean {
38753874
return !!parent && !!findAncestor(initial, n =>
38763875
n === parent
3877-
|| (n === stopAt || isFunctionLike(n) && (!getImmediatelyInvokedFunctionExpression(n) || isAsyncFunction(n)) ? "quit" : false));
3876+
|| (n === stopAt || isFunctionLike(n) && (!getImmediatelyInvokedFunctionExpression(n) || (getFunctionFlags(n) & FunctionFlags.AsyncGenerator)) ? "quit" : false));
38783877
}
38793878

38803879
function getAnyImportSyntax(node: Node): AnyImportSyntax | undefined {

tests/baselines/reference/blockScopedVariablesUseBeforeDef.errors.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,19 @@ blockScopedVariablesUseBeforeDef.ts(122,22): error TS2448: Block-scoped variable
164164

165165
const foo = 1;
166166
}
167+
168+
// #30907
169+
function wrapI1() {
170+
const iter = (function* foo() {
171+
iter;
172+
yield 1;
173+
})();
174+
}
175+
176+
function wrapI2() {
177+
const iter = (async function* foo() {
178+
iter;
179+
yield 1;
180+
})();
181+
}
167182

tests/baselines/reference/blockScopedVariablesUseBeforeDef.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ function foo17() {
136136

137137
const foo = 1;
138138
}
139+
140+
// #30907
141+
function wrapI1() {
142+
const iter = (function* foo() {
143+
iter;
144+
yield 1;
145+
})();
146+
}
147+
148+
function wrapI2() {
149+
const iter = (async function* foo() {
150+
iter;
151+
yield 1;
152+
})();
153+
}
139154

140155

141156
//// [blockScopedVariablesUseBeforeDef.js]
@@ -179,6 +194,18 @@ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, p
179194
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
180195
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
181196
};
197+
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
198+
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
199+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
200+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
201+
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
202+
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
203+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
204+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
205+
function fulfill(value) { resume("next", value); }
206+
function reject(value) { resume("throw", value); }
207+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
208+
};
182209
function foo0() {
183210
var a = x;
184211
var x;
@@ -329,3 +356,35 @@ function foo17() {
329356
}); })();
330357
var foo = 1;
331358
}
359+
// #30907
360+
function wrapI1() {
361+
var iter = (function foo() {
362+
return __generator(this, function (_a) {
363+
switch (_a.label) {
364+
case 0:
365+
iter;
366+
return [4 /*yield*/, 1];
367+
case 1:
368+
_a.sent();
369+
return [2 /*return*/];
370+
}
371+
});
372+
})();
373+
}
374+
function wrapI2() {
375+
var iter = (function foo() {
376+
return __asyncGenerator(this, arguments, function foo_1() {
377+
return __generator(this, function (_a) {
378+
switch (_a.label) {
379+
case 0:
380+
iter;
381+
return [4 /*yield*/, __await(1)];
382+
case 1: return [4 /*yield*/, _a.sent()];
383+
case 2:
384+
_a.sent();
385+
return [2 /*return*/];
386+
}
387+
});
388+
});
389+
})();
390+
}

tests/baselines/reference/blockScopedVariablesUseBeforeDef.symbols

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,32 @@ function foo17() {
283283
>foo : Symbol(foo, Decl(blockScopedVariablesUseBeforeDef.ts, 133, 9))
284284
}
285285

286+
// #30907
287+
function wrapI1() {
288+
>wrapI1 : Symbol(wrapI1, Decl(blockScopedVariablesUseBeforeDef.ts, 134, 1))
289+
290+
const iter = (function* foo() {
291+
>iter : Symbol(iter, Decl(blockScopedVariablesUseBeforeDef.ts, 138, 9))
292+
>foo : Symbol(foo, Decl(blockScopedVariablesUseBeforeDef.ts, 138, 18))
293+
294+
iter;
295+
>iter : Symbol(iter, Decl(blockScopedVariablesUseBeforeDef.ts, 138, 9))
296+
297+
yield 1;
298+
})();
299+
}
300+
301+
function wrapI2() {
302+
>wrapI2 : Symbol(wrapI2, Decl(blockScopedVariablesUseBeforeDef.ts, 142, 1))
303+
304+
const iter = (async function* foo() {
305+
>iter : Symbol(iter, Decl(blockScopedVariablesUseBeforeDef.ts, 145, 9))
306+
>foo : Symbol(foo, Decl(blockScopedVariablesUseBeforeDef.ts, 145, 18))
307+
308+
iter;
309+
>iter : Symbol(iter, Decl(blockScopedVariablesUseBeforeDef.ts, 145, 9))
310+
311+
yield 1;
312+
})();
313+
}
314+

tests/baselines/reference/blockScopedVariablesUseBeforeDef.types

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,44 @@ function foo17() {
320320
>1 : 1
321321
}
322322

323+
// #30907
324+
function wrapI1() {
325+
>wrapI1 : () => void
326+
327+
const iter = (function* foo() {
328+
>iter : Generator<number, void, unknown>
329+
>(function* foo() { iter; yield 1; })() : Generator<number, void, unknown>
330+
>(function* foo() { iter; yield 1; }) : () => Generator<number, void, unknown>
331+
>function* foo() { iter; yield 1; } : () => Generator<number, void, unknown>
332+
>foo : () => Generator<number, void, unknown>
333+
334+
iter;
335+
>iter : Generator<number, void, unknown>
336+
337+
yield 1;
338+
>yield 1 : any
339+
>1 : 1
340+
341+
})();
342+
}
343+
344+
function wrapI2() {
345+
>wrapI2 : () => void
346+
347+
const iter = (async function* foo() {
348+
>iter : AsyncGenerator<number, void, unknown>
349+
>(async function* foo() { iter; yield 1; })() : AsyncGenerator<number, void, unknown>
350+
>(async function* foo() { iter; yield 1; }) : () => AsyncGenerator<number, void, unknown>
351+
>async function* foo() { iter; yield 1; } : () => AsyncGenerator<number, void, unknown>
352+
>foo : () => AsyncGenerator<number, void, unknown>
353+
354+
iter;
355+
>iter : AsyncGenerator<number, void, unknown>
356+
357+
yield 1;
358+
>yield 1 : any
359+
>1 : 1
360+
361+
})();
362+
}
363+

tests/cases/compiler/blockScopedVariablesUseBeforeDef.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,18 @@ function foo17() {
135135

136136
const foo = 1;
137137
}
138+
139+
// #30907
140+
function wrapI1() {
141+
const iter = (function* foo() {
142+
iter;
143+
yield 1;
144+
})();
145+
}
146+
147+
function wrapI2() {
148+
const iter = (async function* foo() {
149+
iter;
150+
yield 1;
151+
})();
152+
}

0 commit comments

Comments
 (0)