Skip to content

Commit edb95aa

Browse files
committed
fix: use nearest JSDoc comment for variable docs
Closes #1274
1 parent 92a8480 commit edb95aa

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎src/app/compiler/angular-dependencies.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,11 +1324,17 @@ export class AngularDependencies extends FrameworkDependencies {
13241324
if (
13251325
variableNode.jsDoc &&
13261326
variableNode.jsDoc.length > 0 &&
1327-
variableNode.jsDoc[0].comment
1327+
variableNode.jsDoc[
1328+
variableNode.jsDoc.length - 1
1329+
].comment
13281330
) {
1331+
const lastJsDoc =
1332+
variableNode.jsDoc[
1333+
variableNode.jsDoc.length - 1
1334+
];
13291335
const rawDescription =
13301336
this.jsdocParserUtil.parseJSDocNode(
1331-
variableNode.jsDoc[0],
1337+
lastJsDoc,
13321338
);
13331339
deps.rawdescription = rawDescription;
13341340
deps.description =

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,13 @@ export class ClassHelper {
269269
}
270270

271271
/**
272-
* Set fallback description from jsDoc[0].comment if no description exists
272+
* Set fallback description from the closest JSDoc comment if no description exists
273273
*/
274274
private setFallbackDescription(result: any, node: any): void {
275275
if (!result.description && node.jsDoc && node.jsDoc.length > 0) {
276-
if (typeof node.jsDoc[0].comment !== "undefined") {
277-
const rawDescription = node.jsDoc[0].comment;
276+
const lastJsDoc = node.jsDoc[node.jsDoc.length - 1];
277+
if (typeof lastJsDoc.comment !== "undefined") {
278+
const rawDescription = lastJsDoc.comment;
278279
result.rawdescription = rawDescription;
279280
result.description = markedAcl(rawDescription);
280281
}

‎test/src/utils/jsdoc-parser.util.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ describe('Utils - JsdocParserUtil', () => {
153153
expect(result).to.equal('/** comment */');
154154
});
155155

156+
it('should return the closest variable comment when a file header JSDoc exists', () => {
157+
const sourceFile = project.createSourceFile('test.ts', `/**
158+
* TEST FILE
159+
* @file test file
160+
*/
161+
/** first constant */
162+
const FIRST = 1;
163+
/** second constant */
164+
const SECOND = 2;`);
165+
const variableDeclaration = sourceFile.getVariableDeclaration('FIRST');
166+
167+
const result = jsdocParserUtil.getMainCommentOfNode(variableDeclaration!.compilerNode, sourceFile.compilerNode);
168+
169+
expect(result).to.equal('/** first constant */');
170+
});
171+
156172
it('should return comment for function declaration', () => {
157173
const sourceFile = project.createSourceFile('test.ts', '/** comment */ function test() {}');
158174
const functionDeclaration = sourceFile.getFunction('test');

0 commit comments

Comments
 (0)