Skip to content

Commit 429b3a9

Browse files
committed
fix: Remove tags containing redundant type info
Closes #1198
1 parent eefdb2c commit 429b3a9

File tree

4 files changed

+72
-29
lines changed

4 files changed

+72
-29
lines changed

src/lib/converter/plugins/CommentPlugin.ts

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ import { Context } from '../context';
1616
import { partition, uniq } from 'lodash';
1717
import { SourceReference } from '../../models';
1818

19+
/**
20+
* These tags are not useful to display in the generated documentation.
21+
* They should be ignored when parsing comments. Any relevant type information
22+
* (for JS users) will be consumed by TypeScript and need not be preserved
23+
* in the comment.
24+
*
25+
* Note that param/arg/argument/return/returns are not present.
26+
* These tags will have their type information stripped when parsing, but still
27+
* provide useful information for documentation.
28+
*/
29+
const TAG_BLACKLIST = [
30+
'augments',
31+
'callback',
32+
'class',
33+
'constructor',
34+
'enum',
35+
'extends',
36+
'this',
37+
'type',
38+
'typedef'
39+
];
40+
1941
/**
2042
* Structure used by [[ContainerCommentHandler]] to store discovered module comments.
2143
*/
@@ -37,7 +59,7 @@ interface ModuleComment {
3759
}
3860

3961
/**
40-
* A handler that parses javadoc comments and attaches [[Models.Comment]] instances to
62+
* A handler that parses TypeDoc comments and attaches [[Comment]] instances to
4163
* the generated reflections.
4264
*/
4365
@Component({name: 'comment'})
@@ -92,27 +114,27 @@ export class CommentPlugin extends ConverterComponent {
92114
private applyModifiers(reflection: Reflection, comment: Comment) {
93115
if (comment.hasTag('private')) {
94116
reflection.setFlag(ReflectionFlag.Private);
95-
CommentPlugin.removeTags(comment, 'private');
117+
comment.removeTags('private');
96118
}
97119

98120
if (comment.hasTag('protected')) {
99121
reflection.setFlag(ReflectionFlag.Protected);
100-
CommentPlugin.removeTags(comment, 'protected');
122+
comment.removeTags('protected');
101123
}
102124

103125
if (comment.hasTag('public')) {
104126
reflection.setFlag(ReflectionFlag.Public);
105-
CommentPlugin.removeTags(comment, 'public');
127+
comment.removeTags('public');
106128
}
107129

108130
if (comment.hasTag('event')) {
109131
reflection.kind = ReflectionKind.Event;
110132
// reflection.setFlag(ReflectionFlag.Event);
111-
CommentPlugin.removeTags(comment, 'event');
133+
comment.removeTags('event');
112134
}
113135

114136
if (reflection.kindOf(ReflectionKind.ExternalModule)) {
115-
CommentPlugin.removeTags(comment, 'packagedocumentation');
137+
comment.removeTags('packagedocumentation');
116138
}
117139
}
118140

@@ -172,11 +194,13 @@ export class CommentPlugin extends ConverterComponent {
172194
if (reflection.kindOf(ReflectionKind.FunctionOrMethod) || (reflection.kindOf(ReflectionKind.Event) && reflection['signatures'])) {
173195
const comment = parseComment(rawComment, reflection.comment);
174196
this.applyModifiers(reflection, comment);
197+
this.removeBlacklistedTags(comment);
175198
} else if (reflection.kindOf(ReflectionKind.Module)) {
176199
this.storeModuleComment(rawComment, reflection);
177200
} else {
178201
const comment = parseComment(rawComment, reflection.comment);
179202
this.applyModifiers(reflection, comment);
203+
this.removeBlacklistedTags(comment);
180204
reflection.comment = comment;
181205
}
182206
}
@@ -212,7 +236,7 @@ export class CommentPlugin extends ConverterComponent {
212236

213237
const info = this.comments[id];
214238
const comment = parseComment(info.fullText);
215-
CommentPlugin.removeTags(comment, 'preferred');
239+
comment.removeTags('preferred');
216240

217241
this.applyModifiers(info.reflection, comment);
218242
info.reflection.comment = comment;
@@ -261,14 +285,14 @@ export class CommentPlugin extends ConverterComponent {
261285
const comment = reflection.comment;
262286
if (comment && comment.hasTag('returns')) {
263287
comment.returns = comment.getTag('returns')!.text;
264-
CommentPlugin.removeTags(comment, 'returns');
288+
comment.removeTags('returns');
265289
}
266290

267291
signatures.forEach((signature) => {
268292
let childComment = signature.comment;
269293
if (childComment && childComment.hasTag('returns')) {
270294
childComment.returns = childComment.getTag('returns')!.text;
271-
CommentPlugin.removeTags(childComment, 'returns');
295+
childComment.removeTags('returns');
272296
}
273297

274298
if (comment) {
@@ -296,33 +320,29 @@ export class CommentPlugin extends ConverterComponent {
296320
});
297321
}
298322

299-
CommentPlugin.removeTags(childComment, 'param');
323+
childComment?.removeTags('param');
300324
});
301325

302-
CommentPlugin.removeTags(comment, 'param');
326+
comment?.removeTags('param');
327+
}
328+
}
329+
330+
private removeBlacklistedTags(comment: Comment) {
331+
for (const tag of TAG_BLACKLIST) {
332+
comment.removeTags(tag);
303333
}
304334
}
305335

306336
/**
307337
* Remove all tags with the given name from the given comment instance.
338+
* @deprecated Use [[Comment.removeTags]] instead.
339+
* Warn in 0.17, remove in 0.18.
308340
*
309341
* @param comment The comment that should be modified.
310342
* @param tagName The name of the that that should be removed.
311343
*/
312344
static removeTags(comment: Comment | undefined, tagName: string) {
313-
if (!comment || !comment.tags) {
314-
return;
315-
}
316-
317-
let i = 0, c = comment.tags.length;
318-
while (i < c) {
319-
if (comment.tags[i].tagName === tagName) {
320-
comment.tags.splice(i, 1);
321-
c--;
322-
} else {
323-
i++;
324-
}
325-
}
345+
comment?.removeTags(tagName);
326346
}
327347

328348
/**
@@ -346,7 +366,7 @@ export class CommentPlugin extends ConverterComponent {
346366
}
347367

348368
/**
349-
* Determins whether or not a reflection has been hidden
369+
* Determines whether or not a reflection has been hidden
350370
*
351371
* @param reflection Reflection to check if hidden
352372
*/

src/lib/models/comments/comment.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ export class Comment {
7878
});
7979
}
8080

81+
/**
82+
* Removes all tags with the given tag name from teh comment.
83+
* @param tagName
84+
*/
85+
removeTags(tagName: string) {
86+
if (!this.tags) {
87+
return;
88+
}
89+
90+
let i = 0, c = this.tags.length ?? 0;
91+
while (i < c) {
92+
if (this.tags[i].tagName === tagName) {
93+
this.tags.splice(i, 1);
94+
c--;
95+
} else {
96+
i++;
97+
}
98+
}
99+
}
100+
81101
/**
82102
* Copy the data of the given comment into this comment.
83103
*

src/test/converter/comment/comment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import './comment2';
2626
* ```
2727
* @deprecated
2828
* @todo something
29+
*
30+
* @class will be removed
31+
* @type {Data<object>} will also be removed
2932
*/
3033
export class CommentedClass {
3134
/**

src/test/converter/comment/specs.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"sources": [
5555
{
5656
"fileName": "comment.ts",
57-
"line": 34,
57+
"line": 37,
5858
"character": 6
5959
}
6060
],
@@ -120,7 +120,7 @@
120120
"sources": [
121121
{
122122
"fileName": "comment.ts",
123-
"line": 74,
123+
"line": 77,
124124
"character": 13
125125
}
126126
]
@@ -145,7 +145,7 @@
145145
"sources": [
146146
{
147147
"fileName": "comment.ts",
148-
"line": 30,
148+
"line": 33,
149149
"character": 27
150150
}
151151
]
@@ -198,7 +198,7 @@
198198
"sources": [
199199
{
200200
"fileName": "comment.ts",
201-
"line": 88,
201+
"line": 91,
202202
"character": 22
203203
}
204204
]

0 commit comments

Comments
 (0)