Skip to content

Commit

Permalink
fix: Categories should only appear once if specified multiple times
Browse files Browse the repository at this point in the history
Closes #1522.
  • Loading branch information
Gerrit0 committed Mar 6, 2021
1 parent 34d05f2 commit 6949a1f
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 37 deletions.
32 changes: 15 additions & 17 deletions src/lib/converter/plugins/CategoryPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class CategoryPlugin extends ConverterComponent {
let defaultCat: ReflectionCategory | undefined;
reflections.forEach((child) => {
const childCategories = CategoryPlugin.getCategories(child);
if (childCategories.length === 0) {
if (childCategories.size === 0) {
if (!defaultCat) {
defaultCat = categories.find(
(category) =>
Expand Down Expand Up @@ -177,9 +177,9 @@ export class CategoryPlugin extends ConverterComponent {
* @param reflection The reflection.
* @returns The category the reflection belongs to
*/
static getCategories(reflection: Reflection): string[] {
function extractCategoryTag(comment: Comment): string[] {
const categories: string[] = [];
static getCategories(reflection: Reflection) {
function extractCategoryTag(comment: Comment) {
const categories = new Set<string>();
const tags = comment.tags;
const commentTags: CommentTag[] = [];
tags.forEach((tag) => {
Expand All @@ -191,31 +191,29 @@ export class CategoryPlugin extends ConverterComponent {
if (!text) {
return;
}
categories.push(text);
categories.add(text);
});
comment.tags = commentTags;
return categories;
}

const categories = new Set<string>();

if (reflection.comment) {
return extractCategoryTag(reflection.comment);
} else if (
reflection instanceof DeclarationReflection &&
reflection.signatures
) {
return reflection.signatures.reduce(
(categories: string[], signature) => {
if (!signature.comment) {
return categories;
}
return categories.concat(
extractCategoryTag(signature.comment)
);
},
[]
);
for (const sig of reflection.signatures) {
for (const cat of sig.comment
? extractCategoryTag(sig.comment)
: []) {
categories.add(cat);
}
}
}
return [];
return categories;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/test/converter/comment/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,10 @@ export interface GH1490 {
/** With comment */
optionalMethod?(): void;
}

export declare namespace GH1522 {
/** @category cat */
export function over(): string;
/** @category cat */
export function over(x: number): number;
}
119 changes: 99 additions & 20 deletions src/test/converter/comment/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,78 @@
"shortText": "This is a module doc comment with legacy behavior."
},
"children": [
{
"id": 32,
"name": "GH1522",
"kind": 2,
"kindString": "Namespace",
"flags": {},
"children": [
{
"id": 33,
"name": "over",
"kind": 64,
"kindString": "Function",
"flags": {},
"signatures": [
{
"id": 34,
"name": "over",
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"comment": {},
"type": {
"type": "intrinsic",
"name": "string"
}
},
{
"id": 35,
"name": "over",
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"comment": {},
"parameters": [
{
"id": 36,
"name": "x",
"kind": 32768,
"kindString": "Parameter",
"flags": {},
"type": {
"type": "intrinsic",
"name": "number"
}
}
],
"type": {
"type": "intrinsic",
"name": "number"
}
}
]
}
],
"groups": [
{
"title": "Functions",
"kind": 64,
"children": [
33
],
"categories": [
{
"title": "cat",
"children": [
33
]
}
]
}
]
},
{
"id": 5,
"name": "CommentedClass",
Expand Down Expand Up @@ -227,6 +299,13 @@
}
],
"groups": [
{
"title": "Namespaces",
"kind": 2,
"children": [
32
]
},
{
"title": "Classes",
"kind": 128,
Expand All @@ -251,7 +330,7 @@
]
},
{
"id": 32,
"id": 37,
"name": "comment2",
"kind": 1,
"kindString": "Module",
Expand All @@ -262,21 +341,21 @@
},
"children": [
{
"id": 33,
"id": 38,
"name": "multiply",
"kind": 64,
"kindString": "Function",
"flags": {},
"signatures": [
{
"id": 34,
"id": 39,
"name": "multiply",
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"parameters": [
{
"id": 35,
"id": 40,
"name": "a",
"kind": 32768,
"kindString": "Parameter",
Expand All @@ -287,7 +366,7 @@
}
},
{
"id": 36,
"id": 41,
"name": "b",
"kind": 32768,
"kindString": "Parameter",
Expand All @@ -311,13 +390,13 @@
"title": "Functions",
"kind": 64,
"children": [
33
38
]
}
]
},
{
"id": 37,
"id": 42,
"name": "comment3",
"kind": 1,
"kindString": "Module",
Expand All @@ -327,21 +406,21 @@
},
"children": [
{
"id": 38,
"id": 43,
"name": "multiply",
"kind": 64,
"kindString": "Function",
"flags": {},
"signatures": [
{
"id": 39,
"id": 44,
"name": "multiply",
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"parameters": [
{
"id": 40,
"id": 45,
"name": "a",
"kind": 32768,
"kindString": "Parameter",
Expand All @@ -352,7 +431,7 @@
}
},
{
"id": 41,
"id": 46,
"name": "b",
"kind": 32768,
"kindString": "Parameter",
Expand All @@ -376,13 +455,13 @@
"title": "Functions",
"kind": 64,
"children": [
38
43
]
}
]
},
{
"id": 42,
"id": 47,
"name": "comment4",
"kind": 1,
"kindString": "Module",
Expand All @@ -392,21 +471,21 @@
},
"children": [
{
"id": 43,
"id": 48,
"name": "multiply",
"kind": 64,
"kindString": "Function",
"flags": {},
"signatures": [
{
"id": 44,
"id": 49,
"name": "multiply",
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"parameters": [
{
"id": 45,
"id": 50,
"name": "a",
"kind": 32768,
"kindString": "Parameter",
Expand All @@ -417,7 +496,7 @@
}
},
{
"id": 46,
"id": 51,
"name": "b",
"kind": 32768,
"kindString": "Parameter",
Expand All @@ -441,7 +520,7 @@
"title": "Functions",
"kind": 64,
"children": [
43
48
]
}
]
Expand All @@ -453,9 +532,9 @@
"kind": 1,
"children": [
1,
32,
37,
42
42,
47
]
}
]
Expand Down

0 comments on commit 6949a1f

Please sign in to comment.