1
- import { ok } from "assert" ;
2
1
import {
3
2
Application ,
4
3
Context ,
@@ -15,9 +14,16 @@ declare module "typedoc" {
15
14
export interface TypeDocOptionMap {
16
15
internalModule : string ;
17
16
}
17
+
18
+ export interface Reflection {
19
+ [ InternalModule ] ?: boolean ;
20
+ }
18
21
}
19
22
20
23
let hasMonkeyPatched = false ;
24
+ const ModuleLike : ReflectionKind =
25
+ ReflectionKind . Project | ReflectionKind . Module ;
26
+ const InternalModule = Symbol ( ) ;
21
27
22
28
export function load ( app : Application ) {
23
29
if ( hasMonkeyPatched ) {
@@ -27,26 +33,25 @@ export function load(app: Application) {
27
33
}
28
34
hasMonkeyPatched = true ;
29
35
30
- let activeReflection : Reflection | undefined ;
31
36
const referencedSymbols = new Map < ts . Program , Set < ts . Symbol > > ( ) ;
32
- const symbolToActiveRefl = new Map < ts . Symbol , Reflection > ( ) ;
37
+ const symbolToOwningModule = new Map < ts . Symbol , Reflection > ( ) ;
33
38
const knownPrograms = new Map < Reflection , ts . Program > ( ) ;
34
39
35
40
function discoverMissingExports (
41
+ owningModule : Reflection ,
36
42
context : Context ,
37
43
program : ts . Program ,
38
44
) : Set < ts . Symbol > {
39
45
// An export is missing if if was referenced
40
46
// Is not contained in the documented
41
- // And is "owned" by the active reflection
42
47
const referenced = referencedSymbols . get ( program ) || new Set ( ) ;
43
48
const ownedByOther = new Set < ts . Symbol > ( ) ;
44
49
referencedSymbols . set ( program , ownedByOther ) ;
45
50
46
51
for ( const s of [ ...referenced ] ) {
47
52
if ( context . project . getReflectionFromSymbol ( s ) ) {
48
53
referenced . delete ( s ) ;
49
- } else if ( symbolToActiveRefl . get ( s ) !== activeReflection ) {
54
+ } else if ( symbolToOwningModule . get ( s ) !== owningModule ) {
50
55
referenced . delete ( s ) ;
51
56
ownedByOther . add ( s ) ;
52
57
}
@@ -58,9 +63,15 @@ export function load(app: Application) {
58
63
// Monkey patch the constructor for references so that we can get every
59
64
const origCreateSymbolReference = ReferenceType . createSymbolReference ;
60
65
ReferenceType . createSymbolReference = function ( symbol , context , name ) {
61
- ok ( activeReflection , "active reflection has not been set" ) ;
66
+ const owningModule = getOwningModule ( context ) ;
67
+ console . log (
68
+ "Created ref" ,
69
+ symbol . name ,
70
+ "owner" ,
71
+ owningModule . getFullName ( ) ,
72
+ ) ;
62
73
const set = referencedSymbols . get ( context . program ) ;
63
- symbolToActiveRefl . set ( symbol , activeReflection ) ;
74
+ symbolToOwningModule . set ( symbol , owningModule ) ;
64
75
if ( set ) {
65
76
set . add ( symbol ) ;
66
77
} else {
@@ -78,9 +89,8 @@ export function load(app: Application) {
78
89
app . converter . on (
79
90
Converter . EVENT_CREATE_DECLARATION ,
80
91
( context : Context , refl : Reflection ) => {
81
- if ( refl . kindOf ( ReflectionKind . Project | ReflectionKind . Module ) ) {
92
+ if ( refl . kindOf ( ModuleLike ) ) {
82
93
knownPrograms . set ( refl , context . program ) ;
83
- activeReflection = refl ;
84
94
}
85
95
} ,
86
96
) ;
@@ -96,12 +106,11 @@ export function load(app: Application) {
96
106
}
97
107
98
108
for ( const mod of modules ) {
99
- activeReflection = mod ;
100
-
101
109
const program = knownPrograms . get ( mod ) ;
102
110
if ( ! program ) continue ;
103
- let missing = discoverMissingExports ( context , program ) ;
104
- if ( ! missing || ! missing . size ) continue ;
111
+
112
+ let missing = discoverMissingExports ( mod , context , program ) ;
113
+ if ( ! missing . size ) continue ;
105
114
106
115
// Nasty hack here that will almost certainly break in future TypeDoc versions.
107
116
context . setActiveProgram ( program ) ;
@@ -114,6 +123,7 @@ export function load(app: Application) {
114
123
void 0 ,
115
124
context . converter . application . options . getValue ( "internalModule" ) ,
116
125
) ;
126
+ internalNs [ InternalModule ] = true ;
117
127
context . finalizeDeclarationReflection ( internalNs ) ;
118
128
const internalContext = context . withScope ( internalNs ) ;
119
129
@@ -130,7 +140,7 @@ export function load(app: Application) {
130
140
tried . add ( s ) ;
131
141
}
132
142
133
- missing = discoverMissingExports ( context , program ) ;
143
+ missing = discoverMissingExports ( mod , context , program ) ;
134
144
for ( const s of tried ) {
135
145
missing . delete ( s ) ;
136
146
}
@@ -146,12 +156,28 @@ export function load(app: Application) {
146
156
147
157
knownPrograms . clear ( ) ;
148
158
referencedSymbols . clear ( ) ;
149
- symbolToActiveRefl . clear ( ) ;
159
+ symbolToOwningModule . clear ( ) ;
150
160
} ,
151
161
void 0 ,
152
162
1e9 ,
153
163
) ;
154
164
}
165
+
166
+ function getOwningModule ( context : Context ) : Reflection {
167
+ let refl = context . scope ;
168
+ // Go up the reflection hierarchy until we get to a module
169
+ while ( ! refl . kindOf ( ModuleLike ) ) {
170
+ refl = refl . parent ! ;
171
+ }
172
+
173
+ // The <internal> module cannot be an owning module.
174
+ if ( refl [ InternalModule ] ) {
175
+ return refl . parent ! ;
176
+ }
177
+
178
+ return refl ;
179
+ }
180
+
155
181
function shouldConvertSymbol ( symbol : ts . Symbol , checker : ts . TypeChecker ) {
156
182
while ( symbol . flags & ts . SymbolFlags . Alias ) {
157
183
symbol = checker . getAliasedSymbol ( symbol ) ;
0 commit comments