diff --git a/tsc/internal/transformers/declarations/transform.go b/tsc/internal/transformers/declarations/transform.go index 7e362d9fb1660..7c27e604fcfb3 100644 --- a/tsc/internal/transformers/declarations/transform.go +++ b/tsc/internal/transformers/declarations/transform.go @@ -986,7 +986,7 @@ func (tx *DeclarationTransformer) transformPropertyDeclaration(input *ast.Proper if postfixToken != nil && postfixToken.Kind == ast.KindExclamationToken { postfixToken = nil } - return tx.Factory().UpdatePropertyDeclaration( + result := tx.Factory().UpdatePropertyDeclaration( input, tx.ensureModifiers(input.AsNode()), input.Name(), @@ -994,6 +994,10 @@ func (tx *DeclarationTransformer) transformPropertyDeclaration(input *ast.Proper tx.ensureType(input.AsNode(), false), tx.ensureNoInitializer(input.AsNode()), ) + if tx.resolver.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0 { + tx.suppressJsDoc(result) + } + return result } func (tx *DeclarationTransformer) transformSetAccessorDeclaration(input *ast.SetAccessorDeclaration) *ast.Node { @@ -1001,32 +1005,42 @@ func (tx *DeclarationTransformer) transformSetAccessorDeclaration(input *ast.Set return nil } - return tx.Factory().UpdateSetAccessorDeclaration( + isPrivate := tx.resolver.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0 + result := tx.Factory().UpdateSetAccessorDeclaration( input, tx.ensureModifiers(input.AsNode()), input.Name(), nil, // accessors shouldn't have type params - tx.updateAccessorParamList(input.AsNode(), tx.resolver.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0), + tx.updateAccessorParamList(input.AsNode(), isPrivate), nil, nil, nil, ) + if isPrivate { + tx.suppressJsDoc(result) + } + return result } func (tx *DeclarationTransformer) transformGetAccesorDeclaration(input *ast.GetAccessorDeclaration) *ast.Node { if ast.IsPrivateIdentifier(input.Name()) { return nil } - return tx.Factory().UpdateGetAccessorDeclaration( + isPrivate := tx.resolver.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0 + result := tx.Factory().UpdateGetAccessorDeclaration( input, tx.ensureModifiers(input.AsNode()), input.Name(), nil, // accessors shouldn't have type params - tx.updateAccessorParamList(input.AsNode(), tx.resolver.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0), + tx.updateAccessorParamList(input.AsNode(), isPrivate), tx.ensureType(input.AsNode(), false), nil, nil, ) + if isPrivate { + tx.suppressJsDoc(result) + } + return result } func (tx *DeclarationTransformer) updateAccessorParamList(input *ast.Node, isPrivate bool) *ast.ParameterList { @@ -1110,10 +1124,14 @@ func (tx *DeclarationTransformer) omitPrivateMethodType(input *ast.Node) *ast.No nil, /*initializer*/ ) } - tx.preserveJsDoc(result, input) + tx.suppressJsDoc(result) return result } +func (tx *DeclarationTransformer) suppressJsDoc(node *ast.Node) { + tx.EmitContext().AddEmitFlags(node, printer.EFNoComments|printer.EFNoNestedComments) +} + func (tx *DeclarationTransformer) transformMethodSignatureDeclaration(input *ast.MethodSignatureDeclaration) *ast.Node { if tx.resolver.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0 { return tx.omitPrivateMethodType(input.AsNode()) @@ -1966,7 +1984,11 @@ func (tx *DeclarationTransformer) buildClassMembers(classNode *ast.Node, extraMe tx.ensureType(param, false), tx.ensureNoInitializer(param), ) - tx.preserveJsDoc(updated, param) + if tx.resolver.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(param), ast.ModifierFlagsPrivate) != 0 { + tx.suppressJsDoc(updated) + } else { + tx.preserveJsDoc(updated, param) + } parameterProperties = append(parameterProperties, updated) } else { // Pattern - this is currently an error, but we emit declarations for it somewhat correctly @@ -2166,7 +2188,9 @@ caseBlock: tx.ensureType(node, false), nil, ) - if ast.IsExpressionStatement(node.Parent) { + if modifierFlags&ast.ModifierFlagsPrivate != 0 { + tx.suppressJsDoc(prop) + } else if ast.IsExpressionStatement(node.Parent) { tx.preserveJsDoc(prop, node.Parent) } tx.thisPropertyAssignmentsCollected = append(tx.thisPropertyAssignmentsCollected, prop) diff --git a/tsc/testdata/baselines/reference/compiler/commentsClassMembers(target=es2015).js b/tsc/testdata/baselines/reference/compiler/commentsClassMembers(target=es2015).js index 2d27770cfe14d..02e749bf3970a 100644 --- a/tsc/testdata/baselines/reference/compiler/commentsClassMembers(target=es2015).js +++ b/tsc/testdata/baselines/reference/compiler/commentsClassMembers(target=es2015).js @@ -423,13 +423,9 @@ declare class c1 { get p3(): number; /** setter property*/ set p3(/** this is value*/ value: number); - /** pp1 is property of c1*/ private pp1; - /** sum with property*/ private pp2; - /** getter property*/ private get pp3(); - /** setter property*/ private set pp3(value); /** Constructor method*/ constructor(); @@ -473,13 +469,9 @@ declare class c1 { get b_p3(): number; /** setter property */ set b_p3(value: number); - /** pp1 is property of c1 */ private b_pp1; - /** sum with property */ private b_pp2; - /** getter property */ private get b_pp3(); - /** setter property */ private set b_pp3(value); /** s1 is static property of c1 */ static b_s1: number; diff --git a/tsc/testdata/baselines/reference/compiler/declFileAccessors(target=es2015).js b/tsc/testdata/baselines/reference/compiler/declFileAccessors(target=es2015).js index 2fb11763d7233..3b32af1e46e62 100644 --- a/tsc/testdata/baselines/reference/compiler/declFileAccessors(target=es2015).js +++ b/tsc/testdata/baselines/reference/compiler/declFileAccessors(target=es2015).js @@ -208,9 +208,7 @@ export declare class c1 { get p3(): number; /** setter property*/ set p3(/** this is value*/ value: number); - /** private getter property*/ private get pp3(); - /** private setter property*/ private set pp3(value); /** static getter property*/ static get s3(): number; @@ -232,9 +230,7 @@ declare class c2 { get p3(): number; /** setter property*/ set p3(/** this is value*/ value: number); - /** private getter property*/ private get pp3(); - /** private setter property*/ private set pp3(value); /** static getter property*/ static get s3(): number; diff --git a/tsc/testdata/baselines/reference/compiler/declFileMethods(target=es2015).js b/tsc/testdata/baselines/reference/compiler/declFileMethods(target=es2015).js index f524ed2b3c46b..2f79881994732 100644 --- a/tsc/testdata/baselines/reference/compiler/declFileMethods(target=es2015).js +++ b/tsc/testdata/baselines/reference/compiler/declFileMethods(target=es2015).js @@ -333,9 +333,7 @@ export declare class c1 { fooWithRestParameters(a: string, ...rests: string[]): string; fooWithOverloads(a: string): string; fooWithOverloads(a: number): number; - /** This comment should appear for privateFoo*/ private privateFoo; - /** This is comment for function signature*/ private privateFooWithParameters; private privateFooWithRestParameters; private privateFooWithOverloads; @@ -348,9 +346,7 @@ export declare class c1 { static staticFooWithRestParameters(a: string, ...rests: string[]): string; static staticFooWithOverloads(a: string): string; static staticFooWithOverloads(a: number): number; - /** This comment should appear for privateStaticFoo*/ private static privateStaticFoo; - /** This is comment for function signature*/ private static privateStaticFooWithParameters; private static privateStaticFooWithRestParameters; private static privateStaticFooWithOverloads; @@ -377,9 +373,7 @@ declare class c2 { fooWithRestParameters(a: string, ...rests: string[]): string; fooWithOverloads(a: string): string; fooWithOverloads(a: number): number; - /** This comment should appear for privateFoo*/ private privateFoo; - /** This is comment for function signature*/ private privateFooWithParameters; private privateFooWithRestParameters; private privateFooWithOverloads; @@ -392,9 +386,7 @@ declare class c2 { static staticFooWithRestParameters(a: string, ...rests: string[]): string; static staticFooWithOverloads(a: string): string; static staticFooWithOverloads(a: number): number; - /** This comment should appear for privateStaticFoo*/ private static privateStaticFoo; - /** This is comment for function signature*/ private static privateStaticFooWithParameters; private static privateStaticFooWithRestParameters; private static privateStaticFooWithOverloads; diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateAsyncMethod.js b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateAsyncMethod.js index 797cefbe4f9ea..9b46df671cded 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateAsyncMethod.js +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateAsyncMethod.js @@ -33,12 +33,6 @@ export class C { //// [a.d.ts] export declare class C { - /** - * Non Async function - */ private a; - /** - * Async function - */ private b; } diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMemberComments.js b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMemberComments.js new file mode 100644 index 0000000000000..03a627f87d5fc --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMemberComments.js @@ -0,0 +1,102 @@ +//// [tests/cases/compiler/declarationEmitPrivateMemberComments.ts] //// + +//// [a.ts] +export class A { + /** Public property. */ + a = 1; + + /** Private property. */ + private b = 1; + + /** Private method. */ + private c() {} + + /** Private getter. */ + private get d() { return 1; } + + /** Private setter. */ + private set d(value: number) {} + + /** ECMAScript private property. */ + #e = 1; + + constructor( + /** Private parameter property. */ + private f: number, + ) {} +} + +//// [b.js] +export class B { + /** Public property. */ + a = 1; + + /** @private */ + b = 1; + + /** @private */ + c() {} + + /** @private */ + get d() { return 1; } + + /** @private */ + set d(value) {} + + /** ECMAScript private property. */ + #e = 1; + + constructor() { + /** Public constructor property. */ + this.f = 1; + + /** + * Protected constructor property. + * @protected + */ + this.g = 1; + + /** + * Private constructor property. + * @private + */ + this.h = 1; + } +} + + + + +//// [a.d.ts] +export declare class A { + #private; + private f; + /** Public property. */ + a: number; + private b; + private c; + private get d(); + private set d(value); + constructor( + /** Private parameter property. */ + f: number); +} +//// [b.d.ts] +export declare class B { + #private; + /** Public constructor property. */ + f: number; + /** + * Protected constructor property. + * @protected + */ + protected g: number; + private h; + /** Public property. */ + a: number; + private b; + private c; + private get d(); + private set d(value); + constructor(); +} diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMemberComments.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMemberComments.symbols new file mode 100644 index 0000000000000..dd845de4eaf3e --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMemberComments.symbols @@ -0,0 +1,95 @@ +//// [tests/cases/compiler/declarationEmitPrivateMemberComments.ts] //// + +=== a.ts === +export class A { +>A : Symbol(A, Decl(a.ts, 0, 0)) + + /** Public property. */ + a = 1; +>a : Symbol(A.a, Decl(a.ts, 0, 16)) + + /** Private property. */ + private b = 1; +>b : Symbol(A.b, Decl(a.ts, 2, 10)) + + /** Private method. */ + private c() {} +>c : Symbol(A.c, Decl(a.ts, 5, 18)) + + /** Private getter. */ + private get d() { return 1; } +>d : Symbol(A.d, Decl(a.ts, 8, 18), Decl(a.ts, 11, 33)) + + /** Private setter. */ + private set d(value: number) {} +>d : Symbol(A.d, Decl(a.ts, 8, 18), Decl(a.ts, 11, 33)) +>value : Symbol(value, Decl(a.ts, 14, 18)) + + /** ECMAScript private property. */ + #e = 1; +>#e : Symbol(A.#e, Decl(a.ts, 14, 35)) + + constructor( + /** Private parameter property. */ + private f: number, +>f : Symbol(A.f, Decl(a.ts, 19, 16)) + + ) {} +} + +=== b.js === +export class B { +>B : Symbol(B, Decl(b.js, 0, 0)) + + /** Public property. */ + a = 1; +>a : Symbol(B.a, Decl(b.js, 0, 16)) + + /** @private */ + b = 1; +>b : Symbol(B.b, Decl(b.js, 2, 10)) + + /** @private */ + c() {} +>c : Symbol(B.c, Decl(b.js, 5, 10)) + + /** @private */ + get d() { return 1; } +>d : Symbol(B.d, Decl(b.js, 8, 10), Decl(b.js, 11, 25)) + + /** @private */ + set d(value) {} +>d : Symbol(B.d, Decl(b.js, 8, 10), Decl(b.js, 11, 25)) +>value : Symbol(value, Decl(b.js, 14, 10)) + + /** ECMAScript private property. */ + #e = 1; +>#e : Symbol(B.#e, Decl(b.js, 14, 19)) + + constructor() { + /** Public constructor property. */ + this.f = 1; +>this.f : Symbol(B.f, Decl(b.js, 19, 19)) +>this : Symbol(B, Decl(b.js, 0, 0)) +>f : Symbol(B.f, Decl(b.js, 19, 19)) + + /** + * Protected constructor property. + * @protected + */ + this.g = 1; +>this.g : Symbol(B.g, Decl(b.js, 21, 19)) +>this : Symbol(B, Decl(b.js, 0, 0)) +>g : Symbol(B.g, Decl(b.js, 21, 19)) + + /** + * Private constructor property. + * @private + */ + this.h = 1; +>this.h : Symbol(B.h, Decl(b.js, 27, 19)) +>this : Symbol(B, Decl(b.js, 0, 0)) +>h : Symbol(B.h, Decl(b.js, 27, 19)) + } +} + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMemberComments.types b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMemberComments.types new file mode 100644 index 0000000000000..e465d332bcc9b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMemberComments.types @@ -0,0 +1,109 @@ +//// [tests/cases/compiler/declarationEmitPrivateMemberComments.ts] //// + +=== a.ts === +export class A { +>A : A + + /** Public property. */ + a = 1; +>a : number +>1 : 1 + + /** Private property. */ + private b = 1; +>b : number +>1 : 1 + + /** Private method. */ + private c() {} +>c : () => void + + /** Private getter. */ + private get d() { return 1; } +>d : number +>1 : 1 + + /** Private setter. */ + private set d(value: number) {} +>d : number +>value : number + + /** ECMAScript private property. */ + #e = 1; +>#e : number +>1 : 1 + + constructor( + /** Private parameter property. */ + private f: number, +>f : number + + ) {} +} + +=== b.js === +export class B { +>B : B + + /** Public property. */ + a = 1; +>a : number +>1 : 1 + + /** @private */ + b = 1; +>b : number +>1 : 1 + + /** @private */ + c() {} +>c : () => void + + /** @private */ + get d() { return 1; } +>d : number +>1 : 1 + + /** @private */ + set d(value) {} +>d : number +>value : number + + /** ECMAScript private property. */ + #e = 1; +>#e : number +>1 : 1 + + constructor() { + /** Public constructor property. */ + this.f = 1; +>this.f = 1 : 1 +>this.f : any +>this : this +>f : any +>1 : 1 + + /** + * Protected constructor property. + * @protected + */ + this.g = 1; +>this.g = 1 : 1 +>this.g : any +>this : this +>g : any +>1 : 1 + + /** + * Private constructor property. + * @private + */ + this.h = 1; +>this.h = 1 : 1 +>this.h : any +>this : this +>h : any +>1 : 1 + } +} + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMethodSignatures.js b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMethodSignatures.js index 913e61c23a151..9457014c3c9e4 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMethodSignatures.js +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitPrivateMethodSignatures.js @@ -29,13 +29,11 @@ interface A { private a; } interface B { - /** Method documentation. */ private a; private b; private c; } type C = { - /** Method documentation. */ private a; private b; }; diff --git a/tsc/testdata/baselines/reference/compiler/jsDeclarationEmitPrivateStaticMethod.js b/tsc/testdata/baselines/reference/compiler/jsDeclarationEmitPrivateStaticMethod.js index b0c05be34face..12af002a86773 100644 --- a/tsc/testdata/baselines/reference/compiler/jsDeclarationEmitPrivateStaticMethod.js +++ b/tsc/testdata/baselines/reference/compiler/jsDeclarationEmitPrivateStaticMethod.js @@ -13,7 +13,6 @@ export class C { //// [a.d.ts] export declare class C { - /** @private */ private static foo; /** @protected */ protected static bar(): void; diff --git a/tsc/testdata/baselines/reference/compiler/jsDeclarationEmitThisPropertyJSDocModifiers.js b/tsc/testdata/baselines/reference/compiler/jsDeclarationEmitThisPropertyJSDocModifiers.js index 32cc7c070b62f..0c758cc5d4f3b 100644 --- a/tsc/testdata/baselines/reference/compiler/jsDeclarationEmitThisPropertyJSDocModifiers.js +++ b/tsc/testdata/baselines/reference/compiler/jsDeclarationEmitThisPropertyJSDocModifiers.js @@ -24,7 +24,6 @@ export class A { //// [a.d.ts] export declare class A { - /** @private @type {boolean} */ private privateProperty; /** @protected @type {string} */ protected protectedProperty: string; @@ -32,7 +31,6 @@ export declare class A { readonly readonlyProperty: number; /** @public @type {bigint} */ publicProperty: bigint; - /** @private @type {symbol} */ private static privateStaticProperty; constructor(); static initialize(): void; diff --git a/tsc/testdata/baselines/reference/conformance/jsdocAccessibilityTagsDeclarations.js b/tsc/testdata/baselines/reference/conformance/jsdocAccessibilityTagsDeclarations.js index 1ad91499186dc..de259928d1d74 100644 --- a/tsc/testdata/baselines/reference/conformance/jsdocAccessibilityTagsDeclarations.js +++ b/tsc/testdata/baselines/reference/conformance/jsdocAccessibilityTagsDeclarations.js @@ -93,15 +93,11 @@ declare class Protected { protected set p(value: any); } declare class Private { - /** @private */ private c; /** @private */ private constructor(); - /** @private */ private m; - /** @private */ private get p(); - /** @private */ private set p(value); } declare class C { diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitPrivateMemberComments.ts b/tsc/testdata/tests/cases/compiler/declarationEmitPrivateMemberComments.ts new file mode 100644 index 0000000000000..338e3b3208705 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitPrivateMemberComments.ts @@ -0,0 +1,68 @@ +// @allowJs: true +// @checkJs: true +// @declaration: true +// @emitDeclarationOnly: true + +// @filename: a.ts +export class A { + /** Public property. */ + a = 1; + + /** Private property. */ + private b = 1; + + /** Private method. */ + private c() {} + + /** Private getter. */ + private get d() { return 1; } + + /** Private setter. */ + private set d(value: number) {} + + /** ECMAScript private property. */ + #e = 1; + + constructor( + /** Private parameter property. */ + private f: number, + ) {} +} + +// @filename: b.js +export class B { + /** Public property. */ + a = 1; + + /** @private */ + b = 1; + + /** @private */ + c() {} + + /** @private */ + get d() { return 1; } + + /** @private */ + set d(value) {} + + /** ECMAScript private property. */ + #e = 1; + + constructor() { + /** Public constructor property. */ + this.f = 1; + + /** + * Protected constructor property. + * @protected + */ + this.g = 1; + + /** + * Private constructor property. + * @private + */ + this.h = 1; + } +}