Skip to content

Commit 99d738f

Browse files
committed
fix: Prevent @hidden signature from removing method declaration
For TypeStrong#1142
1 parent 0eca73e commit 99d738f

File tree

3 files changed

+156
-28
lines changed

3 files changed

+156
-28
lines changed

src/lib/converter/plugins/CommentPlugin.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ export class CommentPlugin extends ConverterComponent {
4646
*/
4747
private comments!: {[id: number]: ModuleComment};
4848

49-
/**
50-
* List of hidden reflections.
51-
*/
52-
private hidden?: Reflection[];
53-
5449
/**
5550
* Create a new CommentPlugin instance.
5651
*/
@@ -114,16 +109,6 @@ export class CommentPlugin extends ConverterComponent {
114109
CommentPlugin.removeTags(comment, 'event');
115110
}
116111

117-
if (comment.hasTag('hidden')
118-
|| comment.hasTag('ignore')
119-
|| (comment.hasTag('internal') && this.application.options.getCompilerOptions().stripInternal)
120-
) {
121-
if (!this.hidden) {
122-
this.hidden = [];
123-
}
124-
this.hidden.push(reflection);
125-
}
126-
127112
if (reflection.kindOf(ReflectionKind.ExternalModule)) {
128113
CommentPlugin.removeTags(comment, 'packagedocumentation');
129114
}
@@ -135,7 +120,6 @@ export class CommentPlugin extends ConverterComponent {
135120
* @param context The context object describing the current state the converter is in.
136121
*/
137122
private onBegin(context: Context) {
138-
this.hidden = undefined;
139123
this.comments = {};
140124
}
141125

@@ -232,12 +216,22 @@ export class CommentPlugin extends ConverterComponent {
232216
info.reflection.comment = comment;
233217
}
234218

235-
if (this.hidden) {
236-
const project = context.project;
237-
for (const reflection of this.hidden) {
238-
project.removeReflection(reflection, true);
239-
}
240-
}
219+
const stripInternal = this.application.options.getCompilerOptions().stripInternal;
220+
221+
const project = context.project;
222+
const reflections = Object.values(project.reflections);
223+
224+
// remove signatures
225+
const hidden = reflections.filter(reflection => CommentPlugin.isHidden(reflection, stripInternal));
226+
hidden.forEach(reflection => project.removeReflection(reflection, true));
227+
228+
// remove functions with empty signatures after their signatures have been removed
229+
const hiddenMethods = hidden.map(reflection => reflection.parent!)
230+
.filter(method =>
231+
method.kindOf(ReflectionKind.FunctionOrMethod)
232+
&& method instanceof DeclarationReflection
233+
&& method.signatures?.length === 0);
234+
hiddenMethods.forEach(reflection => project.removeReflection(reflection, true));
241235
}
242236

243237
/**
@@ -345,4 +339,23 @@ export class CommentPlugin extends ConverterComponent {
345339
static removeReflection(project: ProjectReflection, reflection: Reflection) {
346340
project.removeReflection(reflection, true);
347341
}
342+
343+
/**
344+
* Determins whether or not a reflection has been hidden
345+
*
346+
* @param reflection Reflection to check if hidden
347+
*/
348+
private static isHidden(reflection: Reflection, stripInternal: boolean | undefined) {
349+
const comment = reflection.comment;
350+
351+
if (!comment) {
352+
return false;
353+
}
354+
355+
return (
356+
comment.hasTag('hidden')
357+
|| comment.hasTag('ignore')
358+
|| (comment.hasTag('internal') && stripInternal)
359+
);
360+
}
348361
}

src/test/converter/comment/comment.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,42 @@ export class CommentedClass {
3838
*/
3939
hiddenprop: string;
4040

41+
/**
42+
* Hidden function
43+
* @hidden
44+
*/
45+
hidden(...args: any[]): void {}
46+
47+
/**
48+
* Single hidden signature
49+
* @hidden
50+
*/
51+
hiddenWithImplementation(arg: any);
52+
hiddenWithImplementation(...args: any[]): void {}
53+
54+
/**
55+
* Multiple hidden 1
56+
* @hidden
57+
*/
58+
multipleHidden(arg: any);
59+
/**
60+
* Multiple hidden 2
61+
* @hidden
62+
*/
63+
multipleHidden(arg1: any, arg2: any);
64+
multipleHidden(...args: any[]): void {}
65+
66+
/**
67+
* Mixed hidden 1
68+
* @hidden
69+
*/
70+
mixedHidden(arg: any);
71+
/**
72+
* Mixed hidden 2
73+
*/
74+
mixedHidden(arg1: any, arg2: any);
75+
mixedHidden(...args: any[]): void {}
76+
4177
/**
4278
* @ignore
4379
*/

src/test/converter/comment/specs.json

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,78 @@
6262
"type": "intrinsic",
6363
"name": "string"
6464
}
65+
},
66+
{
67+
"id": 22,
68+
"name": "mixedHidden",
69+
"kind": 2048,
70+
"kindString": "Method",
71+
"flags": {
72+
"isExported": true
73+
},
74+
"signatures": [
75+
{
76+
"id": 25,
77+
"name": "mixedHidden",
78+
"kind": 4096,
79+
"kindString": "Call signature",
80+
"flags": {
81+
"isExported": true
82+
},
83+
"comment": {
84+
"shortText": "Mixed hidden 2"
85+
},
86+
"parameters": [
87+
{
88+
"id": 26,
89+
"name": "arg1",
90+
"kind": 32768,
91+
"kindString": "Parameter",
92+
"flags": {
93+
"isExported": true
94+
},
95+
"type": {
96+
"type": "intrinsic",
97+
"name": "any"
98+
}
99+
},
100+
{
101+
"id": 27,
102+
"name": "arg2",
103+
"kind": 32768,
104+
"kindString": "Parameter",
105+
"flags": {
106+
"isExported": true
107+
},
108+
"type": {
109+
"type": "intrinsic",
110+
"name": "any"
111+
}
112+
}
113+
],
114+
"type": {
115+
"type": "intrinsic",
116+
"name": "any"
117+
}
118+
}
119+
],
120+
"sources": [
121+
{
122+
"fileName": "comment.ts",
123+
"line": 70,
124+
"character": 13
125+
},
126+
{
127+
"fileName": "comment.ts",
128+
"line": 74,
129+
"character": 13
130+
},
131+
{
132+
"fileName": "comment.ts",
133+
"line": 75,
134+
"character": 13
135+
}
136+
]
65137
}
66138
],
67139
"groups": [
@@ -71,6 +143,13 @@
71143
"children": [
72144
8
73145
]
146+
},
147+
{
148+
"title": "Methods",
149+
"kind": 2048,
150+
"children": [
151+
22
152+
]
74153
}
75154
],
76155
"sources": [
@@ -82,7 +161,7 @@
82161
]
83162
},
84163
{
85-
"id": 11,
164+
"id": 29,
86165
"name": "gh1164",
87166
"kind": 64,
88167
"kindString": "Function",
@@ -91,7 +170,7 @@
91170
},
92171
"signatures": [
93172
{
94-
"id": 12,
173+
"id": 30,
95174
"name": "gh1164",
96175
"kind": 4096,
97176
"kindString": "Call signature",
@@ -104,7 +183,7 @@
104183
},
105184
"parameters": [
106185
{
107-
"id": 13,
186+
"id": 31,
108187
"name": "scope",
109188
"kind": 32768,
110189
"kindString": "Parameter",
@@ -129,7 +208,7 @@
129208
"sources": [
130209
{
131210
"fileName": "comment.ts",
132-
"line": 52,
211+
"line": 88,
133212
"character": 22
134213
}
135214
]
@@ -147,7 +226,7 @@
147226
"title": "Functions",
148227
"kind": 64,
149228
"children": [
150-
11
229+
29
151230
]
152231
}
153232
],
@@ -260,4 +339,4 @@
260339
]
261340
}
262341
]
263-
}
342+
}

0 commit comments

Comments
 (0)