|
| 1 | +import { |
| 2 | + getIteratorYieldResultFromIteratorResult, |
| 3 | + getPropertyOfType, |
| 4 | + isCompilerOptionEnabled, |
| 5 | + isIntersectionType, |
| 6 | + isModifierFlagSet, |
| 7 | + isSymbolFlagSet, |
| 8 | + isTypeFlagSet, |
| 9 | + isTypeReference, |
| 10 | + isUnionType, |
| 11 | + removeOptionalityFromType, |
| 12 | + someTypePart, |
| 13 | + unionTypeParts, |
| 14 | +} from 'tsutils'; |
| 15 | +import * as ts from 'typescript'; |
| 16 | +import { tryGetBaseConstraintType, typesAreEqual } from './utils'; |
| 17 | + |
| 18 | +function isIterationProtocolAvailable(compilerOptions: ts.CompilerOptions) { |
| 19 | + return compilerOptions.target! >= ts.ScriptTarget.ES2015 || isCompilerOptionEnabled(compilerOptions, 'downlevelIteration'); |
| 20 | +} |
| 21 | + |
| 22 | +export function isExpressionIterable( |
| 23 | + node: ts.Expression, |
| 24 | + checker: ts.TypeChecker, |
| 25 | + compilerOptions: ts.CompilerOptions, |
| 26 | + matchIndexSignature: boolean, |
| 27 | +): boolean { |
| 28 | + const type = checker.getTypeAtLocation(node); |
| 29 | + return isIterationProtocolAvailable(compilerOptions) |
| 30 | + ? isIterable(checker.getApparentType(type), checker, node, matchIndexSignature) |
| 31 | + : unionTypeParts(tryGetBaseConstraintType(type, checker)).every((t) => isArrayLike(t, compilerOptions)); |
| 32 | +} |
| 33 | + |
| 34 | +function isArrayLike(type: ts.Type, compilerOptions: ts.CompilerOptions): boolean { |
| 35 | + if (isTypeReference(type)) |
| 36 | + type = type.target; |
| 37 | + if (type.getNumberIndexType() === undefined) |
| 38 | + return false; |
| 39 | + if (type.flags & ts.TypeFlags.StringLike) |
| 40 | + return compilerOptions.target! >= ts.ScriptTarget.ES5; // iterating string is only possible starting from ES5 |
| 41 | + if (type.symbol !== undefined && /^(?:Readonly)?Array$/.test(<string>type.symbol.escapedName) && |
| 42 | + type.symbol.declarations?.some((node) => node.getSourceFile().hasNoDefaultLib)) |
| 43 | + return true; |
| 44 | + if (isIntersectionType(type)) |
| 45 | + return type.types.some((t) => isArrayLike(t, compilerOptions)); |
| 46 | + return !!type.getBaseTypes()?.some((t) => isArrayLike(t, compilerOptions)); |
| 47 | +} |
| 48 | + |
| 49 | +function isIterable(type: ts.Type, checker: ts.TypeChecker, node: ts.Node, matchIndexSignature: boolean): boolean { |
| 50 | + const indexType = type.getNumberIndexType() || type.getStringIndexType(); |
| 51 | + if (indexType === undefined && matchIndexSignature) |
| 52 | + return false; |
| 53 | + const iteratorFn = getPropertyOfType(type, <ts.__String>'__@iterator'); |
| 54 | + if (!isPresentPublicAndRequired(iteratorFn)) |
| 55 | + return false; |
| 56 | + return checkReturnTypeAndRequireZeroArity(checker.getTypeOfSymbolAtLocation(iteratorFn, node), (iterator) => { |
| 57 | + const next = iterator.getProperty('next'); |
| 58 | + return isPresentPublicAndRequired(next) && |
| 59 | + checkReturnTypeAndRequireZeroArity(checker.getTypeOfSymbolAtLocation(next, node), (iteratorResult) => { |
| 60 | + const done = iteratorResult.getProperty('done'); |
| 61 | + if ( |
| 62 | + !isPresentAndPublic(done) || |
| 63 | + someTypePart( |
| 64 | + removeOptionalityFromType(checker, checker.getTypeOfSymbolAtLocation(done, node)), |
| 65 | + isUnionType, |
| 66 | + (t) => !isTypeFlagSet(t, ts.TypeFlags.BooleanLike), |
| 67 | + ) |
| 68 | + ) |
| 69 | + return false; |
| 70 | + const value = getIteratorYieldResultFromIteratorResult(iteratorResult, node, checker).getProperty('value'); |
| 71 | + return isPresentAndPublic(value) && |
| 72 | + ( |
| 73 | + !matchIndexSignature || |
| 74 | + typesAreEqual(checker.getTypeOfSymbolAtLocation(value, node), indexType!, checker) |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +function checkReturnTypeAndRequireZeroArity(type: ts.Type, cb: (type: ts.Type) => boolean): boolean { |
| 82 | + let zeroArity = false; |
| 83 | + for (const signature of type.getCallSignatures()) { |
| 84 | + if (!cb(signature.getReturnType())) |
| 85 | + return false; |
| 86 | + if (signatureHasArityZero(signature)) |
| 87 | + zeroArity = true; |
| 88 | + } |
| 89 | + return zeroArity; |
| 90 | +} |
| 91 | + |
| 92 | +function signatureHasArityZero(signature: ts.Signature): boolean { |
| 93 | + if (signature.parameters.length === 0) |
| 94 | + return true; |
| 95 | + const decl = <ts.ParameterDeclaration | undefined>signature.parameters[0].declarations![0]; |
| 96 | + return decl !== undefined && isOptionalParameter(decl); |
| 97 | +} |
| 98 | + |
| 99 | +function isOptionalParameter(node: ts.ParameterDeclaration): boolean { |
| 100 | + if (node.questionToken !== undefined || node.dotDotDotToken !== undefined) |
| 101 | + return true; |
| 102 | + if (node.flags & ts.NodeFlags.JavaScriptFile && ts.getJSDocParameterTags(node).some((tag) => tag.isBracketed)) |
| 103 | + return true; |
| 104 | + if (node.initializer === undefined) |
| 105 | + return false; |
| 106 | + const parameters = node.parent!.parameters; |
| 107 | + const nextIndex = parameters.indexOf(node) + 1; |
| 108 | + if (nextIndex === parameters.length) |
| 109 | + return true; |
| 110 | + return isOptionalParameter(parameters[nextIndex]); |
| 111 | +} |
| 112 | + |
| 113 | +function isPresentPublicAndRequired(symbol: ts.Symbol | undefined): symbol is ts.Symbol { |
| 114 | + return isPresentAndPublic(symbol) && !isSymbolFlagSet(symbol, ts.SymbolFlags.Optional); |
| 115 | +} |
| 116 | + |
| 117 | +function isPresentAndPublic(symbol: ts.Symbol | undefined): symbol is ts.Symbol { |
| 118 | + return symbol !== undefined && |
| 119 | + ( |
| 120 | + symbol.declarations === undefined || |
| 121 | + symbol.declarations.every((d) => !isModifierFlagSet(d, ts.ModifierFlags.NonPublicAccessibilityModifier)) |
| 122 | + ); |
| 123 | +} |
0 commit comments