MCPcopy Index your code
hub / github.com/nodejs/node / getIterationTypesOfMethod

Function getIterationTypesOfMethod

test/fixtures/snapshot/typescript.js:83488–83574  ·  view source on GitHub ↗

* Gets the *yield*, *return*, and *next* types of a the `next()`, `return()`, or * `throw()` method of an `Iterator`-like or `AsyncIterator`-like type. * * If we successfully found the *yield*, *return*, and *next* types, an `IterationTypes` * record is returned.

(type, resolver, methodName, errorNode)

Source from the content-addressed store, hash-verified

83486 * record is returned. Otherwise, we return `undefined`.
83487 */
83488 function getIterationTypesOfMethod(type, resolver, methodName, errorNode) {
83489 var _a, _b, _c, _d;
83490 var method = getPropertyOfType(type, methodName);
83491 // Ignore 'return' or 'throw' if they are missing.
83492 if (!method && methodName !== "next") {
83493 return undefined;
83494 }
83495 var methodType = method && !(methodName === "next" && (method.flags & 16777216 /* SymbolFlags.Optional */))
83496 ? methodName === "next" ? getTypeOfSymbol(method) : getTypeWithFacts(getTypeOfSymbol(method), 2097152 /* TypeFacts.NEUndefinedOrNull */)
83497 : undefined;
83498 if (isTypeAny(methodType)) {
83499 // `return()` and `throw()` don't provide a *next* type.
83500 return methodName === "next" ? anyIterationTypes : anyIterationTypesExceptNext;
83501 }
83502 // Both async and non-async iterators *must* have a `next` method.
83503 var methodSignatures = methodType ? getSignaturesOfType(methodType, 0 /* SignatureKind.Call */) : ts.emptyArray;
83504 if (methodSignatures.length === 0) {
83505 if (errorNode) {
83506 var diagnostic = methodName === "next"
83507 ? resolver.mustHaveANextMethodDiagnostic
83508 : resolver.mustBeAMethodDiagnostic;
83509 error(errorNode, diagnostic, methodName);
83510 }
83511 return methodName === "next" ? anyIterationTypes : undefined;
83512 }
83513 // If the method signature comes exclusively from the global iterator or generator type,
83514 // create iteration types from its type arguments like `getIterationTypesOfIteratorFast`
83515 // does (so as to remove `undefined` from the next and return types). We arrive here when
83516 // a contextual type for a generator was not a direct reference to one of those global types,
83517 // but looking up `methodType` referred to one of them (and nothing else). E.g., in
83518 // `interface SpecialIterator extends Iterator<number> {}`, `SpecialIterator` is not a
83519 // reference to `Iterator`, but its `next` member derives exclusively from `Iterator`.
83520 if ((methodType === null || methodType === void 0 ? void 0 : methodType.symbol) && methodSignatures.length === 1) {
83521 var globalGeneratorType = resolver.getGlobalGeneratorType(/*reportErrors*/ false);
83522 var globalIteratorType = resolver.getGlobalIteratorType(/*reportErrors*/ false);
83523 var isGeneratorMethod = ((_b = (_a = globalGeneratorType.symbol) === null || _a === void 0 ? void 0 : _a.members) === null || _b === void 0 ? void 0 : _b.get(methodName)) === methodType.symbol;
83524 var isIteratorMethod = !isGeneratorMethod && ((_d = (_c = globalIteratorType.symbol) === null || _c === void 0 ? void 0 : _c.members) === null || _d === void 0 ? void 0 : _d.get(methodName)) === methodType.symbol;
83525 if (isGeneratorMethod || isIteratorMethod) {
83526 var globalType = isGeneratorMethod ? globalGeneratorType : globalIteratorType;
83527 var mapper = methodType.mapper;
83528 return createIterationTypes(getMappedType(globalType.typeParameters[0], mapper), getMappedType(globalType.typeParameters[1], mapper), methodName === "next" ? getMappedType(globalType.typeParameters[2], mapper) : undefined);
83529 }
83530 }
83531 // Extract the first parameter and return type of each signature.
83532 var methodParameterTypes;
83533 var methodReturnTypes;
83534 for (var _i = 0, methodSignatures_1 = methodSignatures; _i < methodSignatures_1.length; _i++) {
83535 var signature = methodSignatures_1[_i];
83536 if (methodName !== "throw" && ts.some(signature.parameters)) {
83537 methodParameterTypes = ts.append(methodParameterTypes, getTypeAtPosition(signature, 0));
83538 }
83539 methodReturnTypes = ts.append(methodReturnTypes, getReturnTypeOfSignature(signature));
83540 }
83541 // Resolve the *next* or *return* type from the first parameter of a `next()` or
83542 // `return()` method, respectively.
83543 var returnTypes;
83544 var nextType;
83545 if (methodName !== "throw") {

Callers 1

Calls 15

getPropertyOfTypeFunction · 0.85
getTypeOfSymbolFunction · 0.85
getTypeWithFactsFunction · 0.85
isTypeAnyFunction · 0.85
getSignaturesOfTypeFunction · 0.85
createIterationTypesFunction · 0.85
getMappedTypeFunction · 0.85
getTypeAtPositionFunction · 0.85
getReturnTypeOfSignatureFunction · 0.85
getUnionTypeFunction · 0.85
getIntersectionTypeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…