Skip to content

Fix captured shorthand properties in ES2015 loops #59285

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 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49377,7 +49377,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const nodeLinks = getNodeLinks(node);
nodeLinks.calculatedFlags |= NodeCheckFlags.ConstructorReference | NodeCheckFlags.CapturedBlockScopedBinding | NodeCheckFlags.BlockScopedBindingInLoop;
if (isIdentifier(node) && isExpressionNode(node) && !(isPropertyAccessExpression(node.parent) && node.parent.name === node)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's the targeted fix.

const s = getSymbolAtLocation(node, /*ignoreErrors*/ true);
const s = getResolvedSymbol(node);
if (s && s !== unknownSymbol) {
checkIdentifierCalculateNodeCheckFlags(node, s);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3550,7 +3550,7 @@ export function isInExpressionContext(node: Node): boolean {
case SyntaxKind.ExpressionWithTypeArguments:
return (parent as ExpressionWithTypeArguments).expression === node && !isPartOfTypeNode(parent);
case SyntaxKind.ShorthandPropertyAssignment:
return (parent as ShorthandPropertyAssignment).objectAssignmentInitializer === node;
return ((parent as ShorthandPropertyAssignment).objectAssignmentInitializer ?? (parent as ShorthandPropertyAssignment).name) === node;
case SyntaxKind.SatisfiesExpression:
return node === (parent as SatisfiesExpression).expression;
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/capturedShorthandPropertyAssignmentNoCheck.ts] ////

//// [capturedShorthandPropertyAssignmentNoCheck.ts]
const fns = [];
for (const value of [1, 2, 3]) {
fns.push(() => ({ value }));
}
const result = fns.map(fn => fn());
console.log(result)


//// [capturedShorthandPropertyAssignmentNoCheck.js]
var fns = [];
var _loop_1 = function (value) {
fns.push(function () { return ({ value: value }); });
};
for (var _i = 0, _a = [1, 2, 3]; _i < _a.length; _i++) {
var value = _a[_i];
_loop_1(value);
}
var result = fns.map(function (fn) { return fn(); });
console.log(result);
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/capturedShorthandPropertyAssignmentNoCheck.ts] ////

=== capturedShorthandPropertyAssignmentNoCheck.ts ===
const fns = [];
>fns : Symbol(fns, Decl(capturedShorthandPropertyAssignmentNoCheck.ts, 0, 5))

for (const value of [1, 2, 3]) {
>value : Symbol(value, Decl(capturedShorthandPropertyAssignmentNoCheck.ts, 1, 10))

fns.push(() => ({ value }));
>fns.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
>fns : Symbol(fns, Decl(capturedShorthandPropertyAssignmentNoCheck.ts, 0, 5))
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
>value : Symbol(value, Decl(capturedShorthandPropertyAssignmentNoCheck.ts, 2, 21))
}
const result = fns.map(fn => fn());
>result : Symbol(result, Decl(capturedShorthandPropertyAssignmentNoCheck.ts, 4, 5))
>fns.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>fns : Symbol(fns, Decl(capturedShorthandPropertyAssignmentNoCheck.ts, 0, 5))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>fn : Symbol(fn, Decl(capturedShorthandPropertyAssignmentNoCheck.ts, 4, 23))
>fn : Symbol(fn, Decl(capturedShorthandPropertyAssignmentNoCheck.ts, 4, 23))

console.log(result)
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>result : Symbol(result, Decl(capturedShorthandPropertyAssignmentNoCheck.ts, 4, 5))

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//// [tests/cases/compiler/capturedShorthandPropertyAssignmentNoCheck.ts] ////

=== capturedShorthandPropertyAssignmentNoCheck.ts ===
const fns = [];
>fns : any[]
> : ^^^^^
>[] : undefined[]
> : ^^^^^^^^^^^

for (const value of [1, 2, 3]) {
>value : number
> : ^^^^^^
>[1, 2, 3] : number[]
> : ^^^^^^^^
>1 : 1
> : ^
>2 : 2
> : ^
>3 : 3
> : ^

fns.push(() => ({ value }));
>fns.push(() => ({ value })) : number
> : ^^^^^^
>fns.push : (...items: any[]) => number
> : ^^^^ ^^^^^^^^^^^^
>fns : any[]
> : ^^^^^
>push : (...items: any[]) => number
> : ^^^^ ^^^^^^^^^^^^
>() => ({ value }) : () => { value: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>({ value }) : { value: number; }
> : ^^^^^^^^^^^^^^^^^^
>{ value } : { value: number; }
> : ^^^^^^^^^^^^^^^^^^
>value : number
> : ^^^^^^
}
const result = fns.map(fn => fn());
>result : any[]
> : ^^^^^
>fns.map(fn => fn()) : any[]
> : ^^^^^
>fns.map : <U>(callbackfn: (value: any, index: number, array: any[]) => U, thisArg?: any) => U[]
> : ^ ^^ ^^^ ^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^
>fns : any[]
> : ^^^^^
>map : <U>(callbackfn: (value: any, index: number, array: any[]) => U, thisArg?: any) => U[]
> : ^ ^^ ^^^ ^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^
>fn => fn() : (fn: any) => any
> : ^ ^^^^^^^^^^^^^
>fn : any
>fn() : any
>fn : any

console.log(result)
>console.log(result) : void
> : ^^^^
>console.log : (...data: any[]) => void
> : ^^^^ ^^ ^^^^^
>console : Console
> : ^^^^^^^
>log : (...data: any[]) => void
> : ^^^^ ^^ ^^^^^
>result : any[]
> : ^^^^^

Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ const obj = {

/** @type {string} */
lol
>lol : string
> : ^^^^^^
>lol : undefined
> : ^^^^^^^^^
}
lol = "string"
>lol = "string" : "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ export function makeNewChannel<T extends ChannelType>(type: T): NewChannel<Chann
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>type : T
> : ^
>localChannelId : string
> : ^^^^^^
>localChannelId : "blahblahblah"
> : ^^^^^^^^^^^^^^
}

const newTextChannel = makeNewChannel('text');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ function f15() {
> : ^^^^^^
>b4 : number
> : ^^^^^^
>c4 : boolean
> : ^^^^^^^
>c4 : true
> : ^^^^
}
var { a4, b4, c4 } = f15();
>a4 : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ function f15() {
> : ^^^^^^
>b4 : number
> : ^^^^^^
>c4 : boolean
> : ^^^^^^^
>c4 : true
> : ^^^^
}
var { a4, b4, c4 } = f15();
>a4 : string
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/declarationsAndAssignments.types
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,8 @@ function f15() {
> : ^^^^^^
>b : number
> : ^^^^^^
>c : boolean
> : ^^^^^^^
>c : true
> : ^^^^
}

function f16() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ const obj = {
> : ^^^^^^^^^^^^^^^
>{param2} : { param2: string; }
> : ^^^^^^^^^^^^^^^^^^^
>param2 : string
> : ^^^^^^
>param2 : "value2"
> : ^^^^^^^^
>{} : {}
> : ^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = {
> : ^^^^^^^^^^^^^^^^^^^^^^^^^

customSymbol,
>customSymbol : symbol
> : ^^^^^^
>customSymbol : unique symbol
> : ^^^^^^^^^^^^^

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function f1() {
> : ^^^^^^^^^^^^^^^
>{ b } : { b: boolean; }
> : ^^^^^^^^^^^^^^^
>b : boolean
> : ^^^^^^^
>b : true
> : ^^^^

// Desired: OK
// 3.0: OK
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/shorthand-property-es5-es6.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ const bar = { foo, baz };
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
>foo : any
> : ^^^
>baz : number
> : ^^^^^^
>baz : 42
> : ^^

4 changes: 2 additions & 2 deletions tests/baselines/reference/shorthand-property-es6-amd.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ const bar = { foo, baz };
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
>foo : any
> : ^^^
>baz : number
> : ^^^^^^
>baz : 42
> : ^^

4 changes: 2 additions & 2 deletions tests/baselines/reference/shorthand-property-es6-es6.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ const bar = { foo, baz };
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
>foo : any
> : ^^^
>baz : number
> : ^^^^^^
>baz : 42
> : ^^

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function foo () {
> : ^^^^^^^^^^^^^^^^^
>{ test } : { test: string; }
> : ^^^^^^^^^^^^^^^^^
>test : string
>test : "test"
> : ^^^^^^
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function foo () {
> : ^^^^^^^^^^^^^^^^^
>{ test } : { test: string; }
> : ^^^^^^^^^^^^^^^^^
>test : string
>test : "test"
> : ^^^^^^
}

8 changes: 4 additions & 4 deletions tests/baselines/reference/systemObjectShorthandRename.types
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const x2 = {x}
> : ^^^^^^^^^^^^^^
>{x} : { x: string; }
> : ^^^^^^^^^^^^^^
>x : string
> : ^^^^^^
>x : "X"
> : ^^^

const a = {x2}
>a : { x2: { x: string; }; }
Expand All @@ -39,6 +39,6 @@ const b = {x3}
> : ^^^^^^^^^^^^^^^
>{x3} : { x3: string; }
> : ^^^^^^^^^^^^^^^
>x3 : string
> : ^^^^^^
>x3 : "X"
> : ^^^

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @target: es5

const fns = [];
for (const value of [1, 2, 3]) {
fns.push(() => ({ value }));
}
const result = fns.map(fn => fn());
console.log(result)