Skip to content

Commit f7b2062

Browse files
authored
Merge pull request #13916 from Microsoft/master-fix13602
[Master] Fix 13602 preserve comment following element in node list
2 parents b5c59c6 + 8da2bd9 commit f7b2062

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+496
-20
lines changed

src/compiler/comments.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace ts {
99
emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void;
1010
emitBodyWithDetachedComments(node: Node, detachedRange: TextRange, emitCallback: (node: Node) => void): void;
1111
emitTrailingCommentsOfPosition(pos: number): void;
12+
emitLeadingCommentsOfPosition(pos: number): void;
1213
}
1314

1415
export function createCommentWriter(printerOptions: PrinterOptions, emitPos: ((pos: number) => void) | undefined): CommentWriter {
@@ -32,6 +33,7 @@ namespace ts {
3233
emitNodeWithComments,
3334
emitBodyWithDetachedComments,
3435
emitTrailingCommentsOfPosition,
36+
emitLeadingCommentsOfPosition,
3537
};
3638

3739
function emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) {
@@ -210,6 +212,14 @@ namespace ts {
210212
}
211213
}
212214

215+
function emitLeadingCommentsOfPosition(pos: number) {
216+
if (disabled || pos === -1) {
217+
return;
218+
}
219+
220+
emitLeadingComments(pos, /*isEmittedNode*/ true);
221+
}
222+
213223
function emitTrailingComments(pos: number) {
214224
forEachTrailingCommentToEmit(pos, emitTrailingComment);
215225
}

src/compiler/emitter.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="checker.ts" />
1+
/// <reference path="checker.ts" />
22
/// <reference path="transformer.ts" />
33
/// <reference path="declarationEmitter.ts" />
44
/// <reference path="sourcemap.ts" />
@@ -211,6 +211,7 @@ namespace ts {
211211
emitNodeWithComments,
212212
emitBodyWithDetachedComments,
213213
emitTrailingCommentsOfPosition,
214+
emitLeadingCommentsOfPosition,
214215
} = comments;
215216

216217
let currentSourceFile: SourceFile;
@@ -1346,6 +1347,10 @@ namespace ts {
13461347
else {
13471348
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
13481349
emitBlockStatements(node);
1350+
// We have to call emitLeadingComments explicitly here because otherwise leading comments of the close brace token will not be emitted
1351+
increaseIndent();
1352+
emitLeadingCommentsOfPosition(node.statements.end);
1353+
decreaseIndent();
13491354
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
13501355
}
13511356
}
@@ -2228,6 +2233,15 @@ namespace ts {
22282233

22292234
// Write the delimiter if this is not the first node.
22302235
if (previousSibling) {
2236+
// i.e
2237+
// function commentedParameters(
2238+
// /* Parameter a */
2239+
// a
2240+
// /* End of parameter a */ -> this comment isn't considered to be trailing comment of parameter "a" due to newline
2241+
// ,
2242+
if (delimiter && previousSibling.end !== parentNode.end) {
2243+
emitLeadingCommentsOfPosition(previousSibling.end);
2244+
}
22312245
write(delimiter);
22322246

22332247
// Write either a line terminator or whitespace to separate the elements.
@@ -2274,6 +2288,17 @@ namespace ts {
22742288
write(",");
22752289
}
22762290

2291+
2292+
// Emit any trailing comment of the last element in the list
2293+
// i.e
2294+
// var array = [...
2295+
// 2
2296+
// /* end of element 2 */
2297+
// ];
2298+
if (previousSibling && delimiter && previousSibling.end !== parentNode.end) {
2299+
emitLeadingCommentsOfPosition(previousSibling.end);
2300+
}
2301+
22772302
// Decrease the indent, if requested.
22782303
if (format & ListFormat.Indented) {
22792304
decreaseIndent();

tests/baselines/reference/amdImportAsPrimaryExpression.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ define(["require", "exports"], function (require, exports) {
2626
define(["require", "exports", "./foo_0"], function (require, exports, foo) {
2727
"use strict";
2828
if (foo.E1.A === 0) {
29+
// Should cause runtime import - interesting optimization possibility, as gets inlined to 0.
2930
}
3031
});

tests/baselines/reference/argsInScope.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var C = (function () {
1717
}
1818
C.prototype.P = function (ii, j, k) {
1919
for (var i = 0; i < arguments.length; i++) {
20+
// WScript.Echo("param: " + arguments[i]);
2021
}
2122
};
2223
return C;

tests/baselines/reference/arrowFunctionErrorSpan.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ f(// comment 1
8383
// comment 2
8484
function () {
8585
// comment 4
86-
});
86+
}
87+
// comment 5
88+
);
8789
// body is not a block
8890
f(function (_) { return 1 +
8991
2; });
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [commentLeadingCloseBrace.ts]
2+
declare function commentedParameters(...args): any;
3+
4+
function ifelse() {
5+
if (commentedParameters(1, 2)) {
6+
/*comment1*/
7+
commentedParameters(3, 4);
8+
/*comment2*/
9+
} else {
10+
commentedParameters(5, 6);
11+
}
12+
}
13+
14+
//// [commentLeadingCloseBrace.js]
15+
function ifelse() {
16+
if (commentedParameters(1, 2)) {
17+
/*comment1*/
18+
commentedParameters(3, 4);
19+
/*comment2*/
20+
}
21+
else {
22+
commentedParameters(5, 6);
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/commentLeadingCloseBrace.ts ===
2+
declare function commentedParameters(...args): any;
3+
>commentedParameters : Symbol(commentedParameters, Decl(commentLeadingCloseBrace.ts, 0, 0))
4+
>args : Symbol(args, Decl(commentLeadingCloseBrace.ts, 0, 37))
5+
6+
function ifelse() {
7+
>ifelse : Symbol(ifelse, Decl(commentLeadingCloseBrace.ts, 0, 51))
8+
9+
if (commentedParameters(1, 2)) {
10+
>commentedParameters : Symbol(commentedParameters, Decl(commentLeadingCloseBrace.ts, 0, 0))
11+
12+
/*comment1*/
13+
commentedParameters(3, 4);
14+
>commentedParameters : Symbol(commentedParameters, Decl(commentLeadingCloseBrace.ts, 0, 0))
15+
16+
/*comment2*/
17+
} else {
18+
commentedParameters(5, 6);
19+
>commentedParameters : Symbol(commentedParameters, Decl(commentLeadingCloseBrace.ts, 0, 0))
20+
}
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/commentLeadingCloseBrace.ts ===
2+
declare function commentedParameters(...args): any;
3+
>commentedParameters : (...args: any[]) => any
4+
>args : any[]
5+
6+
function ifelse() {
7+
>ifelse : () => void
8+
9+
if (commentedParameters(1, 2)) {
10+
>commentedParameters(1, 2) : any
11+
>commentedParameters : (...args: any[]) => any
12+
>1 : 1
13+
>2 : 2
14+
15+
/*comment1*/
16+
commentedParameters(3, 4);
17+
>commentedParameters(3, 4) : any
18+
>commentedParameters : (...args: any[]) => any
19+
>3 : 3
20+
>4 : 4
21+
22+
/*comment2*/
23+
} else {
24+
commentedParameters(5, 6);
25+
>commentedParameters(5, 6) : any
26+
>commentedParameters : (...args: any[]) => any
27+
>5 : 5
28+
>6 : 6
29+
}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [commentOnArrayElement1.ts]
2+
var array = [
3+
/* element 1*/
4+
1
5+
/* end of element 1 */,
6+
2
7+
/* end of element 2 */
8+
];
9+
10+
//// [commentOnArrayElement1.js]
11+
var array = [
12+
/* element 1*/
13+
1
14+
/* end of element 1 */ ,
15+
2
16+
/* end of element 2 */
17+
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/commentOnArrayElement1.ts ===
2+
var array = [
3+
>array : Symbol(array, Decl(commentOnArrayElement1.ts, 0, 3))
4+
5+
/* element 1*/
6+
1
7+
/* end of element 1 */,
8+
2
9+
/* end of element 2 */
10+
];

0 commit comments

Comments
 (0)