1
1
import * as ts from "typescript" ;
2
2
import {
3
+ ContainerReflection ,
3
4
DeclarationReflection ,
4
5
Reflection ,
5
6
ReflectionKind ,
@@ -10,7 +11,6 @@ import { filterMap, zip } from "../../utils/array";
10
11
import { Component , ConverterComponent } from "../components" ;
11
12
import type { Context } from "../context" ;
12
13
import { Converter } from "../converter" ;
13
- import { copyComment } from "../utils/reflections" ;
14
14
15
15
/**
16
16
* A plugin that detects interface implementations of functions and
@@ -25,7 +25,11 @@ export class ImplementsPlugin extends ConverterComponent {
25
25
* Create a new ImplementsPlugin instance.
26
26
*/
27
27
override initialize ( ) {
28
- this . listenTo ( this . owner , Converter . EVENT_RESOLVE , this . onResolve , - 10 ) ;
28
+ this . listenTo (
29
+ this . owner ,
30
+ Converter . EVENT_RESOLVE_END ,
31
+ this . onResolveEnd
32
+ ) ;
29
33
this . listenTo (
30
34
this . owner ,
31
35
Converter . EVENT_CREATE_DECLARATION ,
@@ -47,38 +51,21 @@ export class ImplementsPlugin extends ConverterComponent {
47
51
* @param classReflection The reflection of the classReflection class.
48
52
* @param interfaceReflection The reflection of the interfaceReflection interface.
49
53
*/
50
- private analyzeClass (
54
+ private analyzeImplements (
51
55
context : Context ,
52
56
classReflection : DeclarationReflection ,
53
57
interfaceReflection : DeclarationReflection
54
58
) {
59
+ handleInheritedComments ( classReflection , interfaceReflection ) ;
55
60
if ( ! interfaceReflection . children ) {
56
61
return ;
57
62
}
58
63
59
64
interfaceReflection . children . forEach ( ( interfaceMember ) => {
60
- let classMember : DeclarationReflection | undefined ;
61
-
62
- if ( ! classReflection . children ) {
63
- return ;
64
- }
65
-
66
- for (
67
- let index = 0 , count = classReflection . children . length ;
68
- index < count ;
69
- index ++
70
- ) {
71
- const child = classReflection . children [ index ] ;
72
- if ( child . name !== interfaceMember . name ) {
73
- continue ;
74
- }
75
- if ( child . flags . isStatic !== interfaceMember . flags . isStatic ) {
76
- continue ;
77
- }
78
-
79
- classMember = child ;
80
- break ;
81
- }
65
+ const classMember = findMatchingMember (
66
+ interfaceMember ,
67
+ classReflection
68
+ ) ;
82
69
83
70
if ( ! classMember ) {
84
71
return ;
@@ -92,23 +79,6 @@ export class ImplementsPlugin extends ConverterComponent {
92
79
interfaceMember ,
93
80
context . project
94
81
) ;
95
- copyComment ( classMember , interfaceMember ) ;
96
-
97
- if (
98
- interfaceMember . kindOf ( ReflectionKind . Property ) &&
99
- classMember . kindOf ( ReflectionKind . Accessor )
100
- ) {
101
- if ( classMember . getSignature ) {
102
- copyComment ( classMember . getSignature , interfaceMember ) ;
103
- classMember . getSignature . implementationOf =
104
- classMember . implementationOf ;
105
- }
106
- if ( classMember . setSignature ) {
107
- copyComment ( classMember . setSignature , interfaceMember ) ;
108
- classMember . setSignature . implementationOf =
109
- classMember . implementationOf ;
110
- }
111
- }
112
82
113
83
if (
114
84
interfaceMember . kindOf ( ReflectionKind . FunctionOrMethod ) &&
@@ -127,9 +97,10 @@ export class ImplementsPlugin extends ConverterComponent {
127
97
context . project
128
98
) ;
129
99
}
130
- copyComment ( clsSig , intSig ) ;
131
100
}
132
101
}
102
+
103
+ handleInheritedComments ( classMember , interfaceMember ) ;
133
104
} ) ;
134
105
}
135
106
@@ -150,12 +121,10 @@ export class ImplementsPlugin extends ConverterComponent {
150
121
) ;
151
122
152
123
for ( const parent of extendedTypes ) {
124
+ handleInheritedComments ( reflection , parent . reflection ) ;
125
+
153
126
for ( const parentMember of parent . reflection . children ?? [ ] ) {
154
- const child = reflection . children ?. find (
155
- ( child ) =>
156
- child . name == parentMember . name &&
157
- child . flags . isStatic === parentMember . flags . isStatic
158
- ) ;
127
+ const child = findMatchingMember ( parentMember , reflection ) ;
159
128
160
129
if ( child ) {
161
130
const key = child . overwrites
@@ -171,28 +140,26 @@ export class ImplementsPlugin extends ConverterComponent {
171
140
parentSig ,
172
141
context . project
173
142
) ;
174
- copyComment ( childSig , parentSig ) ;
175
143
}
176
144
177
145
child [ key ] = ReferenceType . createResolvedReference (
178
146
`${ parent . name } .${ parentMember . name } ` ,
179
147
parentMember ,
180
148
context . project
181
149
) ;
182
- copyComment ( child , parentMember ) ;
150
+
151
+ handleInheritedComments ( child , parentMember ) ;
183
152
}
184
153
}
185
154
}
186
155
}
187
156
188
- /**
189
- * Triggered when the converter resolves a reflection.
190
- *
191
- * @param context The context object describing the current state the converter is in.
192
- * @param reflection The reflection that is currently resolved.
193
- */
194
- private onResolve ( context : Context , reflection : DeclarationReflection ) {
195
- this . tryResolve ( context , reflection ) ;
157
+ private onResolveEnd ( context : Context ) {
158
+ for ( const reflection of Object . values ( context . project . reflections ) ) {
159
+ if ( reflection instanceof DeclarationReflection ) {
160
+ this . tryResolve ( context , reflection ) ;
161
+ }
162
+ }
196
163
}
197
164
198
165
private tryResolve ( context : Context , reflection : DeclarationReflection ) {
@@ -235,22 +202,19 @@ export class ImplementsPlugin extends ConverterComponent {
235
202
236
203
if (
237
204
type . reflection &&
238
- type . reflection . kindOf ( ReflectionKind . Interface )
205
+ type . reflection . kindOf ( ReflectionKind . ClassOrInterface )
239
206
) {
240
- this . analyzeClass (
207
+ this . analyzeImplements (
241
208
context ,
242
209
reflection ,
243
- < DeclarationReflection > type . reflection
210
+ type . reflection as DeclarationReflection
244
211
) ;
245
212
}
246
213
} ) ;
247
214
}
248
215
249
216
if (
250
- reflection . kindOf ( [
251
- ReflectionKind . Class ,
252
- ReflectionKind . Interface ,
253
- ] ) &&
217
+ reflection . kindOf ( ReflectionKind . ClassOrInterface ) &&
254
218
reflection . extendedTypes
255
219
) {
256
220
this . analyzeInheritance ( context , reflection ) ;
@@ -460,3 +424,109 @@ function createLink(
460
424
}
461
425
}
462
426
}
427
+
428
+ /**
429
+ * Responsible for copying comments from "parent" reflections defined
430
+ * in either a base class or implemented interface to the child class.
431
+ */
432
+ function handleInheritedComments (
433
+ child : DeclarationReflection ,
434
+ parent : DeclarationReflection
435
+ ) {
436
+ copyComment ( child , parent ) ;
437
+
438
+ if (
439
+ parent . kindOf ( ReflectionKind . Property ) &&
440
+ child . kindOf ( ReflectionKind . Accessor )
441
+ ) {
442
+ if ( child . getSignature ) {
443
+ copyComment ( child . getSignature , parent ) ;
444
+ child . getSignature . implementationOf = child . implementationOf ;
445
+ }
446
+ if ( child . setSignature ) {
447
+ copyComment ( child . setSignature , parent ) ;
448
+ child . setSignature . implementationOf = child . implementationOf ;
449
+ }
450
+ }
451
+ if (
452
+ parent . kindOf ( ReflectionKind . Accessor ) &&
453
+ child . kindOf ( ReflectionKind . Accessor )
454
+ ) {
455
+ if ( parent . getSignature && child . getSignature ) {
456
+ copyComment ( child . getSignature , parent . getSignature ) ;
457
+ }
458
+ if ( parent . setSignature && child . setSignature ) {
459
+ copyComment ( child . setSignature , parent . setSignature ) ;
460
+ }
461
+ }
462
+
463
+ if (
464
+ parent . kindOf ( ReflectionKind . FunctionOrMethod ) &&
465
+ parent . signatures &&
466
+ child . signatures
467
+ ) {
468
+ for ( const [ cs , ps ] of zip ( child . signatures , parent . signatures ) ) {
469
+ copyComment ( cs , ps ) ;
470
+ }
471
+ }
472
+ }
473
+
474
+ /**
475
+ * Copy the comment of the source reflection to the target reflection with a JSDoc style copy
476
+ * function. The TSDoc copy function is in the InheritDocPlugin.
477
+ */
478
+ function copyComment ( target : Reflection , source : Reflection ) {
479
+ if ( target . comment ) {
480
+ // We might still want to copy, if the child has a JSDoc style inheritDoc tag.
481
+ const tag = target . comment . getTag ( "@inheritDoc" ) ;
482
+ if ( ! tag || tag . name ) {
483
+ return ;
484
+ }
485
+ }
486
+
487
+ if ( ! source . comment ) {
488
+ return ;
489
+ }
490
+
491
+ target . comment = source . comment . clone ( ) ;
492
+
493
+ if (
494
+ target instanceof DeclarationReflection &&
495
+ source instanceof DeclarationReflection
496
+ ) {
497
+ for ( const [ tt , ts ] of zip (
498
+ target . typeParameters || [ ] ,
499
+ source . typeParameters || [ ]
500
+ ) ) {
501
+ copyComment ( tt , ts ) ;
502
+ }
503
+ }
504
+ if (
505
+ target instanceof SignatureReflection &&
506
+ source instanceof SignatureReflection
507
+ ) {
508
+ for ( const [ tt , ts ] of zip (
509
+ target . typeParameters || [ ] ,
510
+ source . typeParameters || [ ]
511
+ ) ) {
512
+ copyComment ( tt , ts ) ;
513
+ }
514
+ for ( const [ pt , ps ] of zip (
515
+ target . parameters || [ ] ,
516
+ source . parameters || [ ]
517
+ ) ) {
518
+ copyComment ( pt , ps ) ;
519
+ }
520
+ }
521
+ }
522
+
523
+ function findMatchingMember (
524
+ toMatch : Reflection ,
525
+ container : ContainerReflection
526
+ ) {
527
+ return container . children ?. find (
528
+ ( child ) =>
529
+ child . name == toMatch . name &&
530
+ child . flags . isStatic === toMatch . flags . isStatic
531
+ ) ;
532
+ }
0 commit comments