|
| 1 | +import {DocCollection, Processor} from 'dgeni'; |
| 2 | +import {Type, TypeChecker, TypeFormatFlags} from 'dgeni-packages/node_modules/typescript'; |
| 3 | +import {ConstExportDoc} from 'dgeni-packages/typescript/api-doc-types/ConstExportDoc'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Processor that works around a Dgeni TypeScript package issue where the type of a constant export |
| 7 | + * is automatically truncated. |
| 8 | + * |
| 9 | + * Truncation of the type strings is causing unexpected results and also results in |
| 10 | + * misleading documentation. See https://github.com/angular/dgeni-packages/issues/276 |
| 11 | + */ |
| 12 | +export class NoTruncateConstTypeProcessor implements Processor { |
| 13 | + name = 'no-truncate-const-type'; |
| 14 | + $runBefore = ['categorizer']; |
| 15 | + |
| 16 | + $process(docs: DocCollection) { |
| 17 | + return docs |
| 18 | + .filter(doc => doc.docType === 'const') |
| 19 | + .forEach(doc => doc.type && this.refreshResolvedTypeString(doc)); |
| 20 | + } |
| 21 | + |
| 22 | + /** Refreshes the determined type string of the specified export const document. */ |
| 23 | + private refreshResolvedTypeString(doc: ConstExportDoc) { |
| 24 | + const {variableDeclaration, typeChecker} = doc; |
| 25 | + |
| 26 | + // Logic is aligned with the actual logic from the ConstExportDoc. |
| 27 | + // dgeni-packages#typescript/src/api-doc-types/ConstExportDoc.ts#L22 |
| 28 | + if (variableDeclaration.type) { |
| 29 | + doc.type = this.typeToString( |
| 30 | + typeChecker, typeChecker.getTypeFromTypeNode(variableDeclaration.type)); |
| 31 | + } else if (variableDeclaration.initializer) { |
| 32 | + doc.type = this.typeToString( |
| 33 | + typeChecker, typeChecker.getTypeAtLocation(variableDeclaration.initializer)); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** Converts the specified type to a string that represents the type declaration. */ |
| 38 | + private typeToString(typeChecker: TypeChecker, type: Type): string { |
| 39 | + return typeChecker.typeToString(type, undefined, TypeFormatFlags.NoTruncation); |
| 40 | + } |
| 41 | +} |
0 commit comments