Skip to content

Commit b8ca9ac

Browse files
committed
fix(58145): suppress jsdoc for private members
1 parent 9a8581c commit b8ca9ac

10 files changed

Lines changed: 302 additions & 34 deletions

tsc/internal/transformers/declarations/transform.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -992,47 +992,61 @@ func (tx *DeclarationTransformer) transformPropertyDeclaration(input *ast.Proper
992992
if postfixToken != nil && postfixToken.Kind == ast.KindExclamationToken {
993993
postfixToken = nil
994994
}
995-
return tx.Factory().UpdatePropertyDeclaration(
995+
result := tx.Factory().UpdatePropertyDeclaration(
996996
input,
997997
tx.ensureModifiers(input.AsNode()),
998998
input.Name(),
999999
postfixToken,
10001000
tx.ensureType(input.AsNode(), false),
10011001
tx.ensureNoInitializer(input.AsNode()),
10021002
)
1003+
if tx.host.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0 {
1004+
tx.suppressJsDoc(result)
1005+
}
1006+
return result
10031007
}
10041008

10051009
func (tx *DeclarationTransformer) transformSetAccessorDeclaration(input *ast.SetAccessorDeclaration) *ast.Node {
10061010
if ast.IsPrivateIdentifier(input.Name()) {
10071011
return nil
10081012
}
10091013

1010-
return tx.Factory().UpdateSetAccessorDeclaration(
1014+
isPrivate := tx.host.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0
1015+
result := tx.Factory().UpdateSetAccessorDeclaration(
10111016
input,
10121017
tx.ensureModifiers(input.AsNode()),
10131018
input.Name(),
10141019
nil, // accessors shouldn't have type params
1015-
tx.updateAccessorParamList(input.AsNode(), tx.host.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0),
1020+
tx.updateAccessorParamList(input.AsNode(), isPrivate),
10161021
nil,
10171022
nil,
10181023
nil,
10191024
)
1025+
if isPrivate {
1026+
tx.suppressJsDoc(result)
1027+
}
1028+
return result
10201029
}
10211030

10221031
func (tx *DeclarationTransformer) transformGetAccesorDeclaration(input *ast.GetAccessorDeclaration) *ast.Node {
10231032
if ast.IsPrivateIdentifier(input.Name()) {
10241033
return nil
10251034
}
1026-
return tx.Factory().UpdateGetAccessorDeclaration(
1035+
isPrivate := tx.host.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0
1036+
result := tx.Factory().UpdateGetAccessorDeclaration(
10271037
input,
10281038
tx.ensureModifiers(input.AsNode()),
10291039
input.Name(),
10301040
nil, // accessors shouldn't have type params
1031-
tx.updateAccessorParamList(input.AsNode(), tx.host.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0),
1041+
tx.updateAccessorParamList(input.AsNode(), isPrivate),
10321042
tx.ensureType(input.AsNode(), false),
10331043
nil,
10341044
nil,
10351045
)
1046+
if isPrivate {
1047+
tx.suppressJsDoc(result)
1048+
}
1049+
return result
10361050
}
10371051

10381052
func (tx *DeclarationTransformer) updateAccessorParamList(input *ast.Node, isPrivate bool) *ast.ParameterList {
@@ -1105,10 +1119,14 @@ func (tx *DeclarationTransformer) omitPrivateMethodType(input *ast.Node) *ast.No
11051119
nil,
11061120
nil,
11071121
)
1108-
tx.preserveJsDoc(result, input)
1122+
tx.suppressJsDoc(result)
11091123
return result
11101124
}
11111125

1126+
func (tx *DeclarationTransformer) suppressJsDoc(node *ast.Node) {
1127+
tx.EmitContext().AddEmitFlags(node, printer.EFNoComments|printer.EFNoNestedComments)
1128+
}
1129+
11121130
func (tx *DeclarationTransformer) transformMethodSignatureDeclaration(input *ast.MethodSignatureDeclaration) *ast.Node {
11131131
if tx.host.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0 {
11141132
return tx.omitPrivateMethodType(input.AsNode())
@@ -1940,7 +1958,11 @@ func (tx *DeclarationTransformer) buildClassMembers(classNode *ast.Node, extraMe
19401958
tx.ensureType(param, false),
19411959
tx.ensureNoInitializer(param),
19421960
)
1943-
tx.preserveJsDoc(updated, param)
1961+
if tx.host.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(param), ast.ModifierFlagsPrivate) != 0 {
1962+
tx.suppressJsDoc(updated)
1963+
} else {
1964+
tx.preserveJsDoc(updated, param)
1965+
}
19441966
parameterProperties = append(parameterProperties, updated)
19451967
} else {
19461968
// Pattern - this is currently an error, but we emit declarations for it somewhat correctly

tsc/testdata/baselines/reference/compiler/commentsClassMembers(target=es2015).js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,9 @@ declare class c1 {
423423
get p3(): number;
424424
/** setter property*/
425425
set p3(/** this is value*/ value: number);
426-
/** pp1 is property of c1*/
427426
private pp1;
428-
/** sum with property*/
429427
private pp2;
430-
/** getter property*/
431428
private get pp3();
432-
/** setter property*/
433429
private set pp3(value);
434430
/** Constructor method*/
435431
constructor();
@@ -473,13 +469,9 @@ declare class c1 {
473469
get b_p3(): number;
474470
/** setter property */
475471
set b_p3(value: number);
476-
/** pp1 is property of c1 */
477472
private b_pp1;
478-
/** sum with property */
479473
private b_pp2;
480-
/** getter property */
481474
private get b_pp3();
482-
/** setter property */
483475
private set b_pp3(value);
484476
/** s1 is static property of c1 */
485477
static b_s1: number;

tsc/testdata/baselines/reference/compiler/declFileAccessors(target=es2015).js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,7 @@ export declare class c1 {
208208
get p3(): number;
209209
/** setter property*/
210210
set p3(/** this is value*/ value: number);
211-
/** private getter property*/
212211
private get pp3();
213-
/** private setter property*/
214212
private set pp3(value);
215213
/** static getter property*/
216214
static get s3(): number;
@@ -232,9 +230,7 @@ declare class c2 {
232230
get p3(): number;
233231
/** setter property*/
234232
set p3(/** this is value*/ value: number);
235-
/** private getter property*/
236233
private get pp3();
237-
/** private setter property*/
238234
private set pp3(value);
239235
/** static getter property*/
240236
static get s3(): number;

tsc/testdata/baselines/reference/compiler/declFileMethods(target=es2015).js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,7 @@ export declare class c1 {
333333
fooWithRestParameters(a: string, ...rests: string[]): string;
334334
fooWithOverloads(a: string): string;
335335
fooWithOverloads(a: number): number;
336-
/** This comment should appear for privateFoo*/
337336
private privateFoo;
338-
/** This is comment for function signature*/
339337
private privateFooWithParameters;
340338
private privateFooWithRestParameters;
341339
private privateFooWithOverloads;
@@ -348,9 +346,7 @@ export declare class c1 {
348346
static staticFooWithRestParameters(a: string, ...rests: string[]): string;
349347
static staticFooWithOverloads(a: string): string;
350348
static staticFooWithOverloads(a: number): number;
351-
/** This comment should appear for privateStaticFoo*/
352349
private static privateStaticFoo;
353-
/** This is comment for function signature*/
354350
private static privateStaticFooWithParameters;
355351
private static privateStaticFooWithRestParameters;
356352
private static privateStaticFooWithOverloads;
@@ -377,9 +373,7 @@ declare class c2 {
377373
fooWithRestParameters(a: string, ...rests: string[]): string;
378374
fooWithOverloads(a: string): string;
379375
fooWithOverloads(a: number): number;
380-
/** This comment should appear for privateFoo*/
381376
private privateFoo;
382-
/** This is comment for function signature*/
383377
private privateFooWithParameters;
384378
private privateFooWithRestParameters;
385379
private privateFooWithOverloads;
@@ -392,9 +386,7 @@ declare class c2 {
392386
static staticFooWithRestParameters(a: string, ...rests: string[]): string;
393387
static staticFooWithOverloads(a: string): string;
394388
static staticFooWithOverloads(a: number): number;
395-
/** This comment should appear for privateStaticFoo*/
396389
private static privateStaticFoo;
397-
/** This is comment for function signature*/
398390
private static privateStaticFooWithParameters;
399391
private static privateStaticFooWithRestParameters;
400392
private static privateStaticFooWithOverloads;

tsc/testdata/baselines/reference/compiler/declarationEmitPrivateAsyncMethod.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ export class C {
3333

3434
//// [a.d.ts]
3535
export declare class C {
36-
/**
37-
* Non Async function
38-
*/
3936
private a;
40-
/**
41-
* Async function
42-
*/
4337
private b;
4438
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//// [tests/cases/compiler/declarationEmitPrivateMemberComments.ts] ////
2+
3+
//// [a.ts]
4+
export class A {
5+
/** Public property. */
6+
a = 1;
7+
8+
/** Private property. */
9+
private b = 1;
10+
11+
/** Private method. */
12+
private c() {}
13+
14+
/** Private getter. */
15+
private get d() { return 1; }
16+
17+
/** Private setter. */
18+
private set d(value: number) {}
19+
20+
/** ECMAScript private property. */
21+
#e = 1;
22+
23+
constructor(
24+
/** Private parameter property. */
25+
private f: number,
26+
) {}
27+
}
28+
29+
//// [b.js]
30+
export class B {
31+
/** Public property. */
32+
a = 1;
33+
34+
/** @private */
35+
b = 1;
36+
37+
/** @private */
38+
c() {}
39+
40+
/** @private */
41+
get d() { return 1; }
42+
43+
/** @private */
44+
set d(value) {}
45+
46+
/** ECMAScript private property. */
47+
#e = 1;
48+
}
49+
50+
51+
52+
53+
//// [a.d.ts]
54+
export declare class A {
55+
#private;
56+
private f;
57+
/** Public property. */
58+
a: number;
59+
private b;
60+
private c;
61+
private get d();
62+
private set d(value);
63+
constructor(
64+
/** Private parameter property. */
65+
f: number);
66+
}
67+
//// [b.d.ts]
68+
export declare class B {
69+
#private;
70+
/** Public property. */
71+
a: number;
72+
private b;
73+
private c;
74+
private get d();
75+
private set d(value);
76+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//// [tests/cases/compiler/declarationEmitPrivateMemberComments.ts] ////
2+
3+
=== a.ts ===
4+
export class A {
5+
>A : Symbol(A, Decl(a.ts, 0, 0))
6+
7+
/** Public property. */
8+
a = 1;
9+
>a : Symbol(A.a, Decl(a.ts, 0, 16))
10+
11+
/** Private property. */
12+
private b = 1;
13+
>b : Symbol(A.b, Decl(a.ts, 2, 10))
14+
15+
/** Private method. */
16+
private c() {}
17+
>c : Symbol(A.c, Decl(a.ts, 5, 18))
18+
19+
/** Private getter. */
20+
private get d() { return 1; }
21+
>d : Symbol(A.d, Decl(a.ts, 8, 18), Decl(a.ts, 11, 33))
22+
23+
/** Private setter. */
24+
private set d(value: number) {}
25+
>d : Symbol(A.d, Decl(a.ts, 8, 18), Decl(a.ts, 11, 33))
26+
>value : Symbol(value, Decl(a.ts, 14, 18))
27+
28+
/** ECMAScript private property. */
29+
#e = 1;
30+
>#e : Symbol(A.#e, Decl(a.ts, 14, 35))
31+
32+
constructor(
33+
/** Private parameter property. */
34+
private f: number,
35+
>f : Symbol(A.f, Decl(a.ts, 19, 16))
36+
37+
) {}
38+
}
39+
40+
=== b.js ===
41+
export class B {
42+
>B : Symbol(B, Decl(b.js, 0, 0))
43+
44+
/** Public property. */
45+
a = 1;
46+
>a : Symbol(B.a, Decl(b.js, 0, 16))
47+
48+
/** @private */
49+
b = 1;
50+
>b : Symbol(B.b, Decl(b.js, 2, 10))
51+
52+
/** @private */
53+
c() {}
54+
>c : Symbol(B.c, Decl(b.js, 5, 10))
55+
56+
/** @private */
57+
get d() { return 1; }
58+
>d : Symbol(B.d, Decl(b.js, 8, 10), Decl(b.js, 11, 25))
59+
60+
/** @private */
61+
set d(value) {}
62+
>d : Symbol(B.d, Decl(b.js, 8, 10), Decl(b.js, 11, 25))
63+
>value : Symbol(value, Decl(b.js, 14, 10))
64+
65+
/** ECMAScript private property. */
66+
#e = 1;
67+
>#e : Symbol(B.#e, Decl(b.js, 14, 19))
68+
}
69+

0 commit comments

Comments
 (0)