Merge pull request #22275 from delftswa2018/21617-detailed-error-forof-iterators

Fix #21617: Give detailed message on `for-of` of iterators without downlevelIteration
This commit is contained in:
Mohamed Hegazy
2018-03-29 15:43:00 -07:00
committed by GitHub
13 changed files with 130 additions and 4 deletions

View File

@@ -22701,13 +22701,18 @@ namespace ts {
// want to say that number is not an array type. But if the input was just
// number and string input is allowed, we want to say that number is not an
// array type or a string type.
const isIterable = !!getIteratedTypeOfIterable(inputType, /* errorNode */ undefined, allowAsyncIterables, /*allowSyncIterables*/ true, checkAssignability);
const diagnostic = !allowStringInput || hasStringConstituent
? downlevelIteration
? Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator
: Diagnostics.Type_0_is_not_an_array_type
: isIterable
? Diagnostics.Type_0_is_not_an_array_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators
: Diagnostics.Type_0_is_not_an_array_type
: downlevelIteration
? Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator
: Diagnostics.Type_0_is_not_an_array_type_or_a_string_type;
: isIterable
? Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators
: Diagnostics.Type_0_is_not_an_array_type_or_a_string_type;
error(errorNode, diagnostic, typeToString(arrayType));
}
return hasStringConstituent ? stringType : undefined;

View File

@@ -1992,6 +1992,14 @@
"category": "Error",
"code": 2567
},
"Type '{0}' is not an array type. Use compiler option '--downlevelIteration' to allow iterating of iterators.": {
"category": "Error",
"code": 2568
},
"Type '{0}' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.": {
"category": "Error",
"code": 2569
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",

View File

@@ -1,5 +1,5 @@
tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck10.ts(9,6): error TS2304: Cannot find name 'Symbol'.
tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck10.ts(14,15): error TS2495: Type 'StringIterator' is not an array type or a string type.
tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck10.ts(14,15): error TS2569: Type 'StringIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
==== tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck10.ts (2 errors) ====
@@ -20,4 +20,4 @@ tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck10.ts(14,1
for (var v of new StringIterator) { }
~~~~~~~~~~~~~~~~~~
!!! error TS2495: Type 'StringIterator' is not an array type or a string type.
!!! error TS2569: Type 'StringIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

View File

@@ -0,0 +1,10 @@
tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck13.ts(4,19): error TS2569: Type 'Set<string>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
==== tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck13.ts (1 errors) ====
const strSet: Set<string> = new Set()
strSet.add('Hello')
strSet.add('World')
for (const str of strSet) { }
~~~~~~
!!! error TS2569: Type 'Set<string>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

View File

@@ -0,0 +1,13 @@
//// [ES5For-ofTypeCheck13.ts]
const strSet: Set<string> = new Set()
strSet.add('Hello')
strSet.add('World')
for (const str of strSet) { }
//// [ES5For-ofTypeCheck13.js]
var strSet = new Set();
strSet.add('Hello');
strSet.add('World');
for (var _i = 0, strSet_1 = strSet; _i < strSet_1.length; _i++) {
var str = strSet_1[_i];
}

View File

@@ -0,0 +1,20 @@
=== tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck13.ts ===
const strSet: Set<string> = new Set()
>strSet : Symbol(strSet, Decl(ES5For-ofTypeCheck13.ts, 0, 5))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
strSet.add('Hello')
>strSet.add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
>strSet : Symbol(strSet, Decl(ES5For-ofTypeCheck13.ts, 0, 5))
>add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
strSet.add('World')
>strSet.add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
>strSet : Symbol(strSet, Decl(ES5For-ofTypeCheck13.ts, 0, 5))
>add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
for (const str of strSet) { }
>str : Symbol(str, Decl(ES5For-ofTypeCheck13.ts, 3, 10))
>strSet : Symbol(strSet, Decl(ES5For-ofTypeCheck13.ts, 0, 5))

View File

@@ -0,0 +1,25 @@
=== tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck13.ts ===
const strSet: Set<string> = new Set()
>strSet : Set<string>
>Set : Set<T>
>new Set() : Set<any>
>Set : SetConstructor
strSet.add('Hello')
>strSet.add('Hello') : Set<string>
>strSet.add : (value: string) => Set<string>
>strSet : Set<string>
>add : (value: string) => Set<string>
>'Hello' : "Hello"
strSet.add('World')
>strSet.add('World') : Set<string>
>strSet.add : (value: string) => Set<string>
>strSet : Set<string>
>add : (value: string) => Set<string>
>'World' : "World"
for (const str of strSet) { }
>str : any
>strSet : Set<string>

View File

@@ -0,0 +1,8 @@
tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck14.ts(2,17): error TS2568: Type 'Set<number>' is not an array type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
==== tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck14.ts (1 errors) ====
var union: string | Set<number>
for (const e of union) { }
~~~~~
!!! error TS2568: Type 'Set<number>' is not an array type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

View File

@@ -0,0 +1,9 @@
//// [ES5For-ofTypeCheck14.ts]
var union: string | Set<number>
for (const e of union) { }
//// [ES5For-ofTypeCheck14.js]
var union;
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
var e = union_1[_i];
}

View File

@@ -0,0 +1,9 @@
=== tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck14.ts ===
var union: string | Set<number>
>union : Symbol(union, Decl(ES5For-ofTypeCheck14.ts, 0, 3))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
for (const e of union) { }
>e : Symbol(e, Decl(ES5For-ofTypeCheck14.ts, 1, 10))
>union : Symbol(union, Decl(ES5For-ofTypeCheck14.ts, 0, 3))

View File

@@ -0,0 +1,9 @@
=== tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck14.ts ===
var union: string | Set<number>
>union : string | Set<number>
>Set : Set<T>
for (const e of union) { }
>e : string
>union : string | Set<number>

View File

@@ -0,0 +1,6 @@
//@target: ES5
//@lib: ES6
const strSet: Set<string> = new Set()
strSet.add('Hello')
strSet.add('World')
for (const str of strSet) { }

View File

@@ -0,0 +1,4 @@
//@target: ES5
//@lib: ES6
var union: string | Set<number>
for (const e of union) { }