|
| 1 | +const td = require("typedoc"); |
| 2 | + |
| 3 | +/** @param {td.Application} app */ |
| 4 | +exports.load = function (app) { |
| 5 | + app.converter.on( |
| 6 | + td.Converter.EVENT_CREATE_DECLARATION, |
| 7 | + function (context, reflection) { |
| 8 | + expandObjectLikeTypes(context, reflection, app); |
| 9 | + }, |
| 10 | + ); |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * The reflection types affected by this plugin. |
| 15 | + */ |
| 16 | +const TYPES_TO_EXPAND = ["mapped", "intersection", "reference"]; |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {td.Context} context |
| 20 | + * @param {td.DeclarationReflection} reflection |
| 21 | + * @param {td.Application} app |
| 22 | + */ |
| 23 | +function expandObjectLikeTypes(context, reflection, app) { |
| 24 | + const hasInternalModule = |
| 25 | + app.options.getDeclaration("internalModule") !== undefined; |
| 26 | + const internalModule = hasInternalModule |
| 27 | + ? app.options.getValue("internalModule") || "<internal>" |
| 28 | + : "<internal>"; |
| 29 | + |
| 30 | + if ( |
| 31 | + reflection.kind !== td.ReflectionKind.TypeAlias || |
| 32 | + !reflection.type?.type || |
| 33 | + !TYPES_TO_EXPAND.includes(reflection.type.type) || |
| 34 | + reflection.parent.originalName === internalModule |
| 35 | + ) |
| 36 | + return; |
| 37 | + |
| 38 | + const symbol = context.project.getSymbolFromReflection(reflection); |
| 39 | + const declaration = symbol?.declarations?.[0]; |
| 40 | + |
| 41 | + if (!symbol || !declaration) return; // Will probably never happen in practice |
| 42 | + const reflectionType = context.checker.getDeclaredTypeOfSymbol(symbol); |
| 43 | + |
| 44 | + const typeRefl = context |
| 45 | + .withScope(reflection) |
| 46 | + .createDeclarationReflection( |
| 47 | + td.ReflectionKind.TypeLiteral, |
| 48 | + undefined, |
| 49 | + undefined, |
| 50 | + "__type", |
| 51 | + ); |
| 52 | + context.finalizeDeclarationReflection(typeRefl); |
| 53 | + const typeContext = context.withScope(typeRefl); |
| 54 | + |
| 55 | + for (const propertySymbol of reflectionType.getProperties()) { |
| 56 | + const propertyType = |
| 57 | + propertySymbol && |
| 58 | + context.checker.getTypeOfSymbolAtLocation(propertySymbol, declaration); |
| 59 | + const resolvedReflection = resolvePropertyReflection( |
| 60 | + context, |
| 61 | + reflectionType, |
| 62 | + propertySymbol, |
| 63 | + ); |
| 64 | + |
| 65 | + const element = typeContext.createDeclarationReflection( |
| 66 | + td.ReflectionKind.Property, |
| 67 | + undefined, |
| 68 | + undefined, |
| 69 | + propertySymbol.name, |
| 70 | + ); |
| 71 | + |
| 72 | + if (resolvedReflection) { |
| 73 | + element.comment = resolvedReflection.comment; |
| 74 | + element.flags = resolvedReflection.flags; |
| 75 | + element.sources = resolvedReflection.sources; |
| 76 | + |
| 77 | + if (resolvedReflection instanceof td.DeclarationReflection) { |
| 78 | + element.defaultValue = resolvedReflection.defaultValue; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + element.type = typeContext.converter.convertType(typeContext, propertyType); |
| 83 | + typeContext.finalizeDeclarationReflection(element); |
| 84 | + } |
| 85 | + |
| 86 | + reflection.type = new td.ReflectionType(typeRefl); |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * @param {td.Context} context |
| 91 | + * @param {td.TypeScript.Type} objectType |
| 92 | + * @param {td.TypeScript.Symbol} propertySymbol |
| 93 | + */ |
| 94 | +function resolvePropertyReflection(context, objectType, propertySymbol) { |
| 95 | + const resolvedType = context.checker.getPropertyOfType( |
| 96 | + objectType, |
| 97 | + propertySymbol.name, |
| 98 | + ); |
| 99 | + const resolvedDeclaration = resolvedType?.declarations?.[0]; |
| 100 | + const resolvedSymbol = |
| 101 | + resolvedDeclaration && context.getSymbolAtLocation(resolvedDeclaration); |
| 102 | + |
| 103 | + return ( |
| 104 | + resolvedSymbol && context.project.getReflectionFromSymbol(resolvedSymbol) |
| 105 | + ); |
| 106 | +} |
0 commit comments