@@ -16,7 +16,9 @@ import {isNamedClassDeclaration, TypeScriptReflectionHost} from '../../reflectio
1616import { extractClass , extractInterface } from './class_extractor' ;
1717import { extractConstant , isSyntheticAngularConstant } from './constant_extractor' ;
1818import { DocEntry } from './entities' ;
19+ import { isAngularPrivateName } from './filters' ;
1920
21+ type DeclarationWithExportName = readonly [ string , ts . Declaration ] ;
2022
2123/**
2224 * Extracts all information from a source file that may be relevant for generating
@@ -34,51 +36,80 @@ export class DocsExtractor {
3436 extractAll ( sourceFile : ts . SourceFile ) : DocEntry [ ] {
3537 const entries : DocEntry [ ] = [ ] ;
3638
39+ const exportedDeclarations = this . getExportedDeclarations ( sourceFile ) ;
40+ for ( const [ exportName , node ] of exportedDeclarations ) {
41+ // Skip any symbols with an Angular-internal name.
42+ if ( isAngularPrivateName ( exportName ) ) continue ;
43+
44+ const entry = this . extractDeclaration ( node ) ;
45+ if ( entry ) {
46+ // The exported name of an API may be different from its declaration name, so
47+ // use the declaration name.
48+ entries . push ( { ...entry , name : exportName } ) ;
49+ }
50+ }
51+
52+ return entries ;
53+ }
54+
55+ /** Extract the doc entry for a single declaration. */
56+ private extractDeclaration ( node : ts . Declaration ) : DocEntry | null {
57+ // Ignore anonymous classes.
58+ if ( isNamedClassDeclaration ( node ) ) {
59+ return extractClass ( node , this . metadataReader , this . typeChecker ) ;
60+ }
61+
62+ if ( ts . isInterfaceDeclaration ( node ) ) {
63+ return extractInterface ( node , this . typeChecker ) ;
64+ }
65+
66+ if ( ts . isFunctionDeclaration ( node ) ) {
67+ const functionExtractor = new FunctionExtractor ( node , this . typeChecker ) ;
68+ return functionExtractor . extract ( ) ;
69+ }
70+
71+ if ( ts . isVariableDeclaration ( node ) && ! isSyntheticAngularConstant ( node ) ) {
72+ return extractConstant ( node , this . typeChecker ) ;
73+ }
74+
75+ if ( ts . isEnumDeclaration ( node ) ) {
76+ return extractEnum ( node , this . typeChecker ) ;
77+ }
78+
79+ return null ;
80+ }
81+
82+ /** Gets the list of exported declarations for doc extraction. */
83+ private getExportedDeclarations ( sourceFile : ts . SourceFile ) : DeclarationWithExportName [ ] {
3784 // Use the reflection host to get all the exported declarations from this
3885 // source file entry point.
3986 const reflector = new TypeScriptReflectionHost ( this . typeChecker ) ;
4087 const exportedDeclarationMap = reflector . getExportsOfModule ( sourceFile ) ;
4188
42- // Sort the declaration nodes into declaration position because their order is lost in
43- // reading from the export map. This is primarily useful for testing and debugging.
44- const exportedDeclarations =
89+ // Augment each declaration with the exported name in the public API.
90+ let exportedDeclarations =
4591 Array . from ( exportedDeclarationMap ?. entries ( ) ?? [ ] )
46- . map ( ( [ exportName , declaration ] ) => [ exportName , declaration . node ] as const )
47- . sort ( ( [ a , declarationA ] , [ b , declarationB ] ) => declarationA . pos - declarationB . pos ) ;
48-
49- for ( const [ exportName , node ] of exportedDeclarations ) {
50- let entry : DocEntry | undefined = undefined ;
51-
52- // Ignore anonymous classes.
53- if ( isNamedClassDeclaration ( node ) ) {
54- entry = extractClass ( node , this . metadataReader , this . typeChecker ) ;
55- }
56-
57- if ( ts . isInterfaceDeclaration ( node ) ) {
58- entry = extractInterface ( node , this . typeChecker ) ;
59- }
92+ . map ( ( [ exportName , declaration ] ) => [ exportName , declaration . node ] as const ) ;
6093
61- if ( ts . isFunctionDeclaration ( node ) ) {
62- const functionExtractor = new FunctionExtractor ( node , this . typeChecker ) ;
63- entry = functionExtractor . extract ( ) ;
64- }
94+ // Cache the declaration count since we're going to be appending more declarations as
95+ // we iterate.
96+ const declarationCount = exportedDeclarations . length ;
6597
66- if ( ts . isVariableDeclaration ( node ) && ! isSyntheticAngularConstant ( node ) ) {
67- entry = extractConstant ( node , this . typeChecker ) ;
68- }
98+ // The exported declaration map only includes one function declaration in situations
99+ // where a function has overloads, so we add the overloads here.
100+ for ( let i = 0 ; i < declarationCount ; i ++ ) {
101+ const [ exportName , declaration ] = exportedDeclarations [ i ] ;
102+ if ( ts . isFunctionDeclaration ( declaration ) ) {
103+ const extractor = new FunctionExtractor ( declaration , this . typeChecker ) ;
104+ const overloads = extractor . getOverloads ( ) . map ( overload => [ exportName , overload ] as const ) ;
69105
70- if ( ts . isEnumDeclaration ( node ) ) {
71- entry = extractEnum ( node , this . typeChecker ) ;
72- }
73-
74- // The exported name of an API may be different from its declaration name, so
75- // use the declaration name.
76- if ( entry ) {
77- entry . name = exportName ;
78- entries . push ( entry ) ;
106+ exportedDeclarations . push ( ...overloads ) ;
79107 }
80108 }
81109
82- return entries ;
110+ // Sort the declaration nodes into declaration position because their order is lost in
111+ // reading from the export map. This is primarily useful for testing and debugging.
112+ return exportedDeclarations . sort (
113+ ( [ a , declarationA ] , [ b , declarationB ] ) => declarationA . pos - declarationB . pos ) ;
83114 }
84115}
0 commit comments