1+ const { ReferenceType} = require ( "typedoc" ) ;
2+
3+ function fixAceInternalReferences ( projectReflection ) {
4+ const ace = Object . values ( projectReflection . reflections ) . find ( ( el ) => {
5+ return el . name === "ace" ;
6+ } ) ;
7+
8+ if ( ! ace ) {
9+ console . log ( "Could not find ace reflection" ) ;
10+ return ;
11+ }
12+
13+ const aceInternalReferences = findAceInternalReferences ( projectReflection ) ;
14+
15+ const symbolMapping = createReferencedSymbolMapping ( aceInternalReferences , ace ) ;
16+
17+ replaceReferences ( aceInternalReferences , symbolMapping , projectReflection ) ;
18+
19+ }
20+
21+ function findAceInternalReferences ( projectReflection ) {
22+ const references = [ ] ;
23+
24+ Object . values ( projectReflection . reflections ) . forEach ( reflection => {
25+ if ( reflection . type ) {
26+ findAndCollectReferences ( reflection . type , references ) ;
27+ }
28+ } ) ;
29+
30+ return references ;
31+ }
32+
33+ function isAceInternalReference ( type ) {
34+ return type . type === 'reference' && type . _target && type . _target . packagePath === 'ace-internal.d.ts' ;
35+ }
36+
37+ function createReferencedSymbolMapping ( references , ace ) {
38+ const mapping = new Map ( ) ;
39+
40+ const aceSymbols = new Map ( ) ;
41+
42+ function collectAceSymbols ( reflection ) {
43+ if ( reflection . children ) {
44+ reflection . children . forEach ( child => {
45+ aceSymbols . set ( child . name , child ) ;
46+ collectAceSymbols ( child ) ;
47+ } ) ;
48+ }
49+ }
50+
51+ collectAceSymbols ( ace ) ;
52+
53+ references . forEach ( ref => {
54+ const symbolName = ref . name ;
55+ const aceSymbol = aceSymbols . get ( symbolName ) ;
56+
57+ if ( aceSymbol ) {
58+ mapping . set ( ref . _target , aceSymbol ) ;
59+ }
60+ } ) ;
61+
62+ return mapping ;
63+ }
64+
65+ function findAndCollectReferences ( type , references ) {
66+ if ( ! type ) return ;
67+
68+ if ( isAceInternalReference ( type ) ) {
69+ references . push ( type ) ;
70+ }
71+
72+ if ( type . type === 'union' && type . types ) {
73+ type . types . forEach ( unionType => {
74+ findAndCollectReferences ( unionType , references ) ;
75+ } ) ;
76+ }
77+ else if ( type . type === 'intersection' && type . types ) {
78+ type . types . forEach ( intersectionType => {
79+ findAndCollectReferences ( intersectionType , references ) ;
80+ } ) ;
81+ }
82+ else if ( type . type === 'array' && type . elementType ) {
83+ findAndCollectReferences ( type . elementType , references ) ;
84+ }
85+ else if ( type . type === 'tuple' && type . elements ) {
86+ type . elements . forEach ( element => {
87+ findAndCollectReferences ( element , references ) ;
88+ } ) ;
89+ }
90+ else if ( type . type === 'reference' && type . typeArguments ) {
91+ type . typeArguments . forEach ( typeArg => {
92+ findAndCollectReferences ( typeArg , references ) ;
93+ } ) ;
94+ }
95+ else if ( type . type === 'conditional' ) {
96+ if ( type . checkType ) findAndCollectReferences ( type . checkType , references ) ;
97+ if ( type . extendsType ) findAndCollectReferences ( type . extendsType , references ) ;
98+ if ( type . trueType ) findAndCollectReferences ( type . trueType , references ) ;
99+ if ( type . falseType ) findAndCollectReferences ( type . falseType , references ) ;
100+ }
101+ else if ( type . type === 'indexedAccess' ) {
102+ if ( type . objectType ) findAndCollectReferences ( type . objectType , references ) ;
103+ if ( type . indexType ) findAndCollectReferences ( type . indexType , references ) ;
104+ }
105+ }
106+
107+ function replaceReferences ( references , symbolMapping , projectReflection ) {
108+ Object . values ( projectReflection . reflections ) . forEach ( reflection => {
109+ if ( reflection . type ) {
110+ reflection . type = replaceReferencesInType ( reflection . type , symbolMapping , projectReflection ) ;
111+ }
112+ } ) ;
113+ }
114+
115+ function replaceReferencesInType ( type , symbolMapping , projectReflection ) {
116+ if ( ! type ) return type ;
117+
118+ if ( isAceInternalReference ( type ) ) {
119+ const aceSymbol = symbolMapping . get ( type . _target ) ;
120+ if ( aceSymbol ) {
121+ return ReferenceType . createResolvedReference ( type . name , aceSymbol , projectReflection ) ;
122+ }
123+ }
124+
125+ if ( type . type === 'union' && type . types ) {
126+ type . types = type . types . map ( unionType => replaceReferencesInType ( unionType , symbolMapping , projectReflection ) ) ;
127+ }
128+ else if ( type . type === 'intersection' && type . types ) {
129+ type . types = type . types . map (
130+ intersectionType => replaceReferencesInType ( intersectionType , symbolMapping , projectReflection ) ) ;
131+ }
132+ else if ( type . type === 'array' && type . elementType ) {
133+ type . elementType = replaceReferencesInType ( type . elementType , symbolMapping , projectReflection ) ;
134+ }
135+ else if ( type . type === 'tuple' && type . elements ) {
136+ type . elements = type . elements . map (
137+ element => replaceReferencesInType ( element , symbolMapping , projectReflection ) ) ;
138+ }
139+ else if ( type . type === 'reference' && type . typeArguments ) {
140+ type . typeArguments = type . typeArguments . map (
141+ typeArg => replaceReferencesInType ( typeArg , symbolMapping , projectReflection ) ) ;
142+ }
143+ else if ( type . type === 'conditional' ) {
144+ if ( type . checkType ) type . checkType = replaceReferencesInType ( type . checkType , symbolMapping , projectReflection ) ;
145+ if ( type . extendsType ) type . extendsType = replaceReferencesInType (
146+ type . extendsType , symbolMapping , projectReflection ) ;
147+ if ( type . trueType ) type . trueType = replaceReferencesInType ( type . trueType , symbolMapping , projectReflection ) ;
148+ if ( type . falseType ) type . falseType = replaceReferencesInType ( type . falseType , symbolMapping , projectReflection ) ;
149+ }
150+ else if ( type . type === 'indexedAccess' ) {
151+ if ( type . objectType ) type . objectType = replaceReferencesInType (
152+ type . objectType , symbolMapping , projectReflection ) ;
153+ if ( type . indexType ) type . indexType = replaceReferencesInType ( type . indexType , symbolMapping , projectReflection ) ;
154+ }
155+
156+ return type ;
157+ }
158+
159+ exports . fixAceInternalReferences = fixAceInternalReferences ;
0 commit comments