|
| 1 | +import { |
| 2 | + ArrayType, |
| 3 | + ConditionalType, |
| 4 | + IndexedAccessType, |
| 5 | + IntersectionType, |
| 6 | + IntrinsicType, |
| 7 | + PredicateType, |
| 8 | + ProjectReflection, |
| 9 | + ReferenceType, |
| 10 | + Reflection, |
| 11 | + ReflectionKind, |
| 12 | + SignatureReflection, |
| 13 | + TupleType, |
| 14 | + Type, |
| 15 | + TypeOperatorType, |
| 16 | + UnionType, |
| 17 | + UnknownType, |
| 18 | +} from 'typedoc/dist/lib/models'; |
| 19 | +import { DeclarationReflection } from 'typedoc'; |
| 20 | + |
| 21 | +export default class UnresolvedTypesMapper { |
| 22 | + private _project: ProjectReflection; |
| 23 | + private _knownReflectionNames: Set<string>; |
| 24 | + private _knownReflections: Set<Reflection>; |
| 25 | + private _currentReflections: Set<Reflection>; |
| 26 | + |
| 27 | + constructor(project: ProjectReflection) { |
| 28 | + this._project = project; |
| 29 | + this._knownReflectionNames = new Set<string>(); |
| 30 | + this._knownReflections = new Set<Reflection>(); |
| 31 | + this._currentReflections = new Set<Reflection>(); |
| 32 | + } |
| 33 | + |
| 34 | + public clearKnownReflections() { |
| 35 | + this._knownReflectionNames.clear(); |
| 36 | + this._knownReflections.clear(); |
| 37 | + } |
| 38 | + |
| 39 | + public registerKnownReflection(reflection: Reflection) { |
| 40 | + this._knownReflections.add(reflection); |
| 41 | + if (reflection.kindOf(ReflectionKind.ClassOrInterface | ReflectionKind.TypeAlias | ReflectionKind.SomeModule)) { |
| 42 | + this._knownReflectionNames.add(reflection.name); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public registerKnownReflections(reflections: Reflection[]) { |
| 47 | + reflections.forEach(reflection => { |
| 48 | + this.registerKnownReflection(reflection); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + public resolve() { |
| 53 | + this._currentReflections.clear(); |
| 54 | + Object.values(this._project.reflections).forEach(reflection => this._currentReflections.add(reflection)); |
| 55 | + |
| 56 | + this.getReflectionsByInstanceType(this._project, DeclarationReflection).forEach(reflection => { |
| 57 | + reflection.extendedTypes = reflection.extendedTypes?.filter(t => this.isMapped(t)); |
| 58 | + reflection.type = this.remapType(reflection.type); |
| 59 | + }); |
| 60 | + |
| 61 | + this.getReflectionsByInstanceType(this._project, SignatureReflection).forEach(reflection => { |
| 62 | + reflection.type = this.remapType(reflection.type); |
| 63 | + reflection.parameters?.forEach(p => p.type = this.remapType(p.type)); |
| 64 | + reflection.typeParameters?.forEach(p => p.type = this.remapType(p.type)); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + private isKnown(type: Type) { |
| 69 | + if (type instanceof ReferenceType) { |
| 70 | + return this._knownReflectionNames.has(type.name); |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + private isRemoved(reflection: Reflection) { |
| 76 | + return this._knownReflections.has(reflection) && !this._currentReflections.has(reflection); |
| 77 | + } |
| 78 | + |
| 79 | + private isUnmapped(type: Type | undefined) { |
| 80 | + return (type instanceof ReferenceType) |
| 81 | + && (!type.reflection || this.isRemoved(type.reflection)) |
| 82 | + && !type.typeArguments |
| 83 | + && this.isKnown(type); |
| 84 | + } |
| 85 | + |
| 86 | + private isMapped(type: Type | undefined) { |
| 87 | + return !this.isUnmapped(type); |
| 88 | + } |
| 89 | + |
| 90 | + private remapType<T extends Type | undefined>(type: T): T extends undefined ? undefined : T; |
| 91 | + private remapType(type: Type | undefined) { |
| 92 | + if (type instanceof UnionType) { |
| 93 | + let items = type.types.filter(t => this.isMapped(t)); |
| 94 | + if (items.length === 0) { |
| 95 | + items = type.types.map(t => this.remapType(t)); |
| 96 | + } else if (items.length === 1) { |
| 97 | + const item = items[0]; |
| 98 | + if ((item instanceof IntrinsicType) && (item.name === 'undefined' || item.name === 'null')) { |
| 99 | + items = type.types.map(t => this.remapType(t)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (items.length === 1) { |
| 104 | + return items[0]; |
| 105 | + } else { |
| 106 | + type.types = items; |
| 107 | + } |
| 108 | + } else if (type instanceof IntersectionType) { |
| 109 | + type.types = type.types.map(t => this.remapType(t)); |
| 110 | + } else if (type instanceof ArrayType) { |
| 111 | + type.elementType = this.remapType(type.elementType); |
| 112 | + } else if (type instanceof PredicateType) { |
| 113 | + type.targetType = this.remapType(type.targetType); |
| 114 | + } else if (type instanceof TupleType) { |
| 115 | + type.elements = type.elements.map(t => this.remapType(t)); |
| 116 | + } else if (type instanceof ReferenceType && type.typeArguments) { |
| 117 | + type.typeArguments = type.typeArguments.map(t => this.remapType(t)!); |
| 118 | + } else if (type instanceof ConditionalType) { |
| 119 | + type.checkType = this.remapType(type.checkType); |
| 120 | + type.extendsType = this.remapType(type.extendsType); |
| 121 | + type.trueType = this.remapType(type.trueType); |
| 122 | + type.falseType = this.remapType(type.falseType) |
| 123 | + } else if ((type instanceof TypeOperatorType) && this.isUnmapped(type.target)) { |
| 124 | + return new UnknownType('unknown'); |
| 125 | + } else if ((type instanceof IndexedAccessType) && this.isUnmapped(type.objectType)) { |
| 126 | + return new UnknownType('unknown'); |
| 127 | + } |
| 128 | + |
| 129 | + return this.isUnmapped(type) ? new UnknownType('unknown') : type; |
| 130 | + } |
| 131 | + |
| 132 | + private getReflectionsByInstanceType<T extends Reflection>(project: ProjectReflection, func: { new(...args: any[]): T }): T[] { |
| 133 | + return Object.values(project.reflections).filter(r => r instanceof func) as unknown as T[]; |
| 134 | + } |
| 135 | +} |
0 commit comments