-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Support 'for...of' loops in ES6 #2164
Merged
Merged
Changes from 22 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b6a4987
Remove error disallowing for...of loops
JsonFreeman 9e9dcde
Fix naming in checkForInStatement
JsonFreeman e0d7734
Basic for...of checking
JsonFreeman aa06622
Basic tests
JsonFreeman 277c931
Basic iterator support in for..of statement
JsonFreeman ec9e563
Add tests and accept correct baselines
JsonFreeman a2c5573
Get type of 'for...of' variables
JsonFreeman 1043d87
Tests and baselines for 'for...of' variables
JsonFreeman 2858771
Check iterable structure of the right hand side of 'for...of' statement
JsonFreeman 3d5c113
Iterator tests
JsonFreeman 0571086
More tests
JsonFreeman 732637d
Fix getIteratedType to work with 'any' at all levels
JsonFreeman 0049b21
Check for assignability to the relevant iteratable type
JsonFreeman 034bd09
Add tests for circular references in for...of loops
JsonFreeman c00a264
Move assert
JsonFreeman f3543b7
In ES6, an Iterable should contextually type an array literal
JsonFreeman 00408a6
Add tests for for-of destructuring
JsonFreeman 79b609b
Update comment
JsonFreeman 9faa09b
Delete bad baseline
JsonFreeman f8150d3
Support assignment patterns in 'for...of' statements
JsonFreeman 84a22be
Add a few more tests
JsonFreeman fd0fd36
Disallow destructuring in 'for...in'
JsonFreeman 996b9d2
Respond to PR feedback
JsonFreeman 14ba418
Merge branch 'master' of https://github.com/Microsoft/TypeScript into…
JsonFreeman bcb466e
Merge branch 'master' of https://github.com/Microsoft/TypeScript into…
JsonFreeman 3de73e6
Merge branch 'master' of https://github.com/Microsoft/TypeScript into…
JsonFreeman 8da49aa
Address feedback
JsonFreeman cb97686
Add tests for scoping
JsonFreeman 7dd7b43
Add one more test
JsonFreeman e0227d1
Merge branch 'master' of https://github.com/Microsoft/TypeScript into…
JsonFreeman 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 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 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 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 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: 7 additions & 0 deletions
7
tests/baselines/reference/for-inStatementsDestructuring.errors.txt
This file contains 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,7 @@ | ||
tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring.ts(1,10): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
|
||
|
||
==== tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring.ts (1 errors) ==== | ||
for (var [a, b] in []) {} | ||
~~~~~~ | ||
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. |
This file contains 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,5 @@ | ||
//// [for-inStatementsDestructuring.ts] | ||
for (var [a, b] in []) {} | ||
|
||
//// [for-inStatementsDestructuring.js] | ||
for (var _a = void 0, a = _a[0], b = _a[1] in []) { } |
7 changes: 7 additions & 0 deletions
7
tests/baselines/reference/for-inStatementsDestructuring2.errors.txt
This file contains 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,7 @@ | ||
tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring2.ts(1,10): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
|
||
|
||
==== tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring2.ts (1 errors) ==== | ||
for (var {a, b} in []) {} | ||
~~~~~~ | ||
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. |
This file contains 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,5 @@ | ||
//// [for-inStatementsDestructuring2.ts] | ||
for (var {a, b} in []) {} | ||
|
||
//// [for-inStatementsDestructuring2.js] | ||
for (var _a = void 0, a = _a.a, b = _a.b in []) { } |
8 changes: 8 additions & 0 deletions
8
tests/baselines/reference/for-inStatementsDestructuring3.errors.txt
This file contains 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 @@ | ||
tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring3.ts(2,6): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
|
||
|
||
==== tests/cases/conformance/statements/for-inStatements/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. |
This file contains 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,7 @@ | ||
//// [for-inStatementsDestructuring3.ts] | ||
var a, b; | ||
for ([a, b] in []) { } | ||
|
||
//// [for-inStatementsDestructuring3.js] | ||
var a, b; | ||
for ([a, b] in []) { } |
8 changes: 8 additions & 0 deletions
8
tests/baselines/reference/for-inStatementsDestructuring4.errors.txt
This file contains 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 @@ | ||
tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring4.ts(2,6): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern. | ||
|
||
|
||
==== tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring4.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. |
This file contains 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,7 @@ | ||
//// [for-inStatementsDestructuring4.ts] | ||
var a, b; | ||
for ({a, b} in []) { } | ||
|
||
//// [for-inStatementsDestructuring4.js] | ||
var a, b; | ||
for ({ a: a, b: b } in []) { } |
This file contains 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,7 @@ | ||
//// [for-of1.ts] | ||
var v; | ||
for (v of []) { } | ||
|
||
//// [for-of1.js] | ||
var v; | ||
for (v of []) { } |
This file contains 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 @@ | ||
=== tests/cases/conformance/es6/for-ofStatements/for-of1.ts === | ||
var v; | ||
>v : any | ||
|
||
for (v of []) { } | ||
>v : any | ||
>[] : undefined[] | ||
|
This file contains 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 @@ | ||
tests/cases/conformance/es6/for-ofStatements/for-of10.ts(2,6): error TS2322: Type 'number' is not assignable to type 'string'. | ||
|
||
|
||
==== tests/cases/conformance/es6/for-ofStatements/for-of10.ts (1 errors) ==== | ||
var v: string; | ||
for (v of [0]) { } | ||
~ | ||
!!! error TS2322: Type 'number' is not assignable to type 'string'. |
This file contains 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,7 @@ | ||
//// [for-of10.ts] | ||
var v: string; | ||
for (v of [0]) { } | ||
|
||
//// [for-of10.js] | ||
var v; | ||
for (v of [0]) { } |
This file contains 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,10 @@ | ||
tests/cases/conformance/es6/for-ofStatements/for-of11.ts(2,6): error TS2322: Type 'string | number' is not assignable to type 'string'. | ||
Type 'number' is not assignable to type 'string'. | ||
|
||
|
||
==== tests/cases/conformance/es6/for-ofStatements/for-of11.ts (1 errors) ==== | ||
var v: string; | ||
for (v of [0, ""]) { } | ||
~ | ||
!!! error TS2322: Type 'string | number' is not assignable to type 'string'. | ||
!!! error TS2322: Type 'number' is not assignable to type 'string'. |
This file contains 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,7 @@ | ||
//// [for-of11.ts] | ||
var v: string; | ||
for (v of [0, ""]) { } | ||
|
||
//// [for-of11.js] | ||
var v; | ||
for (v of [0, ""]) { } |
This file contains 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,10 @@ | ||
tests/cases/conformance/es6/for-ofStatements/for-of12.ts(2,6): error TS2322: Type 'string | number' is not assignable to type 'string'. | ||
Type 'number' is not assignable to type 'string'. | ||
|
||
|
||
==== tests/cases/conformance/es6/for-ofStatements/for-of12.ts (1 errors) ==== | ||
var v: string; | ||
for (v of [0, ""].values()) { } | ||
~ | ||
!!! error TS2322: Type 'string | number' is not assignable to type 'string'. | ||
!!! error TS2322: Type 'number' is not assignable to type 'string'. |
This file contains 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,7 @@ | ||
//// [for-of12.ts] | ||
var v: string; | ||
for (v of [0, ""].values()) { } | ||
|
||
//// [for-of12.js] | ||
var v; | ||
for (v of [0, ""].values()) { } |
This file contains 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,7 @@ | ||
//// [for-of13.ts] | ||
var v: string; | ||
for (v of [""].values()) { } | ||
|
||
//// [for-of13.js] | ||
var v; | ||
for (v of [""].values()) { } |
This file contains 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 @@ | ||
=== tests/cases/conformance/es6/for-ofStatements/for-of13.ts === | ||
var v: string; | ||
>v : string | ||
|
||
for (v of [""].values()) { } | ||
>v : string | ||
>[""].values() : IterableIterator<string> | ||
>[""].values : () => IterableIterator<string> | ||
>[""] : string[] | ||
>values : () => IterableIterator<string> | ||
|
This file contains 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/es6/for-ofStatements/for-of14.ts(2,11): error TS2488: The right-hand side of a 'for...of' statement must have a '[Symbol.iterator]()' method that returns an iterator. | ||
|
||
|
||
==== tests/cases/conformance/es6/for-ofStatements/for-of14.ts (1 errors) ==== | ||
var v: string; | ||
for (v of new StringIterator) { } // Should fail because the iterator is not iterable | ||
~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2488: The right-hand side of a 'for...of' statement must have a '[Symbol.iterator]()' method that returns an iterator. | ||
|
||
class StringIterator { | ||
next() { | ||
return ""; | ||
} | ||
} |
This file contains 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,21 @@ | ||
//// [for-of14.ts] | ||
var v: string; | ||
for (v of new StringIterator) { } // Should fail because the iterator is not iterable | ||
|
||
class StringIterator { | ||
next() { | ||
return ""; | ||
} | ||
} | ||
|
||
//// [for-of14.js] | ||
var v; | ||
for (v of new StringIterator) { } // Should fail because the iterator is not iterable | ||
var StringIterator = (function () { | ||
function StringIterator() { | ||
} | ||
StringIterator.prototype.next = function () { | ||
return ""; | ||
}; | ||
return StringIterator; | ||
})(); |
This file contains 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,17 @@ | ||
tests/cases/conformance/es6/for-ofStatements/for-of15.ts(2,11): error TS2490: The object returned by the next method of the iterator must have a 'value' property. | ||
|
||
|
||
==== tests/cases/conformance/es6/for-ofStatements/for-of15.ts (1 errors) ==== | ||
var v: string; | ||
for (v of new StringIterator) { } // Should fail | ||
~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2490: The object returned by the next method of the iterator must have a 'value' property. | ||
|
||
class StringIterator { | ||
next() { | ||
return ""; | ||
} | ||
[Symbol.iterator]() { | ||
return this; | ||
} | ||
} |
This file contains 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,27 @@ | ||
//// [for-of15.ts] | ||
var v: string; | ||
for (v of new StringIterator) { } // Should fail | ||
|
||
class StringIterator { | ||
next() { | ||
return ""; | ||
} | ||
[Symbol.iterator]() { | ||
return this; | ||
} | ||
} | ||
|
||
//// [for-of15.js] | ||
var v; | ||
for (v of new StringIterator) { } // Should fail | ||
var StringIterator = (function () { | ||
function StringIterator() { | ||
} | ||
StringIterator.prototype.next = function () { | ||
return ""; | ||
}; | ||
StringIterator.prototype[Symbol.iterator] = function () { | ||
return this; | ||
}; | ||
return StringIterator; | ||
})(); |
This file contains 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/es6/for-ofStatements/for-of16.ts(2,11): error TS2489: The iterator returned by the right-hand side of a 'for...of' statement must have a 'next()' method. | ||
|
||
|
||
==== tests/cases/conformance/es6/for-ofStatements/for-of16.ts (1 errors) ==== | ||
var v: string; | ||
for (v of new StringIterator) { } // Should fail | ||
~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2489: The iterator returned by the right-hand side of a 'for...of' statement must have a 'next()' method. | ||
|
||
class StringIterator { | ||
[Symbol.iterator]() { | ||
return this; | ||
} | ||
} |
This file contains 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,21 @@ | ||
//// [for-of16.ts] | ||
var v: string; | ||
for (v of new StringIterator) { } // Should fail | ||
|
||
class StringIterator { | ||
[Symbol.iterator]() { | ||
return this; | ||
} | ||
} | ||
|
||
//// [for-of16.js] | ||
var v; | ||
for (v of new StringIterator) { } // Should fail | ||
var StringIterator = (function () { | ||
function StringIterator() { | ||
} | ||
StringIterator.prototype[Symbol.iterator] = function () { | ||
return this; | ||
}; | ||
return StringIterator; | ||
})(); |
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.
Inconsistent with the last message -
next
should have parens and single-quotes.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.
Should it be "of an iterator"?
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.
You're totally right about
next
. I will fix that.I see your point. I guess unqualified noun phrases should be indefinite. So in a possessive chain of noun phrases, the tail should be indefinite, but all the preceding ones can be definite.
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.
Should it be 'The object' or 'The type' in the beginning?
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 think it should be 'The type', or if not, 'The value'.
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'll make it type.