Skip to content

Commit d6045e4

Browse files
committed
Merge pull request #2164 from Microsoft/for-ofES6
Support 'for...of' loops in ES6
2 parents 18b7257 + e0227d1 commit d6045e4

File tree

227 files changed

+2794
-184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

227 files changed

+2794
-184
lines changed

src/compiler/checker.ts

+226-63
Large diffs are not rendered by default.

src/compiler/diagnosticInformationMap.generated.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ module ts {
328328
for_of_statements_are_only_available_when_targeting_ECMAScript_6_or_higher: { code: 2482, category: DiagnosticCategory.Error, key: "'for...of' statements are only available when targeting ECMAScript 6 or higher." },
329329
The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation: { code: 2483, category: DiagnosticCategory.Error, key: "The left-hand side of a 'for...of' statement cannot use a type annotation." },
330330
Export_declaration_conflicts_with_exported_declaration_of_0: { code: 2484, category: DiagnosticCategory.Error, key: "Export declaration conflicts with exported declaration of '{0}'" },
331+
The_left_hand_side_of_a_for_of_statement_cannot_be_a_previously_defined_constant: { code: 2485, category: DiagnosticCategory.Error, key: "The left-hand side of a 'for...of' statement cannot be a previously defined constant." },
332+
The_left_hand_side_of_a_for_in_statement_cannot_be_a_previously_defined_constant: { code: 2486, category: DiagnosticCategory.Error, key: "The left-hand side of a 'for...in' statement cannot be a previously defined constant." },
333+
Invalid_left_hand_side_in_for_of_statement: { code: 2487, category: DiagnosticCategory.Error, key: "Invalid left-hand side in 'for...of' statement." },
334+
The_right_hand_side_of_a_for_of_statement_must_have_a_Symbol_iterator_method_that_returns_an_iterator: { code: 2488, category: DiagnosticCategory.Error, key: "The right-hand side of a 'for...of' statement must have a '[Symbol.iterator]()' method that returns an iterator." },
335+
The_iterator_returned_by_the_right_hand_side_of_a_for_of_statement_must_have_a_next_method: { code: 2489, category: DiagnosticCategory.Error, key: "The iterator returned by the right-hand side of a 'for...of' statement must have a 'next()' method." },
336+
The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property: { code: 2490, category: DiagnosticCategory.Error, key: "The type returned by the 'next()' method of an iterator must have a 'value' property." },
337+
The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern: { code: 2491, category: DiagnosticCategory.Error, key: "The left-hand side of a 'for...in' statement cannot be a destructuring pattern." },
331338
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
332339
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
333340
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },
@@ -474,6 +481,5 @@ module ts {
474481
yield_expressions_are_not_currently_supported: { code: 9000, category: DiagnosticCategory.Error, key: "'yield' expressions are not currently supported." },
475482
Generators_are_not_currently_supported: { code: 9001, category: DiagnosticCategory.Error, key: "Generators are not currently supported." },
476483
The_arguments_object_cannot_be_referenced_in_an_arrow_function_Consider_using_a_standard_function_expression: { code: 9002, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression." },
477-
for_of_statements_are_not_currently_supported: { code: 9003, category: DiagnosticCategory.Error, key: "'for...of' statements are not currently supported." },
478484
};
479485
}

src/compiler/diagnosticMessages.json

+28-4
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,34 @@
13041304
"category": "Error",
13051305
"code": 2484
13061306
},
1307+
"The left-hand side of a 'for...of' statement cannot be a previously defined constant.": {
1308+
"category": "Error",
1309+
"code": 2485
1310+
},
1311+
"The left-hand side of a 'for...in' statement cannot be a previously defined constant.": {
1312+
"category": "Error",
1313+
"code": 2486
1314+
},
1315+
"Invalid left-hand side in 'for...of' statement.": {
1316+
"category": "Error",
1317+
"code": 2487
1318+
},
1319+
"The right-hand side of a 'for...of' statement must have a '[Symbol.iterator]()' method that returns an iterator.": {
1320+
"category": "Error",
1321+
"code": 2488
1322+
},
1323+
"The iterator returned by the right-hand side of a 'for...of' statement must have a 'next()' method.": {
1324+
"category": "Error",
1325+
"code": 2489
1326+
},
1327+
"The type returned by the 'next()' method of an iterator must have a 'value' property.": {
1328+
"category": "Error",
1329+
"code": 2490
1330+
},
1331+
"The left-hand side of a 'for...in' statement cannot be a destructuring pattern.": {
1332+
"category": "Error",
1333+
"code": 2491
1334+
},
13071335

13081336
"Import declaration '{0}' is using private name '{1}'.": {
13091337
"category": "Error",
@@ -1889,9 +1917,5 @@
18891917
"The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.": {
18901918
"category": "Error",
18911919
"code": 9002
1892-
},
1893-
"'for...of' statements are not currently supported.": {
1894-
"category": "Error",
1895-
"code": 9003
18961920
}
18971921
}

src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ module ts {
683683
}
684684

685685
export function isBindingPattern(node: Node) {
686-
return node.kind === SyntaxKind.ArrayBindingPattern || node.kind === SyntaxKind.ObjectBindingPattern;
686+
return !!node && (node.kind === SyntaxKind.ArrayBindingPattern || node.kind === SyntaxKind.ObjectBindingPattern);
687687
}
688688

689689
export function isInAmbientContext(node: Node): boolean {

tests/baselines/reference/downlevelLetConst16.errors.txt

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
tests/cases/compiler/downlevelLetConst16.ts(189,5): error TS9003: 'for...of' statements are not currently supported.
2-
tests/cases/compiler/downlevelLetConst16.ts(196,5): error TS9003: 'for...of' statements are not currently supported.
3-
tests/cases/compiler/downlevelLetConst16.ts(203,5): error TS9003: 'for...of' statements are not currently supported.
4-
tests/cases/compiler/downlevelLetConst16.ts(210,5): error TS9003: 'for...of' statements are not currently supported.
5-
tests/cases/compiler/downlevelLetConst16.ts(217,5): error TS9003: 'for...of' statements are not currently supported.
6-
tests/cases/compiler/downlevelLetConst16.ts(224,5): error TS9003: 'for...of' statements are not currently supported.
1+
tests/cases/compiler/downlevelLetConst16.ts(189,5): error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
2+
tests/cases/compiler/downlevelLetConst16.ts(196,5): error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
3+
tests/cases/compiler/downlevelLetConst16.ts(203,5): error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
4+
tests/cases/compiler/downlevelLetConst16.ts(210,5): error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
5+
tests/cases/compiler/downlevelLetConst16.ts(217,5): error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
6+
tests/cases/compiler/downlevelLetConst16.ts(224,5): error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
77

88

99
==== tests/cases/compiler/downlevelLetConst16.ts (6 errors) ====
@@ -197,7 +197,7 @@ tests/cases/compiler/downlevelLetConst16.ts(224,5): error TS9003: 'for...of' sta
197197
function foo7() {
198198
for (let x of []) {
199199
~~~
200-
!!! error TS9003: 'for...of' statements are not currently supported.
200+
!!! error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
201201
use(x);
202202
}
203203
use(x);
@@ -206,7 +206,7 @@ tests/cases/compiler/downlevelLetConst16.ts(224,5): error TS9003: 'for...of' sta
206206
function foo8() {
207207
for (let [x] of []) {
208208
~~~
209-
!!! error TS9003: 'for...of' statements are not currently supported.
209+
!!! error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
210210
use(x);
211211
}
212212
use(x);
@@ -215,7 +215,7 @@ tests/cases/compiler/downlevelLetConst16.ts(224,5): error TS9003: 'for...of' sta
215215
function foo9() {
216216
for (let {a: x} of []) {
217217
~~~
218-
!!! error TS9003: 'for...of' statements are not currently supported.
218+
!!! error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
219219
use(x);
220220
}
221221
use(x);
@@ -224,7 +224,7 @@ tests/cases/compiler/downlevelLetConst16.ts(224,5): error TS9003: 'for...of' sta
224224
function foo10() {
225225
for (const x of []) {
226226
~~~
227-
!!! error TS9003: 'for...of' statements are not currently supported.
227+
!!! error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
228228
use(x);
229229
}
230230
use(x);
@@ -233,7 +233,7 @@ tests/cases/compiler/downlevelLetConst16.ts(224,5): error TS9003: 'for...of' sta
233233
function foo11() {
234234
for (const [x] of []) {
235235
~~~
236-
!!! error TS9003: 'for...of' statements are not currently supported.
236+
!!! error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
237237
use(x);
238238
}
239239
use(x);
@@ -242,7 +242,7 @@ tests/cases/compiler/downlevelLetConst16.ts(224,5): error TS9003: 'for...of' sta
242242
function foo12() {
243243
for (const {a: x} of []) {
244244
~~~
245-
!!! error TS9003: 'for...of' statements are not currently supported.
245+
!!! error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
246246
use(x);
247247
}
248248
use(x);

tests/baselines/reference/downlevelLetConst17.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/downlevelLetConst17.ts(66,1): error TS9003: 'for...of' statements are not currently supported.
1+
tests/cases/compiler/downlevelLetConst17.ts(66,1): error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
22

33

44
==== tests/cases/compiler/downlevelLetConst17.ts (1 errors) ====
@@ -69,6 +69,6 @@ tests/cases/compiler/downlevelLetConst17.ts(66,1): error TS9003: 'for...of' stat
6969
// TODO: update once for-of statements are supported downlevel
7070
for (const x of []) {
7171
~~~
72-
!!! error TS9003: 'for...of' statements are not currently supported.
72+
!!! error TS2482: 'for...of' statements are only available when targeting ECMAScript 6 or higher.
7373
use(x);
7474
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
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.
2+
3+
4+
==== tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring.ts (1 errors) ====
5+
for (var [a, b] in []) {}
6+
~~~~~~
7+
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [for-inStatementsDestructuring.ts]
2+
for (var [a, b] in []) {}
3+
4+
//// [for-inStatementsDestructuring.js]
5+
for (var _a = void 0, a = _a[0], b = _a[1] in []) { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
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.
2+
3+
4+
==== tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring2.ts (1 errors) ====
5+
for (var {a, b} in []) {}
6+
~~~~~~
7+
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [for-inStatementsDestructuring2.ts]
2+
for (var {a, b} in []) {}
3+
4+
//// [for-inStatementsDestructuring2.js]
5+
for (var _a = void 0, a = _a.a, b = _a.b in []) { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
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.
2+
3+
4+
==== tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring3.ts (1 errors) ====
5+
var a, b;
6+
for ([a, b] in []) { }
7+
~~~~~~
8+
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [for-inStatementsDestructuring3.ts]
2+
var a, b;
3+
for ([a, b] in []) { }
4+
5+
//// [for-inStatementsDestructuring3.js]
6+
var a, b;
7+
for ([a, b] in []) { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
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.
2+
3+
4+
==== tests/cases/conformance/statements/for-inStatements/for-inStatementsDestructuring4.ts (1 errors) ====
5+
var a, b;
6+
for ({a, b} in []) { }
7+
~~~~~~
8+
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [for-inStatementsDestructuring4.ts]
2+
var a, b;
3+
for ({a, b} in []) { }
4+
5+
//// [for-inStatementsDestructuring4.js]
6+
var a, b;
7+
for ({ a: a, b: b } in []) { }

tests/baselines/reference/for-of1.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [for-of1.ts]
2+
var v;
3+
for (v of []) { }
4+
5+
//// [for-of1.js]
6+
var v;
7+
for (v of []) { }
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/es6/for-ofStatements/for-of1.ts ===
2+
var v;
3+
>v : any
4+
5+
for (v of []) { }
6+
>v : any
7+
>[] : undefined[]
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/es6/for-ofStatements/for-of10.ts(2,6): error TS2322: Type 'number' is not assignable to type 'string'.
2+
3+
4+
==== tests/cases/conformance/es6/for-ofStatements/for-of10.ts (1 errors) ====
5+
var v: string;
6+
for (v of [0]) { }
7+
~
8+
!!! error TS2322: Type 'number' is not assignable to type 'string'.

tests/baselines/reference/for-of10.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [for-of10.ts]
2+
var v: string;
3+
for (v of [0]) { }
4+
5+
//// [for-of10.js]
6+
var v;
7+
for (v of [0]) { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/conformance/es6/for-ofStatements/for-of11.ts(2,6): error TS2322: Type 'string | number' is not assignable to type 'string'.
2+
Type 'number' is not assignable to type 'string'.
3+
4+
5+
==== tests/cases/conformance/es6/for-ofStatements/for-of11.ts (1 errors) ====
6+
var v: string;
7+
for (v of [0, ""]) { }
8+
~
9+
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
10+
!!! error TS2322: Type 'number' is not assignable to type 'string'.

tests/baselines/reference/for-of11.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [for-of11.ts]
2+
var v: string;
3+
for (v of [0, ""]) { }
4+
5+
//// [for-of11.js]
6+
var v;
7+
for (v of [0, ""]) { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/conformance/es6/for-ofStatements/for-of12.ts(2,6): error TS2322: Type 'string | number' is not assignable to type 'string'.
2+
Type 'number' is not assignable to type 'string'.
3+
4+
5+
==== tests/cases/conformance/es6/for-ofStatements/for-of12.ts (1 errors) ====
6+
var v: string;
7+
for (v of [0, ""].values()) { }
8+
~
9+
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
10+
!!! error TS2322: Type 'number' is not assignable to type 'string'.

tests/baselines/reference/for-of12.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [for-of12.ts]
2+
var v: string;
3+
for (v of [0, ""].values()) { }
4+
5+
//// [for-of12.js]
6+
var v;
7+
for (v of [0, ""].values()) { }

tests/baselines/reference/for-of13.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [for-of13.ts]
2+
var v: string;
3+
for (v of [""].values()) { }
4+
5+
//// [for-of13.js]
6+
var v;
7+
for (v of [""].values()) { }
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/conformance/es6/for-ofStatements/for-of13.ts ===
2+
var v: string;
3+
>v : string
4+
5+
for (v of [""].values()) { }
6+
>v : string
7+
>[""].values() : IterableIterator<string>
8+
>[""].values : () => IterableIterator<string>
9+
>[""] : string[]
10+
>values : () => IterableIterator<string>
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
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.
2+
3+
4+
==== tests/cases/conformance/es6/for-ofStatements/for-of14.ts (1 errors) ====
5+
var v: string;
6+
for (v of new StringIterator) { } // Should fail because the iterator is not iterable
7+
~~~~~~~~~~~~~~~~~~
8+
!!! error TS2488: The right-hand side of a 'for...of' statement must have a '[Symbol.iterator]()' method that returns an iterator.
9+
10+
class StringIterator {
11+
next() {
12+
return "";
13+
}
14+
}

tests/baselines/reference/for-of14.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [for-of14.ts]
2+
var v: string;
3+
for (v of new StringIterator) { } // Should fail because the iterator is not iterable
4+
5+
class StringIterator {
6+
next() {
7+
return "";
8+
}
9+
}
10+
11+
//// [for-of14.js]
12+
var v;
13+
for (v of new StringIterator) { } // Should fail because the iterator is not iterable
14+
var StringIterator = (function () {
15+
function StringIterator() {
16+
}
17+
StringIterator.prototype.next = function () {
18+
return "";
19+
};
20+
return StringIterator;
21+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/conformance/es6/for-ofStatements/for-of15.ts(2,11): error TS2490: The type returned by the 'next()' method of an iterator must have a 'value' property.
2+
3+
4+
==== tests/cases/conformance/es6/for-ofStatements/for-of15.ts (1 errors) ====
5+
var v: string;
6+
for (v of new StringIterator) { } // Should fail
7+
~~~~~~~~~~~~~~~~~~
8+
!!! error TS2490: The type returned by the 'next()' method of an iterator must have a 'value' property.
9+
10+
class StringIterator {
11+
next() {
12+
return "";
13+
}
14+
[Symbol.iterator]() {
15+
return this;
16+
}
17+
}

0 commit comments

Comments
 (0)