-
Notifications
You must be signed in to change notification settings - Fork 13k
Fix destructuring control flow analysis #29053
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 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
43811dd
Use getIndexedAccessType when computing destructured types
ahejlsberg 57ed264
Accept new baselines
ahejlsberg 92f47a7
Use synthetic access expressions for destructuring control flow analysis
ahejlsberg c1ab2b0
Accept new baselines
ahejlsberg 307a9b6
Add tests
ahejlsberg 519e19a
Accept new baselines
ahejlsberg 109fcd5
Address CR feedback
ahejlsberg 54b46d7
Address CR feedback, take two
ahejlsberg 6763389
Improve error message for out-of-bounds tuple element access
ahejlsberg 43d92f0
Accept new baselines
ahejlsberg 6327339
No public API changes
ahejlsberg 2e6366f
Accept new baselines
ahejlsberg 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,15 +1,15 @@ | ||
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,8): error TS2459: Type 'undefined' has no property 'a' and no string index signature. | ||
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,18): error TS2459: Type 'undefined' has no property 'b' and no string index signature. | ||
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,8): error TS2339: Property 'a' does not exist on type 'undefined'. | ||
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,18): error TS2339: Property 'b' does not exist on type 'undefined'. | ||
|
||
|
||
==== tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts (2 errors) ==== | ||
var a: string, b: number; | ||
|
||
for ({ a: b = 1, b: a = ""} of []) { | ||
~ | ||
!!! error TS2459: Type 'undefined' has no property 'a' and no string index signature. | ||
!!! error TS2339: Property 'a' does not exist on type 'undefined'. | ||
~ | ||
!!! error TS2459: Type 'undefined' has no property 'b' and no string index signature. | ||
!!! error TS2339: Property 'b' does not exist on type 'undefined'. | ||
a; | ||
b; | ||
} |
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
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
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
47 changes: 47 additions & 0 deletions
47
tests/baselines/reference/destructuringControlFlow.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,47 @@ | ||
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(31,8): error TS2339: Property 'x' does not exist on type 'Number'. | ||
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(32,9): error TS2339: Property 'x' does not exist on type 'Number'. | ||
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(33,9): error TS2537: Type 'Number' has no matching index signature for type 'string'. | ||
|
||
|
||
==== tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts (3 errors) ==== | ||
function f1(obj: { a?: string }) { | ||
if (obj.a) { | ||
obj = {}; | ||
let a1 = obj["a"]; // string | undefined | ||
let a2 = obj.a; // string | undefined | ||
} | ||
} | ||
|
||
function f2(obj: [number, string] | null[]) { | ||
let a0 = obj[0]; // number | null | ||
let a1 = obj[1]; // string | null | ||
let [b0, b1] = obj; | ||
([a0, a1] = obj); | ||
if (obj[0] && obj[1]) { | ||
let c0 = obj[0]; // number | ||
let c1 = obj[1]; // string | ||
let [d0, d1] = obj; | ||
([c0, c1] = obj); | ||
} | ||
} | ||
|
||
function f3(obj: { a?: number, b?: string }) { | ||
if (obj.a && obj.b) { | ||
let { a, b } = obj; // number, string | ||
({ a, b } = obj); | ||
} | ||
} | ||
|
||
function f4() { | ||
let x: boolean; | ||
({ x } = 0); // Error | ||
~ | ||
!!! error TS2339: Property 'x' does not exist on type 'Number'. | ||
({ ["x"]: x } = 0); // Error | ||
~~~ | ||
!!! error TS2339: Property 'x' does not exist on type 'Number'. | ||
({ ["x" + ""]: x } = 0); // Errpr | ||
~~~~~~~~ | ||
!!! error TS2537: Type 'Number' has no matching index signature for type 'string'. | ||
} | ||
|
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,71 @@ | ||
//// [destructuringControlFlow.ts] | ||
function f1(obj: { a?: string }) { | ||
if (obj.a) { | ||
obj = {}; | ||
let a1 = obj["a"]; // string | undefined | ||
let a2 = obj.a; // string | undefined | ||
} | ||
} | ||
|
||
function f2(obj: [number, string] | null[]) { | ||
let a0 = obj[0]; // number | null | ||
let a1 = obj[1]; // string | null | ||
let [b0, b1] = obj; | ||
([a0, a1] = obj); | ||
if (obj[0] && obj[1]) { | ||
let c0 = obj[0]; // number | ||
let c1 = obj[1]; // string | ||
let [d0, d1] = obj; | ||
([c0, c1] = obj); | ||
} | ||
} | ||
|
||
function f3(obj: { a?: number, b?: string }) { | ||
if (obj.a && obj.b) { | ||
let { a, b } = obj; // number, string | ||
({ a, b } = obj); | ||
} | ||
} | ||
|
||
function f4() { | ||
let x: boolean; | ||
({ x } = 0); // Error | ||
({ ["x"]: x } = 0); // Error | ||
({ ["x" + ""]: x } = 0); // Errpr | ||
} | ||
|
||
|
||
//// [destructuringControlFlow.js] | ||
"use strict"; | ||
function f1(obj) { | ||
if (obj.a) { | ||
obj = {}; | ||
var a1 = obj["a"]; // string | undefined | ||
var a2 = obj.a; // string | undefined | ||
} | ||
} | ||
function f2(obj) { | ||
var a0 = obj[0]; // number | null | ||
var a1 = obj[1]; // string | null | ||
var b0 = obj[0], b1 = obj[1]; | ||
(a0 = obj[0], a1 = obj[1]); | ||
if (obj[0] && obj[1]) { | ||
var c0 = obj[0]; // number | ||
var c1 = obj[1]; // string | ||
var d0 = obj[0], d1 = obj[1]; | ||
(c0 = obj[0], c1 = obj[1]); | ||
} | ||
} | ||
function f3(obj) { | ||
if (obj.a && obj.b) { | ||
var a = obj.a, b = obj.b; // number, string | ||
(a = obj.a, b = obj.b); | ||
} | ||
} | ||
function f4() { | ||
var _a; | ||
var x; | ||
(x = 0..x); // Error | ||
(x = 0["x"]); // Error | ||
(_a = "x" + "", x = 0[_a]); // Errpr | ||
} |
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.
Do we really need to export this from the public API?