|
| 1 | +import ts from "typescript"; |
| 2 | +import { Context, type NodeParser } from "../NodeParser.js"; |
| 3 | +import type { SubNodeParser } from "../SubNodeParser.js"; |
| 4 | +import { AliasType } from "../Type/AliasType.js"; |
| 5 | +import type { BaseType } from "../Type/BaseType.js"; |
| 6 | +import { DefinitionType } from "../Type/DefinitionType.js"; |
| 7 | +import { getKey } from "../Utils/nodeKey.js"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Needs to be registered before 261, 260, 230, 262 node kinds |
| 11 | + */ |
| 12 | +export class PromiseNodeParser implements SubNodeParser { |
| 13 | + public constructor( |
| 14 | + protected typeChecker: ts.TypeChecker, |
| 15 | + protected childNodeParser: NodeParser, |
| 16 | + ) {} |
| 17 | + |
| 18 | + public supportsNode(node: ts.Node): boolean { |
| 19 | + if ( |
| 20 | + // 261 interface PromiseInterface extends Promise<T> |
| 21 | + !ts.isInterfaceDeclaration(node) && |
| 22 | + // 260 class PromiseClass implements Promise<T> |
| 23 | + !ts.isClassDeclaration(node) && |
| 24 | + // 230 Promise<T> |
| 25 | + !ts.isExpressionWithTypeArguments(node) && |
| 26 | + // 262 type PromiseAlias = Promise<T>; |
| 27 | + !ts.isTypeAliasDeclaration(node) |
| 28 | + ) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + const type = this.typeChecker.getTypeAtLocation(node); |
| 33 | + |
| 34 | + const awaitedType = this.typeChecker.getAwaitedType(type); |
| 35 | + |
| 36 | + // ignores non awaitable types |
| 37 | + if (!awaitedType) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + // If the awaited type differs from the original type, the type extends promise |
| 42 | + // Awaited<Promise<T>> -> T (Promise<T> !== T) |
| 43 | + // Awaited<Y> -> Y (Y === Y) |
| 44 | + if (awaitedType === type) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + // In types like: A<T> = T, type C = A<1>, C has the same type as A<1> and 1, |
| 49 | + // the awaitedType is NOT the same reference as the type, so a assignability |
| 50 | + // check is needed |
| 51 | + return ( |
| 52 | + !this.typeChecker.isTypeAssignableTo(type, awaitedType) && |
| 53 | + !this.typeChecker.isTypeAssignableTo(awaitedType, type) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public createType( |
| 58 | + node: ts.InterfaceDeclaration | ts.ClassDeclaration | ts.ExpressionWithTypeArguments | ts.TypeAliasDeclaration, |
| 59 | + context: Context, |
| 60 | + ): BaseType { |
| 61 | + const type = this.typeChecker.getTypeAtLocation(node); |
| 62 | + const awaitedType = this.typeChecker.getAwaitedType(type)!; // supportsNode ensures this |
| 63 | + const awaitedNode = this.typeChecker.typeToTypeNode(awaitedType, undefined, ts.NodeBuilderFlags.IgnoreErrors); |
| 64 | + |
| 65 | + if (!awaitedNode) { |
| 66 | + throw new Error( |
| 67 | + `Could not find awaited node for type ${node.pos === -1 ? "<unresolved>" : node.getText()}`, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + const baseNode = this.childNodeParser.createType(awaitedNode, new Context(node)); |
| 72 | + |
| 73 | + const name = this.getNodeName(node); |
| 74 | + |
| 75 | + // Nodes without name should just be their awaited type |
| 76 | + // export class extends Promise<T> {} -> T |
| 77 | + // export class A extends Promise<T> {} -> A (ref to T) |
| 78 | + if (!name) { |
| 79 | + return baseNode; |
| 80 | + } |
| 81 | + |
| 82 | + return new DefinitionType(name, new AliasType(`promise-${getKey(node, context)}`, baseNode)); |
| 83 | + } |
| 84 | + |
| 85 | + private getNodeName( |
| 86 | + node: ts.InterfaceDeclaration | ts.ClassDeclaration | ts.ExpressionWithTypeArguments | ts.TypeAliasDeclaration, |
| 87 | + ) { |
| 88 | + if (ts.isExpressionWithTypeArguments(node)) { |
| 89 | + if (!ts.isHeritageClause(node.parent)) { |
| 90 | + throw new Error("Expected ExpressionWithTypeArguments to have a HeritageClause parent"); |
| 91 | + } |
| 92 | + |
| 93 | + return node.parent.parent.name?.getText(); |
| 94 | + } |
| 95 | + |
| 96 | + return node.name?.getText(); |
| 97 | + } |
| 98 | +} |
0 commit comments