Skip to content

Commit faac853

Browse files
committed
Address PR feedback for generators
1 parent 33f8e56 commit faac853

File tree

3 files changed

+60
-57
lines changed

3 files changed

+60
-57
lines changed

src/compiler/checker.ts

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9725,42 +9725,43 @@ module ts {
97259725

97269726
return elementType || anyType;
97279727
}
9728-
9729-
function getElementTypeOfIterable(iterable: Type, errorNode: Node): Type {
9730-
// We want to treat type as an iterable, and get the type it is an iterable of. The iterable
9731-
// must have the following structure (annotated with the names of the variables below):
9732-
//
9733-
// { // iterable
9734-
// [Symbol.iterator]: { // iteratorFunction
9735-
// (): Iterator<T>
9736-
// }
9737-
// }
9738-
//
9739-
// T is the type we are after. At every level that involves analyzing return types
9740-
// of signatures, we union the return types of all the signatures.
9741-
//
9742-
// Another thing to note is that at any step of this process, we could run into a dead end,
9743-
// meaning either the property is missing, or we run into the anyType. If either of these things
9744-
// happens, we return undefined to signal that we could not find the iterated type. If a property
9745-
// is missing, and the previous step did not result in 'any', then we also give an error if the
9746-
// caller requested it. Then the caller can decide what to do in the case where there is no iterated
9747-
// type. This is different from returning anyType, because that would signify that we have matched the
9748-
// whole pattern and that T (above) is 'any'.
9749-
9750-
if (allConstituentTypesHaveKind(iterable, TypeFlags.Any)) {
9728+
9729+
/**
9730+
* We want to treat type as an iterable, and get the type it is an iterable of. The iterable
9731+
* must have the following structure (annotated with the names of the variables below):
9732+
*
9733+
* { // iterable
9734+
* [Symbol.iterator]: { // iteratorFunction
9735+
* (): Iterator<T>
9736+
* }
9737+
* }
9738+
*
9739+
* T is the type we are after. At every level that involves analyzing return types
9740+
* of signatures, we union the return types of all the signatures.
9741+
*
9742+
* Another thing to note is that at any step of this process, we could run into a dead end,
9743+
* meaning either the property is missing, or we run into the anyType. If either of these things
9744+
* happens, we return undefined to signal that we could not find the iterated type. If a property
9745+
* is missing, and the previous step did not result in 'any', then we also give an error if the
9746+
* caller requested it. Then the caller can decide what to do in the case where there is no iterated
9747+
* type. This is different from returning anyType, because that would signify that we have matched the
9748+
* whole pattern and that T (above) is 'any'.
9749+
*/
9750+
function getElementTypeOfIterable(type: Type, errorNode: Node): Type {
9751+
if (type.flags & TypeFlags.Any) {
97519752
return undefined;
97529753
}
97539754

9754-
let typeAsIterable = <IterableOrIteratorType>iterable;
9755+
let typeAsIterable = <IterableOrIteratorType>type;
97559756
if (!typeAsIterable.iterableElementType) {
97569757
// As an optimization, if the type is instantiated directly using the globalIterableType (Iterable<number>),
97579758
// then just grab its type argument.
9758-
if ((iterable.flags & TypeFlags.Reference) && (<GenericType>iterable).target === globalIterableType) {
9759-
typeAsIterable.iterableElementType = (<GenericType>iterable).typeArguments[0];
9759+
if ((type.flags & TypeFlags.Reference) && (<GenericType>type).target === globalIterableType) {
9760+
typeAsIterable.iterableElementType = (<GenericType>type).typeArguments[0];
97609761
}
97619762
else {
9762-
let iteratorFunction = getTypeOfPropertyOfType(iterable, getPropertyNameForKnownSymbolName("iterator"));
9763-
if (iteratorFunction && allConstituentTypesHaveKind(iteratorFunction, TypeFlags.Any)) {
9763+
let iteratorFunction = getTypeOfPropertyOfType(type, getPropertyNameForKnownSymbolName("iterator"));
9764+
if (iteratorFunction && iteratorFunction.flags & TypeFlags.Any) {
97649765
return undefined;
97659766
}
97669767

@@ -9779,32 +9780,34 @@ module ts {
97799780
return typeAsIterable.iterableElementType;
97809781
}
97819782

9782-
function getElementTypeOfIterator(iterator: Type, errorNode: Node): Type {
9783-
// This function has very similar logic as getElementTypeOfIterable, except that it operates on
9784-
// Iterators instead of Iterables. Here is the structure:
9785-
//
9786-
// { // iterator
9787-
// next: { // iteratorNextFunction
9788-
// (): { // iteratorNextResult
9789-
// value: T // iteratorNextValue
9790-
// }
9791-
// }
9792-
// }
9793-
//
9794-
if (allConstituentTypesHaveKind(iterator, TypeFlags.Any)) {
9783+
/**
9784+
* This function has very similar logic as getElementTypeOfIterable, except that it operates on
9785+
* Iterators instead of Iterables. Here is the structure:
9786+
*
9787+
* { // iterator
9788+
* next: { // iteratorNextFunction
9789+
* (): { // iteratorNextResult
9790+
* value: T // iteratorNextValue
9791+
* }
9792+
* }
9793+
* }
9794+
*
9795+
*/
9796+
function getElementTypeOfIterator(type: Type, errorNode: Node): Type {
9797+
if (type.flags & TypeFlags.Any) {
97959798
return undefined;
97969799
}
97979800

9798-
let typeAsIterator = <IterableOrIteratorType>iterator;
9801+
let typeAsIterator = <IterableOrIteratorType>type;
97999802
if (!typeAsIterator.iteratorElementType) {
98009803
// As an optimization, if the type is instantiated directly using the globalIteratorType (Iterator<number>),
98019804
// then just grab its type argument.
9802-
if ((iterator.flags & TypeFlags.Reference) && (<GenericType>iterator).target === globalIteratorType) {
9803-
typeAsIterator.iteratorElementType = (<GenericType>iterator).typeArguments[0];
9805+
if ((type.flags & TypeFlags.Reference) && (<GenericType>type).target === globalIteratorType) {
9806+
typeAsIterator.iteratorElementType = (<GenericType>type).typeArguments[0];
98049807
}
98059808
else {
9806-
let iteratorNextFunction = getTypeOfPropertyOfType(iterator, "next");
9807-
if (iteratorNextFunction && allConstituentTypesHaveKind(iteratorNextFunction, TypeFlags.Any)) {
9809+
let iteratorNextFunction = getTypeOfPropertyOfType(type, "next");
9810+
if (iteratorNextFunction && iteratorNextFunction.flags & TypeFlags.Any) {
98089811
return undefined;
98099812
}
98109813

@@ -9817,7 +9820,7 @@ module ts {
98179820
}
98189821

98199822
let iteratorNextResult = getUnionType(map(iteratorNextFunctionSignatures, getReturnTypeOfSignature));
9820-
if (allConstituentTypesHaveKind(iteratorNextResult, TypeFlags.Any)) {
9823+
if (iteratorNextResult.flags & TypeFlags.Any) {
98219824
return undefined;
98229825
}
98239826

@@ -9836,19 +9839,19 @@ module ts {
98369839
return typeAsIterator.iteratorElementType;
98379840
}
98389841

9839-
function getElementTypeOfIterableIterator(iterableIterator: Type): Type {
9840-
if (allConstituentTypesHaveKind(iterableIterator, TypeFlags.Any)) {
9842+
function getElementTypeOfIterableIterator(type: Type): Type {
9843+
if (type.flags & TypeFlags.Any) {
98419844
return undefined;
98429845
}
98439846

98449847
// As an optimization, if the type is instantiated directly using the globalIterableIteratorType (IterableIterator<number>),
98459848
// then just grab its type argument.
9846-
if ((iterableIterator.flags & TypeFlags.Reference) && (<GenericType>iterableIterator).target === globalIterableIteratorType) {
9847-
return (<GenericType>iterableIterator).typeArguments[0];
9849+
if ((type.flags & TypeFlags.Reference) && (<GenericType>type).target === globalIterableIteratorType) {
9850+
return (<GenericType>type).typeArguments[0];
98489851
}
98499852

9850-
return getElementTypeOfIterable(iterableIterator, /*errorNode*/ undefined) ||
9851-
getElementTypeOfIterator(iterableIterator, /*errorNode*/ undefined);
9853+
return getElementTypeOfIterable(type, /*errorNode*/ undefined) ||
9854+
getElementTypeOfIterator(type, /*errorNode*/ undefined);
98529855
}
98539856

98549857
/**

src/compiler/parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4006,9 +4006,9 @@ module ts {
40064006
property.questionToken = questionToken;
40074007
property.type = parseTypeAnnotation();
40084008

4009-
// For initializers, we always want to allow 'in' expressions. For instance properties specifically,
4010-
// since they are evaluated inside the constructor, we do *not* want to parse yield expressions,
4011-
// so we specifically turn the yield context off. The grammar would look something like this:
4009+
// For instance properties specifically, since they are evaluated inside the constructor,
4010+
// we do *not * want to parse yield expressions, so we specifically turn the yield context
4011+
// off. The grammar would look something like this:
40124012
//
40134013
// MemberVariableDeclaration[Yield]:
40144014
// AccessibilityModifier_opt PropertyName TypeAnnotation_opt Initialiser_opt[In];

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1543,8 +1543,8 @@ module ts {
15431543
numberIndexType: Type; // Numeric index type
15441544
}
15451545

1546-
/* @internal */
15471546
// Just a place to cache element types of iterables and iterators
1547+
/* @internal */
15481548
export interface IterableOrIteratorType extends ObjectType, UnionType {
15491549
iterableElementType?: Type;
15501550
iteratorElementType?: Type;

0 commit comments

Comments
 (0)