Skip to content

fix parameter initializer referencing identifier declared after it #29160

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
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
29 changes: 20 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25400,16 +25400,18 @@ namespace ts {
visit(node.initializer!);

function visit(n: Node): void {
if (isTypeNode(n) || isDeclarationName(n)) {
if (isTypeNode(n)) {
// do not dive in types
// skip declaration names (i.e. in object literal expressions)
return;
}
if (n.kind === SyntaxKind.PropertyAccessExpression) {
// skip property names in property access expression
return visit((<PropertyAccessExpression>n).expression);
}
else if (n.kind === SyntaxKind.Identifier) {
if (isDeclarationName(n) && n.parent.kind !== SyntaxKind.ShorthandPropertyAssignment) {
return;
}
// check FunctionLikeDeclaration.locals (stores parameters\function local variable)
// if it contains entry with a specified name
const symbol = resolveName(n, (<Identifier>n).escapedText, SymbolFlags.Value | SymbolFlags.Alias, /*nameNotFoundMessage*/undefined, /*nameArg*/undefined, /*isUse*/ false);
Expand Down Expand Up @@ -25438,13 +25440,22 @@ namespace ts {
if (current === node.initializer) {
return "quit";
}
return isFunctionLike(current.parent) ||
// computed property names/initializers in instance property declaration of class like entities
// are executed in constructor and thus deferred
(current.parent.kind === SyntaxKind.PropertyDeclaration &&
!(hasModifier(current.parent, ModifierFlags.Static)) &&
isClassLike(current.parent.parent));
})) {
if (current.kind === SyntaxKind.ComputedPropertyName) {
// computed property names are eagerly evaluated
return false;
}
if (isFunctionExpressionOrArrowFunction(current.parent)) {
// generator functions and async functions are not inlined in control flow when immediately invoked
if (current.parent.asteriskToken || hasModifier(current.parent, ModifierFlags.Async)) {
return true;
}
return !getImmediatelyInvokedFunctionExpression(current.parent);
}
return isFunctionLikeDeclaration(current.parent) ||
// initializers in instance property declaration of class like entities are executed in constructor and thus deferred
current.parent.kind === SyntaxKind.PropertyDeclaration && !hasModifier(current.parent, ModifierFlags.Static);
})
) {
return;
}
// fall through to report error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
tests/cases/compiler/capturedParametersInInitializers1.ts(16,20): error TS2373: Initializer of parameter 'y' cannot reference identifier 'z' declared after it.
tests/cases/compiler/capturedParametersInInitializers1.ts(20,26): error TS2373: Initializer of parameter 'y' cannot reference identifier 'z' declared after it.
tests/cases/compiler/capturedParametersInInitializers1.ts(36,21): error TS2373: Initializer of parameter 'y' cannot reference identifier 'z' declared after it.


==== tests/cases/compiler/capturedParametersInInitializers1.ts (3 errors) ====
// ok - usage is deferred
function foo1(y = class {c = x}, x = 1) {
new y().c;
}

// ok - used in function
function foo2(y = function z(x: typeof z) {}, z = 1) {
}

// ok -used in type
let a;
function foo3(y = { x: <typeof z>a }, z = 1) {
}

// error - used before declaration
function foo4(y = {z}, z = 1) {
~
!!! error TS2373: Initializer of parameter 'y' cannot reference identifier 'z' declared after it.
}

// error - used before declaration, IIFEs are inlined
function foo5(y = (() => z)(), z = 1) {
~
!!! error TS2373: Initializer of parameter 'y' cannot reference identifier 'z' declared after it.
}

// ok - IIFE inside another function
function foo6(y = () => (() => z)(), z = 1) {
}

// ok - used inside immediately invoked generator function
function foo7(y = (function*() {yield z})(), z = 1) {
}

// ok - used inside immediately invoked async function
function foo8(y = (async () => z)(), z = 1) {
}

// error - used as computed name of method
function foo9(y = {[z]() { return z; }}, z = 1) {
~
!!! error TS2373: Initializer of parameter 'y' cannot reference identifier 'z' declared after it.
}
81 changes: 61 additions & 20 deletions tests/baselines/reference/capturedParametersInInitializers1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,78 @@ function foo1(y = class {c = x}, x = 1) {
new y().c;
}

// ok - used in file
function foo2(y = function(x: typeof z) {}, z = 1) {

// ok - used in function
function foo2(y = function z(x: typeof z) {}, z = 1) {
}

// ok -used in type
let a;
function foo3(y = { x: <typeof z>a }, z = 1) {

}

// error - used before declaration
function foo4(y = {z}, z = 1) {
}

// error - used before declaration, IIFEs are inlined
function foo5(y = (() => z)(), z = 1) {
}

// ok - IIFE inside another function
function foo6(y = () => (() => z)(), z = 1) {
}

// ok - used inside immediately invoked generator function
function foo7(y = (function*() {yield z})(), z = 1) {
}

// ok - used inside immediately invoked async function
function foo8(y = (async () => z)(), z = 1) {
}

// error - used as computed name of method
function foo9(y = {[z]() { return z; }}, z = 1) {
}

//// [capturedParametersInInitializers1.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
// ok - usage is deferred
function foo1(y, x) {
if (y === void 0) { y = /** @class */ (function () {
function class_1() {
this.c = x;
}
return class_1;
}()); }
if (x === void 0) { x = 1; }
function foo1(y = class {
constructor() {
this.c = x;
}
}, x = 1) {
new y().c;
}
// ok - used in file
function foo2(y, z) {
if (y === void 0) { y = function (x) { }; }
if (z === void 0) { z = 1; }
// ok - used in function
function foo2(y = function z(x) { }, z = 1) {
}
// ok -used in type
var a;
function foo3(y, z) {
if (y === void 0) { y = { x: a }; }
if (z === void 0) { z = 1; }
let a;
function foo3(y = { x: a }, z = 1) {
}
// error - used before declaration
function foo4(y = { z }, z = 1) {
}
// error - used before declaration, IIFEs are inlined
function foo5(y = (() => z)(), z = 1) {
}
// ok - IIFE inside another function
function foo6(y = () => (() => z)(), z = 1) {
}
// ok - used inside immediately invoked generator function
function foo7(y = (function* () { yield z; })(), z = 1) {
}
// ok - used inside immediately invoked async function
function foo8(y = (() => __awaiter(this, void 0, void 0, function* () { return z; }))(), z = 1) {
}
// error - used as computed name of method
function foo9(y = { [z]() { return z; } }, z = 1) {
}
77 changes: 63 additions & 14 deletions tests/baselines/reference/capturedParametersInInitializers1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,75 @@ function foo1(y = class {c = x}, x = 1) {
>c : Symbol((Anonymous class).c, Decl(capturedParametersInInitializers1.ts, 1, 25))
}

// ok - used in file
function foo2(y = function(x: typeof z) {}, z = 1) {
// ok - used in function
function foo2(y = function z(x: typeof z) {}, z = 1) {
>foo2 : Symbol(foo2, Decl(capturedParametersInInitializers1.ts, 3, 1))
>y : Symbol(y, Decl(capturedParametersInInitializers1.ts, 6, 14))
>x : Symbol(x, Decl(capturedParametersInInitializers1.ts, 6, 27))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 6, 43))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 6, 43))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 6, 17))
>x : Symbol(x, Decl(capturedParametersInInitializers1.ts, 6, 29))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 6, 17))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 6, 45))
}

// ok -used in type
let a;
>a : Symbol(a, Decl(capturedParametersInInitializers1.ts, 11, 3))
>a : Symbol(a, Decl(capturedParametersInInitializers1.ts, 10, 3))

function foo3(y = { x: <typeof z>a }, z = 1) {
>foo3 : Symbol(foo3, Decl(capturedParametersInInitializers1.ts, 11, 6))
>y : Symbol(y, Decl(capturedParametersInInitializers1.ts, 12, 14))
>x : Symbol(x, Decl(capturedParametersInInitializers1.ts, 12, 19))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 12, 37))
>a : Symbol(a, Decl(capturedParametersInInitializers1.ts, 11, 3))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 12, 37))

>foo3 : Symbol(foo3, Decl(capturedParametersInInitializers1.ts, 10, 6))
>y : Symbol(y, Decl(capturedParametersInInitializers1.ts, 11, 14))
>x : Symbol(x, Decl(capturedParametersInInitializers1.ts, 11, 19))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 11, 37))
>a : Symbol(a, Decl(capturedParametersInInitializers1.ts, 10, 3))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 11, 37))
}

// error - used before declaration
function foo4(y = {z}, z = 1) {
>foo4 : Symbol(foo4, Decl(capturedParametersInInitializers1.ts, 12, 1))
>y : Symbol(y, Decl(capturedParametersInInitializers1.ts, 15, 14))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 15, 19))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 15, 22))
}

// error - used before declaration, IIFEs are inlined
function foo5(y = (() => z)(), z = 1) {
>foo5 : Symbol(foo5, Decl(capturedParametersInInitializers1.ts, 16, 1))
>y : Symbol(y, Decl(capturedParametersInInitializers1.ts, 19, 14))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 19, 30))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 19, 30))
}

// ok - IIFE inside another function
function foo6(y = () => (() => z)(), z = 1) {
>foo6 : Symbol(foo6, Decl(capturedParametersInInitializers1.ts, 20, 1))
>y : Symbol(y, Decl(capturedParametersInInitializers1.ts, 23, 14))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 23, 36))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 23, 36))
}

// ok - used inside immediately invoked generator function
function foo7(y = (function*() {yield z})(), z = 1) {
>foo7 : Symbol(foo7, Decl(capturedParametersInInitializers1.ts, 24, 1))
>y : Symbol(y, Decl(capturedParametersInInitializers1.ts, 27, 14))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 27, 44))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 27, 44))
}

// ok - used inside immediately invoked async function
function foo8(y = (async () => z)(), z = 1) {
>foo8 : Symbol(foo8, Decl(capturedParametersInInitializers1.ts, 28, 1))
>y : Symbol(y, Decl(capturedParametersInInitializers1.ts, 31, 14))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 31, 36))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 31, 36))
}

// error - used as computed name of method
function foo9(y = {[z]() { return z; }}, z = 1) {
>foo9 : Symbol(foo9, Decl(capturedParametersInInitializers1.ts, 32, 1))
>y : Symbol(y, Decl(capturedParametersInInitializers1.ts, 35, 14))
>[z] : Symbol([z], Decl(capturedParametersInInitializers1.ts, 35, 19))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 35, 40))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 35, 40))
>z : Symbol(z, Decl(capturedParametersInInitializers1.ts, 35, 40))
}
89 changes: 80 additions & 9 deletions tests/baselines/reference/capturedParametersInInitializers1.types
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ function foo1(y = class {c = x}, x = 1) {
>c : number
}

// ok - used in file
function foo2(y = function(x: typeof z) {}, z = 1) {
>foo2 : (y?: (x: number) => void, z?: number) => void
>y : (x: number) => void
>function(x: typeof z) {} : (x: number) => void
>x : number
>z : number
// ok - used in function
function foo2(y = function z(x: typeof z) {}, z = 1) {
>foo2 : (y?: (x: any) => void, z?: number) => void
>y : (x: any) => void
>function z(x: typeof z) {} : (x: any) => void
>z : (x: any) => void
>x : (x: any) => void
>z : (x: any) => void
>z : number
>1 : 1

}

// ok -used in type
Expand All @@ -42,5 +42,76 @@ function foo3(y = { x: <typeof z>a }, z = 1) {
>a : any
>z : number
>1 : 1

}

// error - used before declaration
function foo4(y = {z}, z = 1) {
>foo4 : (y?: { z: number; }, z?: number) => void
>y : { z: number; }
>{z} : { z: number; }
>z : number
>z : number
>1 : 1
}

// error - used before declaration, IIFEs are inlined
function foo5(y = (() => z)(), z = 1) {
>foo5 : (y?: number, z?: number) => void
>y : number
>(() => z)() : number
>(() => z) : () => number
>() => z : () => number
>z : number
>z : number
>1 : 1
}

// ok - IIFE inside another function
function foo6(y = () => (() => z)(), z = 1) {
>foo6 : (y?: () => number, z?: number) => void
>y : () => number
>() => (() => z)() : () => number
>(() => z)() : number
>(() => z) : () => number
>() => z : () => number
>z : number
>z : number
>1 : 1
}

// ok - used inside immediately invoked generator function
function foo7(y = (function*() {yield z})(), z = 1) {
>foo7 : (y?: IterableIterator<number>, z?: number) => void
>y : IterableIterator<number>
>(function*() {yield z})() : IterableIterator<number>
>(function*() {yield z}) : () => IterableIterator<number>
>function*() {yield z} : () => IterableIterator<number>
>yield z : any
>z : number
>z : number
>1 : 1
}

// ok - used inside immediately invoked async function
function foo8(y = (async () => z)(), z = 1) {
>foo8 : (y?: Promise<number>, z?: number) => void
>y : Promise<number>
>(async () => z)() : Promise<number>
>(async () => z) : () => Promise<number>
>async () => z : () => Promise<number>
>z : number
>z : number
>1 : 1
}

// error - used as computed name of method
function foo9(y = {[z]() { return z; }}, z = 1) {
>foo9 : (y?: { [x: number]: () => number; }, z?: number) => void
>y : { [x: number]: () => number; }
>{[z]() { return z; }} : { [x: number]: () => number; }
>[z] : () => number
>z : number
>z : number
>z : number
>1 : 1
}
Loading