Skip to content

do not report use-before-declaration error in async like IIFE initial… #55279

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

Merged
merged 4 commits into from
Aug 28, 2023
Merged
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
3 changes: 1 addition & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ import {
isAssignmentOperator,
isAssignmentPattern,
isAssignmentTarget,
isAsyncFunction,
isAutoAccessorPropertyDeclaration,
isAwaitExpression,
isBinaryExpression,
Expand Down Expand Up @@ -3875,7 +3874,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function isSameScopeDescendentOf(initial: Node, parent: Node | undefined, stopAt: Node): boolean {
return !!parent && !!findAncestor(initial, n =>
n === parent
|| (n === stopAt || isFunctionLike(n) && (!getImmediatelyInvokedFunctionExpression(n) || isAsyncFunction(n)) ? "quit" : false));
|| (n === stopAt || isFunctionLike(n) && (!getImmediatelyInvokedFunctionExpression(n) || (getFunctionFlags(n) & FunctionFlags.AsyncGenerator)) ? "quit" : false));
}

function getAnyImportSyntax(node: Node): AnyImportSyntax | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,19 @@ blockScopedVariablesUseBeforeDef.ts(122,22): error TS2448: Block-scoped variable

const foo = 1;
}

// #30907
function wrapI1() {
const iter = (function* foo() {
iter;
yield 1;
})();
}

function wrapI2() {
const iter = (async function* foo() {
iter;
yield 1;
})();
}

59 changes: 59 additions & 0 deletions tests/baselines/reference/blockScopedVariablesUseBeforeDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ function foo17() {

const foo = 1;
}

// #30907
function wrapI1() {
const iter = (function* foo() {
iter;
yield 1;
})();
}

function wrapI2() {
const iter = (async function* foo() {
iter;
yield 1;
})();
}


//// [blockScopedVariablesUseBeforeDef.js]
Expand Down Expand Up @@ -179,6 +194,18 @@ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, p
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
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); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
function foo0() {
var a = x;
var x;
Expand Down Expand Up @@ -329,3 +356,35 @@ function foo17() {
}); })();
var foo = 1;
}
// #30907
function wrapI1() {
var iter = (function foo() {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
iter;
return [4 /*yield*/, 1];
case 1:
_a.sent();
return [2 /*return*/];
}
});
})();
}
function wrapI2() {
var iter = (function foo() {
return __asyncGenerator(this, arguments, function foo_1() {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
iter;
return [4 /*yield*/, __await(1)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
_a.sent();
return [2 /*return*/];
}
});
});
})();
}
29 changes: 29 additions & 0 deletions tests/baselines/reference/blockScopedVariablesUseBeforeDef.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,32 @@ function foo17() {
>foo : Symbol(foo, Decl(blockScopedVariablesUseBeforeDef.ts, 133, 9))
}

// #30907
function wrapI1() {
>wrapI1 : Symbol(wrapI1, Decl(blockScopedVariablesUseBeforeDef.ts, 134, 1))

const iter = (function* foo() {
>iter : Symbol(iter, Decl(blockScopedVariablesUseBeforeDef.ts, 138, 9))
>foo : Symbol(foo, Decl(blockScopedVariablesUseBeforeDef.ts, 138, 18))

iter;
>iter : Symbol(iter, Decl(blockScopedVariablesUseBeforeDef.ts, 138, 9))

yield 1;
})();
}

function wrapI2() {
>wrapI2 : Symbol(wrapI2, Decl(blockScopedVariablesUseBeforeDef.ts, 142, 1))

const iter = (async function* foo() {
>iter : Symbol(iter, Decl(blockScopedVariablesUseBeforeDef.ts, 145, 9))
>foo : Symbol(foo, Decl(blockScopedVariablesUseBeforeDef.ts, 145, 18))

iter;
>iter : Symbol(iter, Decl(blockScopedVariablesUseBeforeDef.ts, 145, 9))

yield 1;
})();
}

41 changes: 41 additions & 0 deletions tests/baselines/reference/blockScopedVariablesUseBeforeDef.types
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,44 @@ function foo17() {
>1 : 1
}

// #30907
function wrapI1() {
>wrapI1 : () => void

const iter = (function* foo() {
>iter : Generator<number, void, unknown>
>(function* foo() { iter; yield 1; })() : Generator<number, void, unknown>
>(function* foo() { iter; yield 1; }) : () => Generator<number, void, unknown>
>function* foo() { iter; yield 1; } : () => Generator<number, void, unknown>
>foo : () => Generator<number, void, unknown>

iter;
>iter : Generator<number, void, unknown>

yield 1;
>yield 1 : any
>1 : 1

})();
}

function wrapI2() {
>wrapI2 : () => void

const iter = (async function* foo() {
>iter : AsyncGenerator<number, void, unknown>
>(async function* foo() { iter; yield 1; })() : AsyncGenerator<number, void, unknown>
>(async function* foo() { iter; yield 1; }) : () => AsyncGenerator<number, void, unknown>
>async function* foo() { iter; yield 1; } : () => AsyncGenerator<number, void, unknown>
>foo : () => AsyncGenerator<number, void, unknown>

iter;
>iter : AsyncGenerator<number, void, unknown>

yield 1;
>yield 1 : any
>1 : 1

})();
}

15 changes: 15 additions & 0 deletions tests/cases/compiler/blockScopedVariablesUseBeforeDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,18 @@ function foo17() {

const foo = 1;
}

// #30907
function wrapI1() {
const iter = (function* foo() {
iter;
yield 1;
})();
}

function wrapI2() {
const iter = (async function* foo() {
iter;
yield 1;
})();
}