Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always emit accessors in .d.ts files #33470

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 17 additions & 58 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ namespace ts {
let emittedImports: readonly AnyImportSyntax[] | undefined; // must be declared in container so it can be `undefined` while transformer's first pass
const resolver = context.getEmitResolver();
const options = context.getCompilerOptions();
const newLine = getNewLineCharacter(options);
const { noResolve, stripInternal } = options;
return transformRoot;

Expand Down Expand Up @@ -861,35 +860,25 @@ namespace ts {
return cleanup(sig);
}
case SyntaxKind.GetAccessor: {
// For now, only emit class accessors as accessors if they were already declared in an ambient context.
if (input.flags & NodeFlags.Ambient) {
const isPrivate = hasModifier(input, ModifierFlags.Private);
const accessorType = getTypeAnnotationFromAllAccessorDeclarations(input, resolver.getAllAccessorDeclarations(input));
return cleanup(updateGetAccessor(
input,
/*decorators*/ undefined,
ensureModifiers(input),
input.name,
updateAccessorParamsList(input, isPrivate),
!isPrivate ? ensureType(input, accessorType) : undefined,
/*body*/ undefined));
}
const newNode = ensureAccessor(input);
return cleanup(newNode);
const isPrivate = hasModifier(input, ModifierFlags.Private);
const accessorType = getTypeAnnotationFromAllAccessorDeclarations(input, resolver.getAllAccessorDeclarations(input));
return cleanup(updateGetAccessor(
input,
/*decorators*/ undefined,
ensureModifiers(input),
input.name,
updateAccessorParamsList(input, isPrivate),
!isPrivate ? ensureType(input, accessorType) : undefined,
/*body*/ undefined));
}
case SyntaxKind.SetAccessor: {
// For now, only emit class accessors as accessors if they were already declared in an ambient context.
if (input.flags & NodeFlags.Ambient) {
return cleanup(updateSetAccessor(
input,
/*decorators*/ undefined,
ensureModifiers(input),
input.name,
updateAccessorParamsList(input, hasModifier(input, ModifierFlags.Private)),
/*body*/ undefined));
}
const newNode = ensureAccessor(input);
return cleanup(newNode);
return cleanup(updateSetAccessor(
input,
/*decorators*/ undefined,
ensureModifiers(input),
input.name,
updateAccessorParamsList(input, hasModifier(input, ModifierFlags.Private)),
/*body*/ undefined));
}
case SyntaxKind.PropertyDeclaration:
return cleanup(updateProperty(
Expand Down Expand Up @@ -1462,36 +1451,6 @@ namespace ts {
return accessorType;
}

function ensureAccessor(node: AccessorDeclaration): PropertyDeclaration | undefined {
const accessors = resolver.getAllAccessorDeclarations(node);
if (node.kind !== accessors.firstAccessor.kind) {
return;
}
const accessorType = getTypeAnnotationFromAllAccessorDeclarations(node, accessors);
const prop = createProperty(/*decorators*/ undefined, maskModifiers(node, /*mask*/ undefined, (!accessors.setAccessor) ? ModifierFlags.Readonly : ModifierFlags.None), node.name, node.questionToken, ensureType(node, accessorType), /*initializer*/ undefined);
const leadingsSyntheticCommentRanges = accessors.secondAccessor && getLeadingCommentRangesOfNode(accessors.secondAccessor, currentSourceFile);
if (leadingsSyntheticCommentRanges) {
for (const range of leadingsSyntheticCommentRanges) {
if (range.kind === SyntaxKind.MultiLineCommentTrivia) {
let text = currentSourceFile.text.slice(range.pos + 2, range.end - 2);
const lines = text.split(/\r\n?|\n/g);
if (lines.length > 1) {
const lastLines = lines.slice(1);
const indentation = guessIndentation(lastLines);
text = [lines[0], ...map(lastLines, l => l.slice(indentation))].join(newLine);
}
addSyntheticLeadingComment(
prop,
range.kind,
text,
range.hasTrailingNewLine
);
}
}
}
return prop;
}

function transformHeritageClauses(nodes: NodeArray<HeritageClause> | undefined) {
return createNodeArray(filter(map(nodes, clause => updateHeritageClause(clause, visitNodes(createNodeArray(filter(clause.types, t => {
return isEntityNameExpression(t.expression) || (clause.token === SyntaxKind.ExtendsKeyword && t.expression.kind === SyntaxKind.NullKeyword);
Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/classdecl.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,14 @@ declare class a {
constructor(s: string);
pgF(): void;
pv: any;
d: number;
static readonly p2: {
get d(): number;
set d(a: number);
static get p2(): {
x: number;
y: number;
};
private static d2;
private static readonly p3;
private static get p3();
private pv3;
private foo;
}
Expand Down
44 changes: 28 additions & 16 deletions tests/baselines/reference/commentsClassMembers.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,65 +486,77 @@ declare class c1 {
/** sum with property*/
p2(/** number to add*/ b: number): number;
/** getter property*/
get p3(): number;
/** setter property*/
p3: number;
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 pp3;
private set pp3(value);
/** Constructor method*/
constructor();
/** s1 is static property of c1*/
static s1: number;
/** static sum with property*/
static s2(/** number to add*/ b: number): number;
/** static getter property*/
static get s3(): number;
/** setter property*/
static s3: number;
static set s3(/** this is value*/ value: number);
nc_p1: number;
nc_p2(b: number): number;
nc_p3: number;
get nc_p3(): number;
set nc_p3(value: number);
private nc_pp1;
private nc_pp2;
private nc_pp3;
private get nc_pp3();
private set nc_pp3(value);
static nc_s1: number;
static nc_s2(b: number): number;
static nc_s3: number;
static get nc_s3(): number;
static set nc_s3(value: number);
a_p1: number;
a_p2(b: number): number;
a_p3: number;
get a_p3(): number;
set a_p3(value: number);
private a_pp1;
private a_pp2;
private a_pp3;
private get a_pp3();
private set a_pp3(value);
static a_s1: number;
static a_s2(b: number): number;
static a_s3: number;
static get a_s3(): number;
static set a_s3(value: number);
/** p1 is property of c1 */
b_p1: number;
/** sum with property */
b_p2(b: number): number;
/** getter property */
get b_p3(): number;
/** setter property */
b_p3: number;
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 b_pp3;
private set b_pp3(value);
/** s1 is static property of c1 */
static b_s1: number;
/** static sum with property */
static b_s2(b: number): number;
/** static getter property
*/
static get b_s3(): number;
/** setter property
*/
static b_s3: number;
static set b_s3(value: number);
}
declare var i1: c1;
declare var i1_p: number;
Expand All @@ -567,11 +579,11 @@ declare var i1_c: typeof c1;
declare class cProperties {
private val;
/** getter only property*/
readonly p1: number;
readonly nc_p1: number;
get p1(): number;
get nc_p1(): number;
/**setter only property*/
p2: number;
nc_p2: number;
set p2(value: number);
set nc_p2(value: number);
x: number;
private y;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/commentsInheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,19 @@ declare class c2 {
/** c2 c2_f1*/
c2_f1(): void;
/** c2 c2_prop*/
readonly c2_prop: number;
get c2_prop(): number;
c2_nc_p1: number;
c2_nc_f1(): void;
readonly c2_nc_prop: number;
get c2_nc_prop(): number;
/** c2 p1*/
p1: number;
/** c2 f1*/
f1(): void;
/** c2 prop*/
readonly prop: number;
get prop(): number;
nc_p1: number;
nc_f1(): void;
readonly nc_prop: number;
get nc_prop(): number;
/** c2 constructor*/
constructor(a: number);
}
Expand All @@ -346,10 +346,10 @@ declare class c3 extends c2 {
/** c3 f1*/
f1(): void;
/** c3 prop*/
readonly prop: number;
get prop(): number;
nc_p1: number;
nc_f1(): void;
readonly nc_prop: number;
get nc_prop(): number;
}
declare var c2_i: c2;
declare var c3_i: c3;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/commentsdoNotEmitComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ declare class c {
constructor();
b: number;
myFoo(): number;
prop1: number;
get prop1(): number;
set prop1(val: number);
foo1(a: number): string;
foo1(b: string): string;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/commentsemitComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ declare class c {
/** function comment */
myFoo(): number;
/** getter comment*/
get prop1(): number;
/** setter comment*/
prop1: number;
set prop1(val: number);
/** overload signature1*/
foo1(a: number): string;
/** Overload signature 2*/
Expand Down
44 changes: 28 additions & 16 deletions tests/baselines/reference/declFileAccessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,35 +273,47 @@ var c2 = /** @class */ (function () {
/** This is comment for c1*/
export declare class c1 {
/** getter property*/
get p3(): number;
/** setter property*/
p3: number;
set p3(/** this is value*/ value: number);
/** private getter property*/
private get pp3();
/** private setter property*/
private pp3;
private set pp3(value);
/** static getter property*/
static get s3(): number;
/** setter property*/
static s3: number;
nc_p3: number;
private nc_pp3;
static nc_s3: string;
readonly onlyGetter: number;
onlySetter: number;
static set s3(/** this is value*/ value: number);
get nc_p3(): number;
set nc_p3(value: number);
private get nc_pp3();
private set nc_pp3(value);
static get nc_s3(): string;
static set nc_s3(value: string);
get onlyGetter(): number;
set onlySetter(value: number);
}
//// [declFileAccessors_1.d.ts]
/** This is comment for c2 - the global class*/
declare class c2 {
/** getter property*/
get p3(): number;
/** setter property*/
p3: number;
set p3(/** this is value*/ value: number);
/** private getter property*/
private get pp3();
/** private setter property*/
private pp3;
private set pp3(value);
/** static getter property*/
static get s3(): number;
/** setter property*/
static s3: number;
nc_p3: number;
private nc_pp3;
static nc_s3: string;
readonly onlyGetter: number;
onlySetter: number;
static set s3(/** this is value*/ value: number);
get nc_p3(): number;
set nc_p3(value: number);
private get nc_pp3();
private set nc_pp3(value);
static get nc_s3(): string;
static set nc_s3(value: string);
get onlyGetter(): number;
set onlySetter(value: number);
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/declFilePrivateStatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ declare class C {
static y: number;
private static a;
static b(): void;
private static readonly c;
static readonly d: number;
private static e;
static f: any;
private static get c();
static get d(): number;
private static set e(value);
static set f(v: any);
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,21 +271,27 @@ declare module m {
}
}
export class c {
readonly foo1: private1;
readonly foo2: private1;
foo3: private1;
foo4: private1;
foo5: private1;
readonly foo11: public1;
readonly foo12: public1;
foo13: public1;
foo14: public1;
foo15: public1;
readonly foo111: m2.public2;
readonly foo112: m2.public2;
foo113: m2.public2;
foo114: m2.public2;
foo115: m2.public2;
get foo1(): private1;
get foo2(): private1;
set foo3(param: private1);
get foo4(): private1;
set foo4(param: private1);
get foo5(): private1;
set foo5(param: private1);
get foo11(): public1;
get foo12(): public1;
set foo13(param: public1);
get foo14(): public1;
set foo14(param: public1);
get foo15(): public1;
set foo15(param: public1);
get foo111(): m2.public2;
get foo112(): m2.public2;
set foo113(param: m2.public2);
get foo114(): m2.public2;
set foo114(param: m2.public2);
get foo115(): m2.public2;
set foo115(param: m2.public2);
}
export {};
}
Loading