-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
54003a5
Test
jakebailey 033a906
Fix isInExpressionContext for name-only object prop shorthand
jakebailey 7e1e976
Use getResolvedSymbol to pass into checkIdentifierCalculateNodeCheckF…
jakebailey f820ca5
Revert isInExpressionContext change
jakebailey 6c910bf
Targeted fix
jakebailey 66113d3
Smaller
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3550,6 +3550,7 @@ export function isInExpressionContext(node: Node): boolean { | |
case SyntaxKind.ExpressionWithTypeArguments: | ||
return (parent as ExpressionWithTypeArguments).expression === node && !isPartOfTypeNode(parent); | ||
case SyntaxKind.ShorthandPropertyAssignment: | ||
// TODO(jakebailey): it's possible that node could be the name, too | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I plan to follow-up on this after merging; hoping that it's straightforwad to fix getTypeAtLocation to retain its old behavior. |
||
return (parent as ShorthandPropertyAssignment).objectAssignmentInitializer === node; | ||
case SyntaxKind.SatisfiesExpression: | ||
return node === (parent as SatisfiesExpression).expression; | ||
|
22 changes: 22 additions & 0 deletions
22
tests/baselines/reference/capturedShorthandPropertyAssignmentNoCheck.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
29 changes: 29 additions & 0 deletions
29
tests/baselines/reference/capturedShorthandPropertyAssignmentNoCheck.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
68 changes: 68 additions & 0 deletions
68
tests/baselines/reference/capturedShorthandPropertyAssignmentNoCheck.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
> : ^^^^^ | ||
|
8 changes: 8 additions & 0 deletions
8
tests/cases/compiler/capturedShorthandPropertyAssignmentNoCheck.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.