Skip to content

Commit 45e8ff8

Browse files
committed
Emit the new line before leading declaration comments in the declaration file if source code had it
1 parent bb638db commit 45e8ff8

File tree

7 files changed

+48
-24
lines changed

7 files changed

+48
-24
lines changed

src/compiler/emitter.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ module ts {
178178
});
179179
}
180180

181+
function emitNewLineBeforeLeadingComments(node: Node, leadingComments: Comment[], writer: EmitTextWriter) {
182+
// If the leading comments start on different line than the start of node, write new line
183+
if (leadingComments && leadingComments.length && node.pos !== leadingComments[0].pos &&
184+
currentSourceFile.getLineAndCharacterFromPosition(node.pos).line !== currentSourceFile.getLineAndCharacterFromPosition(leadingComments[0].pos).line) {
185+
writer.writeLine();
186+
}
187+
}
188+
181189
function writeCommentRange(comment: Comment, writer: EmitTextWriter) {
182190
writer.writeLiteral(currentSourceFile.text.substring(comment.pos, comment.end));
183191
}
@@ -1932,11 +1940,7 @@ module ts {
19321940

19331941
function emitLeadingDeclarationComments(node: Declaration) {
19341942
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
1935-
// If the leading comments start on different line than the start of node, write new line
1936-
if (leadingComments && leadingComments.length && node.pos !== leadingComments[0].pos &&
1937-
currentSourceFile.getLineAndCharacterFromPosition(node.pos).line !== currentSourceFile.getLineAndCharacterFromPosition(leadingComments[0].pos).line) {
1938-
writer.writeLine();
1939-
}
1943+
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
19401944
emitComments(leadingComments, writer, writeComment);
19411945
}
19421946

@@ -2063,6 +2067,7 @@ module ts {
20632067
function writeJsDocComments(declaration: Declaration) {
20642068
if (declaration) {
20652069
var jsDocComments = getJsDocComments(declaration, currentSourceFile);
2070+
emitNewLineBeforeLeadingComments(declaration, jsDocComments, writer);
20662071
emitComments(jsDocComments, writer, writeCommentRange);
20672072
}
20682073
}

tests/baselines/reference/commentsFunction.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ lambdaFoo = function (a, b) { return a * b; };
7575
/** This comment should appear for foo*/
7676
declare function foo(): void;
7777
/** This is comment for function signature*/
78-
declare function fooWithParameters(/** this is comment about a*/ a: string, /** this is comment for b*/
78+
declare function fooWithParameters(/** this is comment about a*/ a: string,
79+
/** this is comment for b*/
7980
b: number): void;
8081
/** fooFunc
8182
* comment

tests/baselines/reference/declFileCallSignatures.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export interface ICallSignature {
7676
}
7777
export interface ICallSignatureWithParameters {
7878
/** This is comment for function signature*/
79-
(/** this is comment about a*/ a: string, /** this is comment for b*/
79+
(/** this is comment about a*/ a: string,
80+
/** this is comment for b*/
8081
b: number): void;
8182
}
8283
export interface ICallSignatureWithRestParameters {
@@ -100,7 +101,8 @@ interface IGlobalCallSignature {
100101
}
101102
interface IGlobalCallSignatureWithParameters {
102103
/** This is comment for function signature*/
103-
(/** this is comment about a*/ a: string, /** this is comment for b*/
104+
(/** this is comment about a*/ a: string,
105+
/** this is comment for b*/
104106
b: number): void;
105107
}
106108
interface IGlobalCallSignatureWithRestParameters {

tests/baselines/reference/declFileConstructSignatures.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export interface IConstructSignature {
7676
}
7777
export interface IConstructSignatureWithParameters {
7878
/** This is comment for function signature*/
79-
new (/** this is comment about a*/ a: string, /** this is comment for b*/
79+
new (/** this is comment about a*/ a: string,
80+
/** this is comment for b*/
8081
b: number): any;
8182
}
8283
export interface IConstructSignatureWithRestParameters {
@@ -100,7 +101,8 @@ interface IGlobalConstructSignature {
100101
}
101102
interface IGlobalConstructSignatureWithParameters {
102103
/** This is comment for function signature*/
103-
new (/** this is comment about a*/ a: string, /** this is comment for b*/
104+
new (/** this is comment about a*/ a: string,
105+
/** this is comment for b*/
104106
b: number): any;
105107
}
106108
interface IGlobalConstructSignatureWithRestParameters {

tests/baselines/reference/declFileConstructors.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ export declare class SimpleConstructor {
226226
}
227227
export declare class ConstructorWithParameters {
228228
/** This is comment for function signature*/
229-
constructor(/** this is comment about a*/ a: string, /** this is comment for b*/
229+
constructor(/** this is comment about a*/ a: string,
230+
/** this is comment for b*/
230231
b: number);
231232
}
232233
export declare class ConstructorWithRestParamters {
@@ -259,7 +260,8 @@ declare class GlobalSimpleConstructor {
259260
}
260261
declare class GlobalConstructorWithParameters {
261262
/** This is comment for function signature*/
262-
constructor(/** this is comment about a*/ a: string, /** this is comment for b*/
263+
constructor(/** this is comment about a*/ a: string,
264+
/** this is comment for b*/
263265
b: number);
264266
}
265267
declare class GlobalConstructorWithRestParamters {

tests/baselines/reference/declFileFunctions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ function globalfooWithOverloads(a) {
128128
/** This comment should appear for foo*/
129129
export declare function foo(): void;
130130
/** This is comment for function signature*/
131-
export declare function fooWithParameters(/** this is comment about a*/ a: string, /** this is comment for b*/
131+
export declare function fooWithParameters(/** this is comment about a*/ a: string,
132+
/** this is comment for b*/
132133
b: number): void;
133134
export declare function fooWithRestParameters(a: string, ...rests: string[]): string;
134135
export declare function fooWithOverloads(a: string): string;
@@ -137,7 +138,8 @@ export declare function fooWithOverloads(a: number): number;
137138
/** This comment should appear for foo*/
138139
declare function globalfoo(): void;
139140
/** This is comment for function signature*/
140-
declare function globalfooWithParameters(/** this is comment about a*/ a: string, /** this is comment for b*/
141+
declare function globalfooWithParameters(/** this is comment about a*/ a: string,
142+
/** this is comment for b*/
141143
b: number): void;
142144
declare function globalfooWithRestParameters(a: string, ...rests: string[]): string;
143145
declare function globalfooWithOverloads(a: string): string;

tests/baselines/reference/declFileMethods.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,31 +362,35 @@ export declare class c1 {
362362
/** This comment should appear for foo*/
363363
foo(): void;
364364
/** This is comment for function signature*/
365-
fooWithParameters(/** this is comment about a*/ a: string, /** this is comment for b*/
365+
fooWithParameters(/** this is comment about a*/ a: string,
366+
/** this is comment for b*/
366367
b: number): void;
367368
fooWithRestParameters(a: string, ...rests: string[]): string;
368369
fooWithOverloads(a: string): string;
369370
fooWithOverloads(a: number): number;
370371
/** This comment should appear for privateFoo*/
371372
private privateFoo();
372373
/** This is comment for function signature*/
373-
private privateFooWithParameters(/** this is comment about a*/ a, /** this is comment for b*/
374+
private privateFooWithParameters(/** this is comment about a*/ a,
375+
/** this is comment for b*/
374376
b);
375377
private privateFooWithRestParameters(a, ...rests);
376378
private privateFooWithOverloads(a);
377379
private privateFooWithOverloads(a);
378380
/** This comment should appear for static foo*/
379381
static staticFoo(): void;
380382
/** This is comment for function signature*/
381-
static staticFooWithParameters(/** this is comment about a*/ a: string, /** this is comment for b*/
383+
static staticFooWithParameters(/** this is comment about a*/ a: string,
384+
/** this is comment for b*/
382385
b: number): void;
383386
static staticFooWithRestParameters(a: string, ...rests: string[]): string;
384387
static staticFooWithOverloads(a: string): string;
385388
static staticFooWithOverloads(a: number): number;
386389
/** This comment should appear for privateStaticFoo*/
387390
private static privateStaticFoo();
388391
/** This is comment for function signature*/
389-
private static privateStaticFooWithParameters(/** this is comment about a*/ a, /** this is comment for b*/
392+
private static privateStaticFooWithParameters(/** this is comment about a*/ a,
393+
/** this is comment for b*/
390394
b);
391395
private static privateStaticFooWithRestParameters(a, ...rests);
392396
private static privateStaticFooWithOverloads(a);
@@ -396,7 +400,8 @@ export interface I1 {
396400
/** This comment should appear for foo*/
397401
foo(): string;
398402
/** This is comment for function signature*/
399-
fooWithParameters(/** this is comment about a*/ a: string, /** this is comment for b*/
403+
fooWithParameters(/** this is comment about a*/ a: string,
404+
/** this is comment for b*/
400405
b: number): void;
401406
fooWithRestParameters(a: string, ...rests: string[]): string;
402407
fooWithOverloads(a: string): string;
@@ -407,31 +412,35 @@ declare class c2 {
407412
/** This comment should appear for foo*/
408413
foo(): void;
409414
/** This is comment for function signature*/
410-
fooWithParameters(/** this is comment about a*/ a: string, /** this is comment for b*/
415+
fooWithParameters(/** this is comment about a*/ a: string,
416+
/** this is comment for b*/
411417
b: number): void;
412418
fooWithRestParameters(a: string, ...rests: string[]): string;
413419
fooWithOverloads(a: string): string;
414420
fooWithOverloads(a: number): number;
415421
/** This comment should appear for privateFoo*/
416422
private privateFoo();
417423
/** This is comment for function signature*/
418-
private privateFooWithParameters(/** this is comment about a*/ a, /** this is comment for b*/
424+
private privateFooWithParameters(/** this is comment about a*/ a,
425+
/** this is comment for b*/
419426
b);
420427
private privateFooWithRestParameters(a, ...rests);
421428
private privateFooWithOverloads(a);
422429
private privateFooWithOverloads(a);
423430
/** This comment should appear for static foo*/
424431
static staticFoo(): void;
425432
/** This is comment for function signature*/
426-
static staticFooWithParameters(/** this is comment about a*/ a: string, /** this is comment for b*/
433+
static staticFooWithParameters(/** this is comment about a*/ a: string,
434+
/** this is comment for b*/
427435
b: number): void;
428436
static staticFooWithRestParameters(a: string, ...rests: string[]): string;
429437
static staticFooWithOverloads(a: string): string;
430438
static staticFooWithOverloads(a: number): number;
431439
/** This comment should appear for privateStaticFoo*/
432440
private static privateStaticFoo();
433441
/** This is comment for function signature*/
434-
private static privateStaticFooWithParameters(/** this is comment about a*/ a, /** this is comment for b*/
442+
private static privateStaticFooWithParameters(/** this is comment about a*/ a,
443+
/** this is comment for b*/
435444
b);
436445
private static privateStaticFooWithRestParameters(a, ...rests);
437446
private static privateStaticFooWithOverloads(a);
@@ -441,7 +450,8 @@ interface I2 {
441450
/** This comment should appear for foo*/
442451
foo(): string;
443452
/** This is comment for function signature*/
444-
fooWithParameters(/** this is comment about a*/ a: string, /** this is comment for b*/
453+
fooWithParameters(/** this is comment about a*/ a: string,
454+
/** this is comment for b*/
445455
b: number): void;
446456
fooWithRestParameters(a: string, ...rests: string[]): string;
447457
fooWithOverloads(a: string): string;

0 commit comments

Comments
 (0)