-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Support destructuring in for-in #54853
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
Open
Andarist
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/destructuring-support-for-in
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5c0373a
Support destructuring in for-in
Andarist e4a1052
add evaluation tests
Andarist 5651013
Merge remote-tracking branch 'origin/main' into fix/destructuring-sup…
Andarist 5f17fc7
fix lint
Andarist 2e71d10
Merge remote-tracking branch 'origin/main' into fix/destructuring-sup…
Andarist 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
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 |
---|---|---|
|
@@ -51,6 +51,7 @@ import { | |
flattenDestructuringAssignment, | ||
flattenDestructuringBinding, | ||
FlattenLevel, | ||
ForInOrOfStatement, | ||
ForInStatement, | ||
ForOfStatement, | ||
ForStatement, | ||
|
@@ -85,6 +86,7 @@ import { | |
isArrayLiteralExpression, | ||
isArrowFunction, | ||
isAssignmentExpression, | ||
isAssignmentPattern, | ||
isBinaryExpression, | ||
isBindingPattern, | ||
isBlock, | ||
|
@@ -2684,11 +2686,16 @@ export function transformES2015(context: TransformationContext): (x: SourceFile | |
} | ||
|
||
function visitForInStatement(node: ForInStatement, outermostLabeledStatement: LabeledStatement | undefined) { | ||
const needsConversionForDestructuring = node.initializer.kind === SyntaxKind.VariableDeclarationList ? | ||
isBindingPattern((node.initializer as VariableDeclarationList).declarations[0].name) : | ||
isAssignmentPattern(node.initializer); | ||
return visitIterationStatementWithFacts( | ||
HierarchyFacts.ForInOrForOfStatementExcludes, | ||
HierarchyFacts.ForInOrForOfStatementIncludes, | ||
node, | ||
outermostLabeledStatement); | ||
outermostLabeledStatement, | ||
needsConversionForDestructuring ? convertForInStatementForDestructuring : undefined | ||
); | ||
} | ||
|
||
function visitForOfStatement(node: ForOfStatement, outermostLabeledStatement: LabeledStatement | undefined): VisitResult<Statement> { | ||
|
@@ -2700,7 +2707,7 @@ export function transformES2015(context: TransformationContext): (x: SourceFile | |
compilerOptions.downlevelIteration ? convertForOfStatementForIterable : convertForOfStatementForArray); | ||
} | ||
|
||
function convertForOfStatementHead(node: ForOfStatement, boundValue: Expression, convertedLoopBodyStatements: Statement[] | undefined) { | ||
function convertForInOrOfStatementHead(node: ForInOrOfStatement, boundValue: Expression, convertedLoopBodyStatements: Statement[] | undefined) { | ||
const statements: Statement[] = []; | ||
const initializer = node.initializer; | ||
if (isVariableDeclarationList(initializer)) { | ||
|
@@ -2800,6 +2807,36 @@ export function transformES2015(context: TransformationContext): (x: SourceFile | |
); | ||
} | ||
|
||
function convertForInStatementForDestructuring(node: ForInStatement, outermostLabeledStatement: LabeledStatement | undefined, convertedLoopBodyStatements: Statement[] | undefined): Statement { | ||
const lhsReference = factory.createUniqueName("key"); | ||
|
||
const forInStatement = setEmitFlags( | ||
setTextRange( | ||
factory.createForInStatement( | ||
/*initializer*/ setEmitFlags( | ||
setTextRange( | ||
factory.createVariableDeclarationList([ | ||
setTextRange(factory.createVariableDeclaration(lhsReference, /*exclamationToken*/ undefined, /*type*/ undefined, /*initializer*/ undefined), node.initializer), | ||
]), | ||
node.expression | ||
), | ||
EmitFlags.NoHoisting | ||
), | ||
/*expression*/ node.expression, | ||
/*statement*/ convertForInOrOfStatementHead( | ||
node, | ||
lhsReference, | ||
convertedLoopBodyStatements | ||
) | ||
), | ||
/*location*/ node | ||
), | ||
EmitFlags.NoTokenTrailingSourceMaps | ||
); | ||
|
||
return factory.restoreEnclosingLabel(forInStatement, outermostLabeledStatement, convertedLoopState && resetLabel); | ||
} | ||
|
||
function convertForOfStatementForArray(node: ForOfStatement, outermostLabeledStatement: LabeledStatement | undefined, convertedLoopBodyStatements: Statement[] | undefined): Statement { | ||
// The following ES6 code: | ||
// | ||
|
@@ -2856,7 +2893,7 @@ export function transformES2015(context: TransformationContext): (x: SourceFile | |
node.expression | ||
), | ||
/*incrementor*/ setTextRange(factory.createPostfixIncrement(counter), node.expression), | ||
/*statement*/ convertForOfStatementHead( | ||
/*statement*/ convertForInOrOfStatementHead( | ||
node, | ||
factory.createElementAccessExpression(rhsReference, counter), | ||
convertedLoopBodyStatements | ||
|
@@ -2867,7 +2904,6 @@ export function transformES2015(context: TransformationContext): (x: SourceFile | |
|
||
// Disable trailing source maps for the OpenParenToken to align source map emit with the old emitter. | ||
setEmitFlags(forStatement, EmitFlags.NoTokenTrailingSourceMaps); | ||
setTextRange(forStatement, node); | ||
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. this was redundant - this call is a mutation and the same call is already done when assigning to |
||
return factory.restoreEnclosingLabel(forStatement, outermostLabeledStatement, convertedLoopState && resetLabel); | ||
} | ||
|
||
|
@@ -2905,7 +2941,7 @@ export function transformES2015(context: TransformationContext): (x: SourceFile | |
), | ||
/*condition*/ factory.createLogicalNot(factory.createPropertyAccessExpression(result, "done")), | ||
/*incrementor*/ factory.createAssignment(result, next), | ||
/*statement*/ convertForOfStatementHead( | ||
/*statement*/ convertForInOrOfStatementHead( | ||
node, | ||
factory.createPropertyAccessExpression(result, "value"), | ||
convertedLoopBodyStatements | ||
|
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
7 changes: 2 additions & 5 deletions
7
tests/baselines/reference/for-inStatementsDestructuring.errors.txt
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 |
---|---|---|
@@ -1,10 +1,7 @@ | ||
for-inStatementsDestructuring.ts(1,10): error TS2461: Type 'string' is not an array type. | ||
for-inStatementsDestructuring.ts(1,10): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
|
||
|
||
==== for-inStatementsDestructuring.ts (2 errors) ==== | ||
==== for-inStatementsDestructuring.ts (1 errors) ==== | ||
for (var [a, b] in []) {} | ||
~~~~~~ | ||
!!! error TS2461: Type 'string' is not an array type. | ||
~~~~~~ | ||
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
!!! error TS2461: Type 'string' is not an array type. |
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
5 changes: 1 addition & 4 deletions
5
tests/baselines/reference/for-inStatementsDestructuring2.errors.txt
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
4 changes: 2 additions & 2 deletions
4
tests/baselines/reference/for-inStatementsDestructuring3.errors.txt
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
for-inStatementsDestructuring3.ts(2,6): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
for-inStatementsDestructuring3.ts(2,6): error TS2461: Type '"length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"' is not an array type. | ||
|
||
|
||
==== for-inStatementsDestructuring3.ts (1 errors) ==== | ||
var a, b; | ||
for ([a, b] in []) { } | ||
~~~~~~ | ||
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
!!! error TS2461: Type '"length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"' is not an array type. |
11 changes: 7 additions & 4 deletions
11
tests/baselines/reference/for-inStatementsDestructuring4.errors.txt
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
for-inStatementsDestructuring4.ts(2,6): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
for-inStatementsDestructuring4.ts(2,7): error TS2339: Property 'a' does not exist on type '"length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"'. | ||
for-inStatementsDestructuring4.ts(2,10): error TS2339: Property 'b' does not exist on type '"length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"'. | ||
|
||
|
||
==== for-inStatementsDestructuring4.ts (1 errors) ==== | ||
==== for-inStatementsDestructuring4.ts (2 errors) ==== | ||
var a, b; | ||
for ({a, b} in []) { } | ||
~~~~~~ | ||
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
~ | ||
!!! error TS2339: Property 'a' does not exist on type '"length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"'. | ||
~ | ||
!!! error TS2339: Property 'b' does not exist on type '"length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"'. |
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
11 changes: 11 additions & 0 deletions
11
tests/baselines/reference/for-inStatementsDestructuring5(target=es5).errors.txt
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,11 @@ | ||
for-inStatementsDestructuring5.ts(4,6): error TS2461: Type 'string' is not an array type. | ||
|
||
|
||
==== for-inStatementsDestructuring5.ts (1 errors) ==== | ||
let a: string; | ||
declare let obj: { [s: string]: string }; | ||
|
||
for ([a] in obj) {} | ||
~~~ | ||
!!! error TS2461: Type 'string' is not an array type. | ||
|
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/for-inStatementsDestructuring5(target=es5).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,12 @@ | ||
//// [tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring5.ts] //// | ||
|
||
//// [for-inStatementsDestructuring5.ts] | ||
let a: string; | ||
declare let obj: { [s: string]: string }; | ||
|
||
for ([a] in obj) {} | ||
|
||
|
||
//// [for-inStatementsDestructuring5.js] | ||
var a; | ||
for ([a] in obj) { } |
14 changes: 14 additions & 0 deletions
14
tests/baselines/reference/for-inStatementsDestructuring5(target=es5).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,14 @@ | ||
//// [tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring5.ts] //// | ||
|
||
=== for-inStatementsDestructuring5.ts === | ||
let a: string; | ||
>a : Symbol(a, Decl(for-inStatementsDestructuring5.ts, 0, 3)) | ||
|
||
declare let obj: { [s: string]: string }; | ||
>obj : Symbol(obj, Decl(for-inStatementsDestructuring5.ts, 1, 11)) | ||
>s : Symbol(s, Decl(for-inStatementsDestructuring5.ts, 1, 20)) | ||
|
||
for ([a] in obj) {} | ||
>a : Symbol(a, Decl(for-inStatementsDestructuring5.ts, 0, 3)) | ||
>obj : Symbol(obj, Decl(for-inStatementsDestructuring5.ts, 1, 11)) | ||
|
15 changes: 15 additions & 0 deletions
15
tests/baselines/reference/for-inStatementsDestructuring5(target=es5).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,15 @@ | ||
//// [tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring5.ts] //// | ||
|
||
=== for-inStatementsDestructuring5.ts === | ||
let a: string; | ||
>a : string | ||
|
||
declare let obj: { [s: string]: string }; | ||
>obj : { [s: string]: string; } | ||
>s : string | ||
|
||
for ([a] in obj) {} | ||
>[a] : [string] | ||
>a : string | ||
>obj : { [s: string]: string; } | ||
|
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/for-inStatementsDestructuring5(target=esnext).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,12 @@ | ||
//// [tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring5.ts] //// | ||
|
||
//// [for-inStatementsDestructuring5.ts] | ||
let a: string; | ||
declare let obj: { [s: string]: string }; | ||
|
||
for ([a] in obj) {} | ||
|
||
|
||
//// [for-inStatementsDestructuring5.js] | ||
let a; | ||
for ([a] in obj) { } |
14 changes: 14 additions & 0 deletions
14
tests/baselines/reference/for-inStatementsDestructuring5(target=esnext).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,14 @@ | ||
//// [tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring5.ts] //// | ||
|
||
=== for-inStatementsDestructuring5.ts === | ||
let a: string; | ||
>a : Symbol(a, Decl(for-inStatementsDestructuring5.ts, 0, 3)) | ||
|
||
declare let obj: { [s: string]: string }; | ||
>obj : Symbol(obj, Decl(for-inStatementsDestructuring5.ts, 1, 11)) | ||
>s : Symbol(s, Decl(for-inStatementsDestructuring5.ts, 1, 20)) | ||
|
||
for ([a] in obj) {} | ||
>a : Symbol(a, Decl(for-inStatementsDestructuring5.ts, 0, 3)) | ||
>obj : Symbol(obj, Decl(for-inStatementsDestructuring5.ts, 1, 11)) | ||
|
15 changes: 15 additions & 0 deletions
15
tests/baselines/reference/for-inStatementsDestructuring5(target=esnext).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,15 @@ | ||
//// [tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring5.ts] //// | ||
|
||
=== for-inStatementsDestructuring5.ts === | ||
let a: string; | ||
>a : string | ||
|
||
declare let obj: { [s: string]: string }; | ||
>obj : { [s: string]: string; } | ||
>s : string | ||
|
||
for ([a] in obj) {} | ||
>[a] : [string] | ||
>a : string | ||
>obj : { [s: string]: string; } | ||
|
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/for-inStatementsDestructuring6(target=es5).errors.txt
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,12 @@ | ||
for-inStatementsDestructuring6.ts(5,6): error TS2461: Type 'string' is not an array type. | ||
|
||
|
||
==== for-inStatementsDestructuring6.ts (1 errors) ==== | ||
let b: number; | ||
let c: string[]; | ||
declare let obj: { [s: string]: string }; | ||
|
||
for ([b, ...c] in obj) {} | ||
~~~~~~~~~ | ||
!!! error TS2461: Type 'string' is not an array type. | ||
|
16 changes: 16 additions & 0 deletions
16
tests/baselines/reference/for-inStatementsDestructuring6(target=es5).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,16 @@ | ||
//// [tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring6.ts] //// | ||
|
||
//// [for-inStatementsDestructuring6.ts] | ||
let b: number; | ||
let c: string[]; | ||
declare let obj: { [s: string]: string }; | ||
|
||
for ([b, ...c] in obj) {} | ||
|
||
|
||
//// [for-inStatementsDestructuring6.js] | ||
var b; | ||
var c; | ||
for (var key_1 in obj) { | ||
b = key_1[0], c = key_1.slice(1); | ||
} |
Oops, something went wrong.
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.
I'm not sure if this should actually be using
getIndexTypeOrString(rightType)
. I used it because the branch below this uses that but because it does use it this code typechecks but, IMHO, it shouldn't (TS playground):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.
Created this PR to address/discuss this: #54856