Skip to content

Commit e1f8be5

Browse files
committed
Merge checkIteratedType* functions
1 parent 5ca6665 commit e1f8be5

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3211,7 +3211,7 @@ namespace ts {
32113211
// This elementType will be used if the specific property corresponding to this index is not
32123212
// present (aka the tuple element property). This call also checks that the parentType is in
32133213
// fact an iterable or array (depending on target language).
3214-
const elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false);
3214+
const elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false, /*allowAsyncIterable*/ false);
32153215
if (declaration.dotDotDotToken) {
32163216
// Rest element has an array type with the same element type as the parent type
32173217
type = createArrayType(elementType);
@@ -9512,12 +9512,12 @@ namespace ts {
95129512

95139513
function getTypeOfDestructuredArrayElement(type: Type, index: number) {
95149514
return isTupleLikeType(type) && getTypeOfPropertyOfType(type, "" + index) ||
9515-
checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) ||
9515+
checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterable*/ false) ||
95169516
unknownType;
95179517
}
95189518

95199519
function getTypeOfDestructuredSpreadExpression(type: Type) {
9520-
return createArrayType(checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || unknownType);
9520+
return createArrayType(checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterable*/ false) || unknownType);
95219521
}
95229522

95239523
function getAssignedTypeOfBinaryExpression(node: BinaryExpression): Type {
@@ -11410,7 +11410,7 @@ namespace ts {
1141011410
const index = indexOf(arrayLiteral.elements, node);
1141111411
return getTypeOfPropertyOfContextualType(type, "" + index)
1141211412
|| getIndexTypeOfContextualType(type, IndexKind.Number)
11413-
|| getIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*checkAssignability*/ false);
11413+
|| getIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterable*/ false, /*checkAssignability*/ false);
1141411414
}
1141511415
return undefined;
1141611416
}
@@ -11628,7 +11628,7 @@ namespace ts {
1162811628
}
1162911629

1163011630
const arrayOrIterableType = checkExpression(node.expression, contextualMapper);
11631-
return checkIteratedTypeOrElementType(arrayOrIterableType, node.expression, /*allowStringInput*/ false);
11631+
return checkIteratedTypeOrElementType(arrayOrIterableType, node.expression, /*allowStringInput*/ false, /*allowAsyncIterable*/ false);
1163211632
}
1163311633

1163411634
function hasDefaultValue(node: BindingElement | Expression): boolean {
@@ -11657,7 +11657,7 @@ namespace ts {
1165711657
// if there is no index type / iterated type.
1165811658
const restArrayType = checkExpression((<SpreadElement>e).expression, contextualMapper);
1165911659
const restElementType = getIndexTypeOfType(restArrayType, IndexKind.Number) ||
11660-
getIteratedTypeOrElementType(restArrayType, /*errorNode*/ undefined, /*allowStringInput*/ false, /*checkAssignability*/ false);
11660+
getIteratedTypeOrElementType(restArrayType, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterable*/ false, /*checkAssignability*/ false);
1166111661
if (restElementType) {
1166211662
elementTypes.push(restElementType);
1166311663
}
@@ -14359,9 +14359,7 @@ namespace ts {
1435914359
let type = checkExpressionCached(expr, contextualMapper);
1436014360
if (yieldExpression.asteriskToken) {
1436114361
// A yield* expression effectively yields everything that its operand yields
14362-
type = functionFlags & FunctionFlags.Async
14363-
? checkIteratedTypeOfAsyncIterable(type, yieldExpression.expression) // AsyncGenerator function
14364-
: checkIteratedTypeOrElementType(type, yieldExpression.expression, /*allowStringInput*/ false); // Generator function
14362+
type = checkIteratedTypeOrElementType(type, yieldExpression.expression, /*allowStringInput*/ false, (functionFlags & FunctionFlags.Async) !== 0);
1436514363
}
1436614364
if (!contains(aggregatedTypes, type)) {
1436714365
aggregatedTypes.push(type);
@@ -14914,7 +14912,7 @@ namespace ts {
1491414912
// This elementType will be used if the specific property corresponding to this index is not
1491514913
// present (aka the tuple element property). This call also checks that the parentType is in
1491614914
// fact an iterable or array (depending on target language).
14917-
const elementType = checkIteratedTypeOrElementType(sourceType, node, /*allowStringInput*/ false) || unknownType;
14915+
const elementType = checkIteratedTypeOrElementType(sourceType, node, /*allowStringInput*/ false, /*allowAsyncIterable*/ false) || unknownType;
1491814916
const elements = node.elements;
1491914917
for (let i = 0; i < elements.length; i++) {
1492014918
checkArrayLiteralDestructuringElementAssignment(node, sourceType, i, elementType, contextualMapper);
@@ -15339,9 +15337,7 @@ namespace ts {
1533915337
let expressionElementType: Type;
1534015338
const nodeIsYieldStar = !!node.asteriskToken;
1534115339
if (nodeIsYieldStar) {
15342-
expressionElementType = functionFlags & FunctionFlags.Async
15343-
? checkIteratedTypeOfAsyncIterable(expressionType, node.expression) // AsyncGenerator function
15344-
: checkIteratedTypeOrElementType(expressionType, node.expression, /*allowStringInput*/ false); // Generator function
15340+
expressionElementType = checkIteratedTypeOrElementType(expressionType, node.expression, /*allowStringInput*/ false, (functionFlags & FunctionFlags.Async) !== 0);
1534515341
}
1534615342

1534715343
// There is no point in doing an assignability check if the function
@@ -17936,41 +17932,32 @@ namespace ts {
1793617932

1793717933
function checkRightHandSideOfForOf(rhsExpression: Expression, awaitModifier: AwaitKeywordToken | undefined): Type {
1793817934
const expressionType = checkNonNullExpression(rhsExpression);
17939-
return awaitModifier
17940-
? checkIteratedTypeOfAsyncIterable(expressionType, rhsExpression)
17941-
: checkIteratedTypeOrElementType(expressionType, rhsExpression, /*allowStringInput*/ true);
17935+
return checkIteratedTypeOrElementType(expressionType, rhsExpression, /*allowStringInput*/ true, awaitModifier !== undefined);
1794217936
}
1794317937

17944-
function checkIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean): Type {
17938+
function checkIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean, allowAsyncIterable: boolean): Type {
1794517939
if (isTypeAny(inputType)) {
1794617940
return inputType;
1794717941
}
1794817942

17949-
return getIteratedTypeOrElementType(inputType, errorNode, allowStringInput, /*checkAssignability*/ true) || anyType;
17950-
}
17951-
17952-
function checkIteratedTypeOfAsyncIterable(inputType: Type, errorNode: Node): Type {
17953-
if (isTypeAny(inputType)) {
17954-
return inputType;
17955-
}
17956-
17957-
return getIteratedTypeOfIterable(inputType, errorNode, /*isAsyncIterable*/ true, /*allowNonAsyncIterables*/ true, /*checkAssignability*/ true) || anyType;
17943+
return getIteratedTypeOrElementType(inputType, errorNode, allowStringInput, allowAsyncIterable, /*checkAssignability*/ true) || anyType;
1795817944
}
1795917945

1796017946
/**
1796117947
* When consuming an iterable type in a for..of, spread, or iterator destructuring assignment
1796217948
* we want to get the iterated type of an iterable for ES2015 or later, or the iterated type
1796317949
* of a iterable (if defined globally) or element type of an array like for ES2015 or earlier.
1796417950
*/
17965-
function getIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean, checkAssignability: boolean): Type {
17951+
function getIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean, allowAsyncIterable: boolean, checkAssignability: boolean): Type {
1796617952
const uplevelIteration = languageVersion >= ScriptTarget.ES2015;
1796717953
const downlevelIteration = !uplevelIteration && compilerOptions.downlevelIteration;
1796817954

1796917955
// Get the iterated type of an `Iterable<T>` or `IterableIterator<T>` only in ES2015
17970-
// or higher, or when downlevelIteration is supplied.
17971-
if (uplevelIteration || downlevelIteration) {
17956+
// or higher, when inside of an async generator or for-await-if, or when
17957+
// downlevelIteration is requested.
17958+
if (uplevelIteration || downlevelIteration || allowAsyncIterable) {
1797217959
// We only report errors for an invalid iterable type in ES2015 or higher.
17973-
const iteratedType = getIteratedTypeOfIterable(inputType, uplevelIteration ? errorNode : undefined, /*isAsyncIterable*/ false, /*allowNonAsyncIterables*/ false, checkAssignability);
17960+
const iteratedType = getIteratedTypeOfIterable(inputType, uplevelIteration ? errorNode : undefined, allowAsyncIterable, allowAsyncIterable, checkAssignability);
1797417961
if (iteratedType || uplevelIteration) {
1797517962
return iteratedType;
1797617963
}
@@ -20458,7 +20445,7 @@ namespace ts {
2045820445
Debug.assert(expr.parent.kind === SyntaxKind.ArrayLiteralExpression);
2045920446
// [{ property1: p1, property2 }] = elems;
2046020447
const typeOfArrayLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>expr.parent);
20461-
const elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false) || unknownType;
20448+
const elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false, /*allowAsyncIterable*/ false) || unknownType;
2046220449
return checkArrayLiteralDestructuringElementAssignment(<ArrayLiteralExpression>expr.parent, typeOfArrayLiteral,
2046320450
indexOf((<ArrayLiteralExpression>expr.parent).elements, expr), elementType || unknownType);
2046420451
}

0 commit comments

Comments
 (0)