Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions tsc/internal/transformers/declarations/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,47 +992,61 @@ 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(),
postfixToken,
tx.ensureType(input.AsNode(), false),
tx.ensureNoInitializer(input.AsNode()),
)
if tx.host.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0 {
tx.suppressJsDoc(result)
Comment thread
a-tarasyuk marked this conversation as resolved.
}
return result
}

func (tx *DeclarationTransformer) transformSetAccessorDeclaration(input *ast.SetAccessorDeclaration) *ast.Node {
if ast.IsPrivateIdentifier(input.Name()) {
return nil
}

return tx.Factory().UpdateSetAccessorDeclaration(
isPrivate := tx.host.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.host.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.host.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.host.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 {
Expand Down Expand Up @@ -1105,10 +1119,14 @@ func (tx *DeclarationTransformer) omitPrivateMethodType(input *ast.Node) *ast.No
nil,
nil,
)
tx.preserveJsDoc(result, input)
tx.suppressJsDoc(result)
return result
}

func (tx *DeclarationTransformer) suppressJsDoc(node *ast.Node) {
tx.EmitContext().AddEmitFlags(node, printer.EFNoComments|printer.EFNoNestedComments)
}
Comment thread
a-tarasyuk marked this conversation as resolved.

func (tx *DeclarationTransformer) transformMethodSignatureDeclaration(input *ast.MethodSignatureDeclaration) *ast.Node {
if tx.host.GetEffectiveDeclarationFlags(tx.EmitContext().ParseNode(input.AsNode()), ast.ModifierFlagsPrivate) != 0 {
return tx.omitPrivateMethodType(input.AsNode())
Expand Down Expand Up @@ -1940,7 +1958,11 @@ func (tx *DeclarationTransformer) buildClassMembers(classNode *ast.Node, extraMe
tx.ensureType(param, false),
tx.ensureNoInitializer(param),
)
tx.preserveJsDoc(updated, param)
if tx.host.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ export class C {

//// [a.d.ts]
export declare class C {
/**
* Non Async function
*/
private a;
/**
* Async function
*/
private b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//// [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;
}




//// [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 property. */
a: number;
private b;
private c;
private get d();
private set d(value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//// [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))
}

Loading