@@ -66,6 +66,11 @@ export class ClassDeclarationVisitor {
6666 const extendsElements = this . collectExtends ( className , astFile ) ;
6767 const coverageIgnore = isCoverageIgnore ( classDeclaration ) ;
6868 const members = this . visitMembers ( classDeclaration . members , sourceFile ) ;
69+ this . applyInheritedDocumentation (
70+ members ,
71+ implementsElements . concat ( extendsElements ) ,
72+ astFile
73+ ) ;
6974
7075 if ( nodeHasDecorator ( classDeclaration ) ) {
7176 return this . buildDecoratedClassResult (
@@ -150,6 +155,140 @@ export class ClassDeclarationVisitor {
150155 return extendsList ;
151156 }
152157
158+ private applyInheritedDocumentation ( members : any , ancestorNames : any [ ] , astFile ?: any ) : void {
159+ if ( ! astFile || ! members ?. methods || ancestorNames . length === 0 ) {
160+ return ;
161+ }
162+
163+ for ( const method of members . methods ) {
164+ if ( ! method . name || ! this . needsInheritedDocumentation ( method ) ) {
165+ continue ;
166+ }
167+
168+ const inherited = this . findInheritedMethodDocumentation (
169+ method . name ,
170+ ancestorNames ,
171+ astFile ,
172+ new Set < string > ( )
173+ ) ;
174+
175+ if ( ! inherited ) {
176+ continue ;
177+ }
178+
179+ const rawdescription = this . hasInheritDoc ( method . rawdescription )
180+ ? this . replaceInheritDoc ( method . rawdescription , inherited . rawdescription )
181+ : inherited . rawdescription ;
182+
183+ method . rawdescription = rawdescription ;
184+ method . description = markedAcl ( rawdescription ) ;
185+ }
186+ }
187+
188+ private needsInheritedDocumentation ( method : any ) : boolean {
189+ return (
190+ ! method . rawdescription ?. trim ( ) ||
191+ this . hasInheritDoc ( method . rawdescription ) ||
192+ this . hasInheritDoc ( method . description )
193+ ) ;
194+ }
195+
196+ private hasInheritDoc ( value : string ) : boolean {
197+ return / \{ @ i n h e r i t D o c \} | @ i n h e r i t D o c / i. test ( value || '' ) ;
198+ }
199+
200+ private replaceInheritDoc ( value : string , inheritedValue : string ) : string {
201+ return ( value || '' ) . replace ( / \{ @ i n h e r i t D o c \} | @ i n h e r i t D o c / gi, inheritedValue ) ;
202+ }
203+
204+ private findInheritedMethodDocumentation (
205+ methodName : string ,
206+ ancestorNames : any [ ] ,
207+ astFile : any ,
208+ visited : Set < string >
209+ ) : { rawdescription : string } | undefined {
210+ for ( const ancestorName of ancestorNames ) {
211+ const normalizedName = this . normalizeHeritageName ( ancestorName ) ;
212+ if ( ! normalizedName || visited . has ( normalizedName ) ) {
213+ continue ;
214+ }
215+ visited . add ( normalizedName ) ;
216+
217+ const declaration = this . getAstDeclaration ( normalizedName , astFile ) ;
218+ if ( ! declaration ) {
219+ continue ;
220+ }
221+
222+ const inheritedMethod = this . findAstMethod ( declaration , methodName ) ;
223+ if ( inheritedMethod ) {
224+ const documentation = this . extractAstDocumentation ( inheritedMethod ) ;
225+ if ( documentation ) {
226+ return documentation ;
227+ }
228+ }
229+
230+ const nestedAncestors = this . collectNestedAncestorNames ( normalizedName , declaration , astFile ) ;
231+ const nestedDocumentation = this . findInheritedMethodDocumentation (
232+ methodName ,
233+ nestedAncestors ,
234+ astFile ,
235+ visited
236+ ) ;
237+ if ( nestedDocumentation ) {
238+ return nestedDocumentation ;
239+ }
240+ }
241+
242+ return undefined ;
243+ }
244+
245+ private normalizeHeritageName ( ancestorName : any ) : string {
246+ return String ( ancestorName || '' )
247+ . replace ( / < [ \s \S ] * $ / , '' )
248+ . trim ( ) ;
249+ }
250+
251+ private getAstDeclaration ( name : string , astFile : any ) : any {
252+ return astFile . getInterface ?.( name ) || astFile . getClass ?.( name ) ;
253+ }
254+
255+ private findAstMethod ( declaration : any , methodName : string ) : any {
256+ const methods = declaration . getMethods ?.( ) || [ ] ;
257+ return methods . find ( ( method : any ) => method . getName ?.( ) === methodName ) ;
258+ }
259+
260+ private extractAstDocumentation ( method : any ) : { rawdescription : string } | undefined {
261+ const compilerNode = method . compilerNode || method ;
262+ const sourceFile = method . getSourceFile ?.( ) ?. compilerNode || compilerNode . getSourceFile ?.( ) ;
263+ const comment = this . context . getJsdocParserUtil ( ) . getMainCommentOfNode (
264+ compilerNode ,
265+ sourceFile
266+ ) ;
267+
268+ if ( typeof comment !== 'string' || comment . length === 0 ) {
269+ return undefined ;
270+ }
271+
272+ const rawdescription = this . replaceInheritDoc (
273+ this . context . getJsdocParserUtil ( ) . parseComment ( comment ) ,
274+ ''
275+ ) ;
276+
277+ if ( ! rawdescription . trim ( ) ) {
278+ return undefined ;
279+ }
280+
281+ return { rawdescription } ;
282+ }
283+
284+ private collectNestedAncestorNames ( name : string , declaration : any , astFile : any ) : any [ ] {
285+ const nestedAncestors = this . collectExtends ( name , astFile ) ;
286+ if ( declaration . compilerNode ) {
287+ nestedAncestors . push ( ...this . collectImplements ( declaration . compilerNode ) ) ;
288+ }
289+ return nestedAncestors ;
290+ }
291+
153292 private buildDecoratedClassResult (
154293 fileName : string ,
155294 className : string ,
0 commit comments