Skip to content

Commit 5c48620

Browse files
committed
Move getIteratedType out of checkIteratedType
1 parent be5557a commit 5c48620

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

src/compiler/checker.ts

+74-74
Original file line numberDiff line numberDiff line change
@@ -5850,7 +5850,7 @@ module ts {
58505850
let index = indexOf(arrayLiteral.elements, node);
58515851
return getTypeOfPropertyOfContextualType(type, "" + index)
58525852
|| getIndexTypeOfContextualType(type, IndexKind.Number)
5853-
|| (languageVersion >= ScriptTarget.ES6 ? checkIteratedType(type, /*expressionForError*/ undefined) : undefined);
5853+
|| (languageVersion >= ScriptTarget.ES6 ? getIteratedType(type, /*expressionForError*/ undefined) : undefined);
58545854
}
58555855
return undefined;
58565856
}
@@ -6037,7 +6037,7 @@ module ts {
60376037
// if there is no index type / iterated type.
60386038
let restArrayType = checkExpression((<SpreadElementExpression>e).expression, contextualMapper);
60396039
let restElementType = getIndexTypeOfType(restArrayType, IndexKind.Number) ||
6040-
(languageVersion >= ScriptTarget.ES6 ? checkIteratedType(restArrayType, /*expressionForError*/ undefined) : undefined);
6040+
(languageVersion >= ScriptTarget.ES6 ? getIteratedType(restArrayType, /*expressionForError*/ undefined) : undefined);
60416041

60426042
if (restElementType) {
60436043
elementTypes.push(restElementType);
@@ -9469,7 +9469,6 @@ module ts {
94699469
* When errorNode is undefined, it means we should not report any errors.
94709470
*/
94719471
function checkIteratedType(iterable: Type, errorNode: Node): Type {
9472-
Debug.assert(languageVersion >= ScriptTarget.ES6);
94739472
let iteratedType = getIteratedType(iterable, errorNode);
94749473
// Now even though we have extracted the iteratedType, we will have to validate that the type
94759474
// passed in is actually an Iterable.
@@ -9478,90 +9477,91 @@ module ts {
94789477
}
94799478

94809479
return iteratedType;
9480+
}
94819481

9482-
function getIteratedType(iterable: Type, errorNode: Node) {
9483-
// We want to treat type as an iterable, and get the type it is an iterable of. The iterable
9484-
// must have the following structure (annotated with the names of the variables below):
9485-
//
9486-
// { // iterable
9487-
// [Symbol.iterator]: { // iteratorFunction
9488-
// (): { // iterator
9489-
// next: { // iteratorNextFunction
9490-
// (): { // iteratorNextResult
9491-
// value: T // iteratorNextValue
9492-
// }
9493-
// }
9494-
// }
9495-
// }
9496-
// }
9497-
//
9498-
// T is the type we are after. At every level that involves analyzing return types
9499-
// of signatures, we union the return types of all the signatures.
9500-
//
9501-
// Another thing to note is that at any step of this process, we could run into a dead end,
9502-
// meaning either the property is missing, or we run into the anyType. If either of these things
9503-
// happens, we return undefined to signal that we could not find the iterated type. If a property
9504-
// is missing, and the previous step did not result in 'any', then we also give an error if the
9505-
// caller requested it. Then the caller can decide what to do in the case where there is no iterated
9506-
// type. This is different from returning anyType, because that would signify that we have matched the
9507-
// whole pattern and that T (above) is 'any'.
9508-
9509-
if (allConstituentTypesHaveKind(iterable, TypeFlags.Any)) {
9510-
return undefined;
9511-
}
9482+
function getIteratedType(iterable: Type, errorNode: Node) {
9483+
Debug.assert(languageVersion >= ScriptTarget.ES6);
9484+
// We want to treat type as an iterable, and get the type it is an iterable of. The iterable
9485+
// must have the following structure (annotated with the names of the variables below):
9486+
//
9487+
// { // iterable
9488+
// [Symbol.iterator]: { // iteratorFunction
9489+
// (): { // iterator
9490+
// next: { // iteratorNextFunction
9491+
// (): { // iteratorNextResult
9492+
// value: T // iteratorNextValue
9493+
// }
9494+
// }
9495+
// }
9496+
// }
9497+
// }
9498+
//
9499+
// T is the type we are after. At every level that involves analyzing return types
9500+
// of signatures, we union the return types of all the signatures.
9501+
//
9502+
// Another thing to note is that at any step of this process, we could run into a dead end,
9503+
// meaning either the property is missing, or we run into the anyType. If either of these things
9504+
// happens, we return undefined to signal that we could not find the iterated type. If a property
9505+
// is missing, and the previous step did not result in 'any', then we also give an error if the
9506+
// caller requested it. Then the caller can decide what to do in the case where there is no iterated
9507+
// type. This is different from returning anyType, because that would signify that we have matched the
9508+
// whole pattern and that T (above) is 'any'.
9509+
9510+
if (allConstituentTypesHaveKind(iterable, TypeFlags.Any)) {
9511+
return undefined;
9512+
}
95129513

9513-
// As an optimization, if the type is instantiated directly using the globalIterableType (Iterable<number>),
9514-
// then just grab its type argument.
9515-
if ((iterable.flags & TypeFlags.Reference) && (<GenericType>iterable).target === globalIterableType) {
9516-
return (<GenericType>iterable).typeArguments[0];
9517-
}
9514+
// As an optimization, if the type is instantiated directly using the globalIterableType (Iterable<number>),
9515+
// then just grab its type argument.
9516+
if ((iterable.flags & TypeFlags.Reference) && (<GenericType>iterable).target === globalIterableType) {
9517+
return (<GenericType>iterable).typeArguments[0];
9518+
}
95189519

9519-
let iteratorFunction = getTypeOfPropertyOfType(iterable, getPropertyNameForKnownSymbolName("iterator"));
9520-
if (iteratorFunction && allConstituentTypesHaveKind(iteratorFunction, TypeFlags.Any)) {
9521-
return undefined;
9522-
}
9520+
let iteratorFunction = getTypeOfPropertyOfType(iterable, getPropertyNameForKnownSymbolName("iterator"));
9521+
if (iteratorFunction && allConstituentTypesHaveKind(iteratorFunction, TypeFlags.Any)) {
9522+
return undefined;
9523+
}
95239524

9524-
let iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, SignatureKind.Call) : emptyArray;
9525-
if (iteratorFunctionSignatures.length === 0) {
9526-
if (errorNode) {
9527-
error(errorNode, Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator);
9528-
}
9529-
return undefined;
9525+
let iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, SignatureKind.Call) : emptyArray;
9526+
if (iteratorFunctionSignatures.length === 0) {
9527+
if (errorNode) {
9528+
error(errorNode, Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator);
95309529
}
9530+
return undefined;
9531+
}
95319532

9532-
let iterator = getUnionType(map(iteratorFunctionSignatures, getReturnTypeOfSignature));
9533-
if (allConstituentTypesHaveKind(iterator, TypeFlags.Any)) {
9534-
return undefined;
9535-
}
9533+
let iterator = getUnionType(map(iteratorFunctionSignatures, getReturnTypeOfSignature));
9534+
if (allConstituentTypesHaveKind(iterator, TypeFlags.Any)) {
9535+
return undefined;
9536+
}
95369537

9537-
let iteratorNextFunction = getTypeOfPropertyOfType(iterator, "next");
9538-
if (iteratorNextFunction && allConstituentTypesHaveKind(iteratorNextFunction, TypeFlags.Any)) {
9539-
return undefined;
9540-
}
9538+
let iteratorNextFunction = getTypeOfPropertyOfType(iterator, "next");
9539+
if (iteratorNextFunction && allConstituentTypesHaveKind(iteratorNextFunction, TypeFlags.Any)) {
9540+
return undefined;
9541+
}
95419542

9542-
let iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, SignatureKind.Call) : emptyArray;
9543-
if (iteratorNextFunctionSignatures.length === 0) {
9544-
if (errorNode) {
9545-
error(errorNode, Diagnostics.An_iterator_must_have_a_next_method);
9546-
}
9547-
return undefined;
9543+
let iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, SignatureKind.Call) : emptyArray;
9544+
if (iteratorNextFunctionSignatures.length === 0) {
9545+
if (errorNode) {
9546+
error(errorNode, Diagnostics.An_iterator_must_have_a_next_method);
95489547
}
9548+
return undefined;
9549+
}
95499550

9550-
let iteratorNextResult = getUnionType(map(iteratorNextFunctionSignatures, getReturnTypeOfSignature));
9551-
if (allConstituentTypesHaveKind(iteratorNextResult, TypeFlags.Any)) {
9552-
return undefined;
9553-
}
9551+
let iteratorNextResult = getUnionType(map(iteratorNextFunctionSignatures, getReturnTypeOfSignature));
9552+
if (allConstituentTypesHaveKind(iteratorNextResult, TypeFlags.Any)) {
9553+
return undefined;
9554+
}
95549555

9555-
let iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value");
9556-
if (!iteratorNextValue) {
9557-
if (errorNode) {
9558-
error(errorNode, Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property);
9559-
}
9560-
return undefined;
9556+
let iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value");
9557+
if (!iteratorNextValue) {
9558+
if (errorNode) {
9559+
error(errorNode, Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property);
95619560
}
9562-
9563-
return iteratorNextValue;
9561+
return undefined;
95649562
}
9563+
9564+
return iteratorNextValue;
95659565
}
95669566

95679567
/**

0 commit comments

Comments
 (0)