Skip to content

Commit 6949a1f

Browse files
committed
fix: Categories should only appear once if specified multiple times
Closes TypeStrong#1522.
1 parent 34d05f2 commit 6949a1f

File tree

3 files changed

+121
-37
lines changed

3 files changed

+121
-37
lines changed

src/lib/converter/plugins/CategoryPlugin.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class CategoryPlugin extends ConverterComponent {
141141
let defaultCat: ReflectionCategory | undefined;
142142
reflections.forEach((child) => {
143143
const childCategories = CategoryPlugin.getCategories(child);
144-
if (childCategories.length === 0) {
144+
if (childCategories.size === 0) {
145145
if (!defaultCat) {
146146
defaultCat = categories.find(
147147
(category) =>
@@ -177,9 +177,9 @@ export class CategoryPlugin extends ConverterComponent {
177177
* @param reflection The reflection.
178178
* @returns The category the reflection belongs to
179179
*/
180-
static getCategories(reflection: Reflection): string[] {
181-
function extractCategoryTag(comment: Comment): string[] {
182-
const categories: string[] = [];
180+
static getCategories(reflection: Reflection) {
181+
function extractCategoryTag(comment: Comment) {
182+
const categories = new Set<string>();
183183
const tags = comment.tags;
184184
const commentTags: CommentTag[] = [];
185185
tags.forEach((tag) => {
@@ -191,31 +191,29 @@ export class CategoryPlugin extends ConverterComponent {
191191
if (!text) {
192192
return;
193193
}
194-
categories.push(text);
194+
categories.add(text);
195195
});
196196
comment.tags = commentTags;
197197
return categories;
198198
}
199199

200+
const categories = new Set<string>();
201+
200202
if (reflection.comment) {
201203
return extractCategoryTag(reflection.comment);
202204
} else if (
203205
reflection instanceof DeclarationReflection &&
204206
reflection.signatures
205207
) {
206-
return reflection.signatures.reduce(
207-
(categories: string[], signature) => {
208-
if (!signature.comment) {
209-
return categories;
210-
}
211-
return categories.concat(
212-
extractCategoryTag(signature.comment)
213-
);
214-
},
215-
[]
216-
);
208+
for (const sig of reflection.signatures) {
209+
for (const cat of sig.comment
210+
? extractCategoryTag(sig.comment)
211+
: []) {
212+
categories.add(cat);
213+
}
214+
}
217215
}
218-
return [];
216+
return categories;
219217
}
220218

221219
/**

src/test/converter/comment/comment.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,10 @@ export interface GH1490 {
9696
/** With comment */
9797
optionalMethod?(): void;
9898
}
99+
100+
export declare namespace GH1522 {
101+
/** @category cat */
102+
export function over(): string;
103+
/** @category cat */
104+
export function over(x: number): number;
105+
}

src/test/converter/comment/specs.json

Lines changed: 99 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,78 @@
1414
"shortText": "This is a module doc comment with legacy behavior."
1515
},
1616
"children": [
17+
{
18+
"id": 32,
19+
"name": "GH1522",
20+
"kind": 2,
21+
"kindString": "Namespace",
22+
"flags": {},
23+
"children": [
24+
{
25+
"id": 33,
26+
"name": "over",
27+
"kind": 64,
28+
"kindString": "Function",
29+
"flags": {},
30+
"signatures": [
31+
{
32+
"id": 34,
33+
"name": "over",
34+
"kind": 4096,
35+
"kindString": "Call signature",
36+
"flags": {},
37+
"comment": {},
38+
"type": {
39+
"type": "intrinsic",
40+
"name": "string"
41+
}
42+
},
43+
{
44+
"id": 35,
45+
"name": "over",
46+
"kind": 4096,
47+
"kindString": "Call signature",
48+
"flags": {},
49+
"comment": {},
50+
"parameters": [
51+
{
52+
"id": 36,
53+
"name": "x",
54+
"kind": 32768,
55+
"kindString": "Parameter",
56+
"flags": {},
57+
"type": {
58+
"type": "intrinsic",
59+
"name": "number"
60+
}
61+
}
62+
],
63+
"type": {
64+
"type": "intrinsic",
65+
"name": "number"
66+
}
67+
}
68+
]
69+
}
70+
],
71+
"groups": [
72+
{
73+
"title": "Functions",
74+
"kind": 64,
75+
"children": [
76+
33
77+
],
78+
"categories": [
79+
{
80+
"title": "cat",
81+
"children": [
82+
33
83+
]
84+
}
85+
]
86+
}
87+
]
88+
},
1789
{
1890
"id": 5,
1991
"name": "CommentedClass",
@@ -227,6 +299,13 @@
227299
}
228300
],
229301
"groups": [
302+
{
303+
"title": "Namespaces",
304+
"kind": 2,
305+
"children": [
306+
32
307+
]
308+
},
230309
{
231310
"title": "Classes",
232311
"kind": 128,
@@ -251,7 +330,7 @@
251330
]
252331
},
253332
{
254-
"id": 32,
333+
"id": 37,
255334
"name": "comment2",
256335
"kind": 1,
257336
"kindString": "Module",
@@ -262,21 +341,21 @@
262341
},
263342
"children": [
264343
{
265-
"id": 33,
344+
"id": 38,
266345
"name": "multiply",
267346
"kind": 64,
268347
"kindString": "Function",
269348
"flags": {},
270349
"signatures": [
271350
{
272-
"id": 34,
351+
"id": 39,
273352
"name": "multiply",
274353
"kind": 4096,
275354
"kindString": "Call signature",
276355
"flags": {},
277356
"parameters": [
278357
{
279-
"id": 35,
358+
"id": 40,
280359
"name": "a",
281360
"kind": 32768,
282361
"kindString": "Parameter",
@@ -287,7 +366,7 @@
287366
}
288367
},
289368
{
290-
"id": 36,
369+
"id": 41,
291370
"name": "b",
292371
"kind": 32768,
293372
"kindString": "Parameter",
@@ -311,13 +390,13 @@
311390
"title": "Functions",
312391
"kind": 64,
313392
"children": [
314-
33
393+
38
315394
]
316395
}
317396
]
318397
},
319398
{
320-
"id": 37,
399+
"id": 42,
321400
"name": "comment3",
322401
"kind": 1,
323402
"kindString": "Module",
@@ -327,21 +406,21 @@
327406
},
328407
"children": [
329408
{
330-
"id": 38,
409+
"id": 43,
331410
"name": "multiply",
332411
"kind": 64,
333412
"kindString": "Function",
334413
"flags": {},
335414
"signatures": [
336415
{
337-
"id": 39,
416+
"id": 44,
338417
"name": "multiply",
339418
"kind": 4096,
340419
"kindString": "Call signature",
341420
"flags": {},
342421
"parameters": [
343422
{
344-
"id": 40,
423+
"id": 45,
345424
"name": "a",
346425
"kind": 32768,
347426
"kindString": "Parameter",
@@ -352,7 +431,7 @@
352431
}
353432
},
354433
{
355-
"id": 41,
434+
"id": 46,
356435
"name": "b",
357436
"kind": 32768,
358437
"kindString": "Parameter",
@@ -376,13 +455,13 @@
376455
"title": "Functions",
377456
"kind": 64,
378457
"children": [
379-
38
458+
43
380459
]
381460
}
382461
]
383462
},
384463
{
385-
"id": 42,
464+
"id": 47,
386465
"name": "comment4",
387466
"kind": 1,
388467
"kindString": "Module",
@@ -392,21 +471,21 @@
392471
},
393472
"children": [
394473
{
395-
"id": 43,
474+
"id": 48,
396475
"name": "multiply",
397476
"kind": 64,
398477
"kindString": "Function",
399478
"flags": {},
400479
"signatures": [
401480
{
402-
"id": 44,
481+
"id": 49,
403482
"name": "multiply",
404483
"kind": 4096,
405484
"kindString": "Call signature",
406485
"flags": {},
407486
"parameters": [
408487
{
409-
"id": 45,
488+
"id": 50,
410489
"name": "a",
411490
"kind": 32768,
412491
"kindString": "Parameter",
@@ -417,7 +496,7 @@
417496
}
418497
},
419498
{
420-
"id": 46,
499+
"id": 51,
421500
"name": "b",
422501
"kind": 32768,
423502
"kindString": "Parameter",
@@ -441,7 +520,7 @@
441520
"title": "Functions",
442521
"kind": 64,
443522
"children": [
444-
43
523+
48
445524
]
446525
}
447526
]
@@ -453,9 +532,9 @@
453532
"kind": 1,
454533
"children": [
455534
1,
456-
32,
457535
37,
458-
42
536+
42,
537+
47
459538
]
460539
}
461540
]

0 commit comments

Comments
 (0)