Skip to content

Commit 2138b1b

Browse files
committed
chore(app): support throws and inherited docs
closes #450 closes #242
1 parent 8f555c0 commit 2138b1b

12 files changed

Lines changed: 412 additions & 3 deletions

File tree

‎src/app/compiler/angular/deps/helpers/class-declaration.visitor.ts‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 /\{@inheritDoc\}|@inheritDoc/i.test(value || '');
198+
}
199+
200+
private replaceInheritDoc(value: string, inheritedValue: string): string {
201+
return (value || '').replace(/\{@inheritDoc\}|@inheritDoc/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,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { JsdocTagInterface } from '../../interfaces/jsdoc-tag.interface';
2+
import type { IHtmlEngineHelper, IHandlebarsOptions } from './html-engine-helper.interface';
3+
4+
const THROW_TAGS = new Set(['throws', 'throw', 'exception']);
5+
6+
export class JsdocThrowsHelper implements IHtmlEngineHelper {
7+
public helperFunc(context: any, jsdocTags: JsdocTagInterface[], options: IHandlebarsOptions) {
8+
const tags = [];
9+
10+
for (const jsdocTag of jsdocTags) {
11+
if (jsdocTag.tagName && THROW_TAGS.has(jsdocTag.tagName.text)) {
12+
tags.push({
13+
comment: jsdocTag.comment
14+
});
15+
}
16+
}
17+
18+
if (tags.length > 0) {
19+
context.tags = tags;
20+
return options.fn(context);
21+
}
22+
}
23+
}

‎src/app/engines/html.engine.helpers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { JsdocExampleHelper } from './html-engine-helpers/jsdoc-example.helper';
2626
import { JsdocParamsValidHelper } from './html-engine-helpers/jsdoc-params-valid.helper';
2727
import { JsdocParamsHelper } from './html-engine-helpers/jsdoc-params.helper';
2828
import { JsdocReturnsCommentHelper } from './html-engine-helpers/jsdoc-returns-comment.helper';
29+
import { JsdocThrowsHelper } from './html-engine-helpers/jsdoc-throws.helper';
2930
import { LinkTypeHelper } from './html-engine-helpers/link-type.helper';
3031
import { ModifIconHelper } from './html-engine-helpers/modif-icon.helper';
3132
import { ModifKindHelper } from './html-engine-helpers/modif-kind-helper';
@@ -66,6 +67,7 @@ export class HtmlEngineHelpers {
6667
this.registerHelper(bars, 'jsdoc-example', new JsdocExampleHelper());
6768
this.registerHelper(bars, 'jsdoc-params', new JsdocParamsHelper());
6869
this.registerHelper(bars, 'jsdoc-params-valid', new JsdocParamsValidHelper());
70+
this.registerHelper(bars, 'jsdoc-throws', new JsdocThrowsHelper());
6971
this.registerHelper(bars, 'jsdoc-default', new JsdocDefaultHelper());
7072
this.registerHelper(bars, 'linkType', new LinkTypeHelper());
7173
this.registerHelper(bars, 'indexableSignature', new IndexableSignatureHelper());

‎src/locales/en-US.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export const TRANSLATION_EN_US = {
101101
statements: 'Statements',
102102
styles: 'Styles',
103103
template: 'Template',
104+
throws: 'Throws',
104105
type: 'Type',
105106
'type-aliases': 'Type aliases',
106107
'type-parameters': 'Type parameters',

‎src/templates/partials/block-accessors.hbs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@
110110
</div>
111111
{{/if}}
112112
{{/if}}
113+
{{#if this.getSignature.jsdoctags}}
114+
{{#jsdoc-throws this.getSignature.jsdoctags}}
115+
<div class="io-description">
116+
<b>{{t "throws" }} :</b>
117+
<ul>
118+
{{#each tags}}
119+
<li>{{{comment}}}</li>
120+
{{/each}}
121+
</ul>
122+
</div>
123+
{{/jsdoc-throws}}
124+
{{/if}}
113125
</td>
114126
</tr>
115127
{{/or}}
@@ -203,6 +215,18 @@
203215
</div>
204216
{{/if}}
205217
{{/if}}
218+
{{#if this.setSignature.jsdoctags}}
219+
{{#jsdoc-throws this.setSignature.jsdoctags}}
220+
<div class="io-description">
221+
<b>{{t "throws" }} :</b>
222+
<ul>
223+
{{#each tags}}
224+
<li>{{{comment}}}</li>
225+
{{/each}}
226+
</ul>
227+
</div>
228+
{{/jsdoc-throws}}
229+
{{/if}}
206230
</td>
207231
</tr>
208232
{{/or}}

‎src/templates/partials/block-constructor.hbs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@
9090
{{/jsdoc-example}}
9191
</div>
9292
{{/jsdoc-params-valid}}
93+
{{#jsdoc-throws constructor.jsdoctags}}
94+
<div class="io-description">
95+
<b>{{t "throws" }} :</b>
96+
<ul>
97+
{{#each tags}}
98+
<li>{{{comment}}}</li>
99+
{{/each}}
100+
</ul>
101+
</div>
102+
{{/jsdoc-throws}}
93103
{{/if}}
94104
</td>
95105
</tr>

‎src/templates/partials/block-host-listener.hbs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@
179179
</div>
180180
{{/if}}
181181
{{/if}}
182+
{{#if jsdoctags}}
183+
{{#jsdoc-throws jsdoctags}}
184+
<div class="io-description">
185+
<b>{{t "throws" }} :</b>
186+
<ul>
187+
{{#each tags}}
188+
<li>{{{comment}}}</li>
189+
{{/each}}
190+
</ul>
191+
</div>
192+
{{/jsdoc-throws}}
193+
{{/if}}
182194
</td>
183195
</tr>
184196
{{/or}}

‎src/templates/partials/block-method.hbs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@
182182
</div>
183183
{{/if}}
184184
{{/if}}
185+
{{#if jsdoctags}}
186+
{{#jsdoc-throws jsdoctags}}
187+
<div class="io-description">
188+
<b>{{t "throws" }} :</b>
189+
<ul>
190+
{{#each tags}}
191+
<li>{{{comment}}}</li>
192+
{{/each}}
193+
</ul>
194+
</div>
195+
{{/jsdoc-throws}}
196+
{{/if}}
185197
</td>
186198
</tr>
187199
{{/or}}

‎src/templates/partials/interface-api.hbs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@
181181
</tr>
182182
{{/if}}
183183

184+
{{#if jsdoctags}}
185+
{{#jsdoc-throws jsdoctags}}
186+
<tr>
187+
<td class="col-md-4">
188+
<div class="io-description">
189+
<b>{{t "throws" }} :</b>
190+
<ul>
191+
{{#each tags}}
192+
<li>{{{comment}}}</li>
193+
{{/each}}
194+
</ul>
195+
</div>
196+
</td>
197+
</tr>
198+
{{/jsdoc-throws}}
199+
{{/if}}
200+
184201
{{#if jsdoctags}}
185202
{{#jsdoc-params-valid jsdoctags}}
186203
<tr>

‎src/utils/utils.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function mergeTagsAndArgs(args: Array<any>, jsdoctags?: Array<any>): Arra
6161
});
6262
}
6363
});
64-
// Add example & returns & private
64+
// Add example, returns, throws & private
6565
if (jsdoctags) {
6666
_.forEach(jsdoctags, jsdoctag => {
6767
if (
@@ -77,15 +77,24 @@ export function mergeTagsAndArgs(args: Array<any>, jsdoctags?: Array<any>): Arra
7777
jsdoctag.tagName &&
7878
(jsdoctag.tagName.text === 'returns' || jsdoctag.tagName.text === 'return')
7979
) {
80-
const ret: { tagName: string; comment: string; returnType?: string } = {
80+
const ret: { tagName: any; comment: string; returnType?: string } = {
8181
tagName: jsdoctag.tagName,
8282
comment: jsdoctag.comment
8383
};
84-
if (jsdoctag.typeExpression && jsdoctag.typeExpression.type) {
84+
if (jsdoctag.typeExpression?.type) {
8585
ret.returnType = kindToType(jsdoctag.typeExpression.type.kind);
8686
}
8787
margs.push(ret);
8888
}
89+
if (
90+
jsdoctag.tagName &&
91+
['throws', 'throw', 'exception'].includes(jsdoctag.tagName.text)
92+
) {
93+
margs.push({
94+
tagName: jsdoctag.tagName,
95+
comment: jsdoctag.comment
96+
});
97+
}
8998
});
9099
}
91100
return margs;

0 commit comments

Comments
 (0)