Skip to content

Commit 4b15b78

Browse files
devversionvivian-hu-zz
authored andcommitted
build(docs): ensure const types are not truncated (#13457)
* By default TypeScript truncates the types when calling `typeToString`. Since we don't want to truncate the types automatically, we need to work around: angular/dgeni-packages#276 until there is a possibility to change this using the `tsHost`.
1 parent 4a9bbcb commit 4b15b78

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

tools/dgeni/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {ReadTypeScriptModules} from 'dgeni-packages/typescript/processors/readTy
1111
import {TsParser} from 'dgeni-packages/typescript/services/TsParser';
1212
import {sync as globSync} from 'glob';
1313
import * as path from 'path';
14+
import {NoTruncateConstTypeProcessor} from './processors/no-truncate-const-type';
1415

1516
// Dgeni packages that the Material docs package depends on.
1617
const jsdocPackage = require('dgeni-packages/jsdoc');
@@ -51,6 +52,9 @@ export const apiDocsPackage = new Package('material2-api-docs', [
5152
typescriptPackage,
5253
]);
5354

55+
// Processor that ensures that Dgeni const docs don't truncate the resolved type string.
56+
apiDocsPackage.processor(new NoTruncateConstTypeProcessor());
57+
5458
// Processor that filters out duplicate exports that should not be shown in the docs.
5559
apiDocsPackage.processor(new FilterDuplicateExports());
5660

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)