Skip to content

Commit 45e76e2

Browse files
committed
Emit leading comments for '}' of the function block
Fixes #503
1 parent 92e3202 commit 45e76e2

18 files changed

+147
-39
lines changed

src/compiler/emitter.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ module ts {
330330
/** Emit Trailing comments of the node */
331331
var emitTrailingComments = compilerOptions.removeComments ? (node: Node) => { } : emitTrailingDeclarationComments;
332332

333+
var emitLeadingCommentsOfPosition = compilerOptions.removeComments ? (pos: number) => { } : emitLeadingCommentsOfLocalPosition;
334+
333335
var detachedCommentsInfo: { nodePos: number; detachedCommentEndPos: number }[];
334336
/** Emit detached comments of the node */
335337
var emitDetachedComments = compilerOptions.removeComments ? (node: TextRange) => { } : emitDetachedCommentsAtPosition;
@@ -1390,12 +1392,14 @@ module ts {
13901392
write(";");
13911393
emitTrailingComments(node.body);
13921394
}
1393-
decreaseIndent();
13941395
writeLine();
13951396
if (node.body.kind === SyntaxKind.FunctionBlock) {
1397+
emitLeadingCommentsOfPosition((<Block>node.body).statements.end);
1398+
decreaseIndent();
13961399
emitToken(SyntaxKind.CloseBraceToken, (<Block>node.body).statements.end);
13971400
}
13981401
else {
1402+
decreaseIndent();
13991403
emitStart(node.body);
14001404
write("}");
14011405
emitEnd(node.body);
@@ -2077,23 +2081,34 @@ module ts {
20772081
}
20782082
}
20792083

2084+
function hasDetachedComments(pos: number) {
2085+
return detachedCommentsInfo !== undefined && detachedCommentsInfo[detachedCommentsInfo.length - 1].nodePos === pos;
2086+
}
2087+
2088+
function getLeadingCommentsWithoutDetachedComments() {
2089+
// get the leading comments from detachedPos
2090+
var leadingComments = getLeadingComments(currentSourceFile.text, detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
2091+
if (detachedCommentsInfo.length - 1) {
2092+
detachedCommentsInfo.pop();
2093+
}
2094+
else {
2095+
detachedCommentsInfo = undefined;
2096+
}
2097+
2098+
return leadingComments;
2099+
}
2100+
20802101
function emitLeadingDeclarationComments(node: Node) {
20812102
// Emit the leading comments only if the parent's pos doesnt match because parent should take care of emitting these comments
20822103
if (node.parent.kind === SyntaxKind.SourceFile || node.pos !== node.parent.pos) {
20832104
var leadingComments: Comment[];
2084-
if (detachedCommentsInfo === undefined || detachedCommentsInfo[detachedCommentsInfo.length - 1].nodePos !== node.pos) {
2085-
// get the leading comments from the node
2086-
leadingComments = getLeadingCommentsOfNode(node, currentSourceFile);
2105+
if (hasDetachedComments(node.pos)) {
2106+
// get comments without detached comments
2107+
leadingComments = getLeadingCommentsWithoutDetachedComments();
20872108
}
20882109
else {
2089-
// get the leading comments from detachedPos
2090-
leadingComments = getLeadingComments(currentSourceFile.text, detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
2091-
if (detachedCommentsInfo.length - 1) {
2092-
detachedCommentsInfo.pop();
2093-
}
2094-
else {
2095-
detachedCommentsInfo = undefined;
2096-
}
2110+
// get the leading comments from the node
2111+
leadingComments = getLeadingCommentsOfNode(node, currentSourceFile);
20972112
}
20982113
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
20992114
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
@@ -2110,6 +2125,21 @@ module ts {
21102125
}
21112126
}
21122127

2128+
function emitLeadingCommentsOfLocalPosition(pos: number) {
2129+
var leadingComments: Comment[];
2130+
if (hasDetachedComments(pos)) {
2131+
// get comments without detached comments
2132+
leadingComments = getLeadingCommentsWithoutDetachedComments();
2133+
}
2134+
else {
2135+
// get the leading comments from the node
2136+
leadingComments = getLeadingComments(currentSourceFile.text, pos);
2137+
}
2138+
emitNewLineBeforeLeadingComments({ pos: pos, end: pos }, leadingComments, writer);
2139+
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
2140+
emitComments(leadingComments, /*trailingSeparator*/ true, writer, writeComment);
2141+
}
2142+
21132143
function emitDetachedCommentsAtPosition(node: TextRange) {
21142144
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
21152145
if (leadingComments) {

tests/baselines/reference/classMemberInitializerWithLamdaScoping.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ var Test1 = (function () {
5252
this.field1 = field1;
5353
this.messageHandler = function () {
5454
console.log(field1); // But this should be error as the field1 will resolve to var field1
55+
// but since this code would be generated inside constructor, in generated js
56+
// it would resolve to private field1 and thats not what user intended here.
5557
};
5658
}
5759
Test1.staticMessageHandler = function () {

tests/baselines/reference/classMemberInitializerWithLamdaScoping2.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var Test1 = (function () {
2525
this.field1 = field1;
2626
this.messageHandler = function () {
2727
console.log(field1); // But this should be error as the field1 will resolve to var field1
28+
// but since this code would be generated inside constructor, in generated js
29+
// it would resolve to private field1 and thats not what user intended here.
2830
};
2931
}
3032
return Test1;

tests/baselines/reference/classOrder1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var A = (function () {
1616
function A() {
1717
}
1818
A.prototype.foo = function () {
19+
/*WScript.Echo("Here!");*/
1920
};
2021
return A;
2122
})();

tests/baselines/reference/commentsFunction.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ lambdaFoo = (a, b) => a * b; // This is trailing comment
4141
/*leading comment*/(() => 0); //trailing comment
4242

4343
function blah4(/*1*/a: string/*2*/,/*3*/b: string/*4*/) {
44-
}
44+
}
45+
46+
function foo1() {
47+
48+
// should emit this
49+
}
50+
51+
function foo2() {
52+
/// This is some detached comment
53+
54+
// should emit this leading comment of } too
55+
}
56+
4557

4658
//// [commentsFunction.js]
4759
/** This comment should appear for foo*/
@@ -79,6 +91,13 @@ lambdaFoo = function (a, b) { return a * b; }; // This is trailing comment
7991
/*leading comment*/ (function () { return 0; }); //trailing comment
8092
function blah4(/*1*/ a /*2*/, /*3*/ b /*4*/) {
8193
}
94+
function foo1() {
95+
// should emit this
96+
}
97+
function foo2() {
98+
/// This is some detached comment
99+
// should emit this leading comment of } too
100+
}
82101

83102

84103
//// [commentsFunction.d.ts]
@@ -98,3 +117,5 @@ declare function blah(a: string): void;
98117
declare function blah2(a: string): void;
99118
declare function blah3(a: string): void;
100119
declare function blah4(a: string, b: string): void;
120+
declare function foo1(): void;
121+
declare function foo2(): void;

tests/baselines/reference/commentsFunction.types

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,18 @@ function blah4(/*1*/a: string/*2*/,/*3*/b: string/*4*/) {
110110
>a : string
111111
>b : string
112112
}
113+
114+
function foo1() {
115+
>foo1 : () => void
116+
117+
// should emit this
118+
}
119+
120+
function foo2() {
121+
>foo2 : () => void
122+
123+
/// This is some detached comment
124+
125+
// should emit this leading comment of } too
126+
}
127+

tests/baselines/reference/genericCallWithGenericSignatureArguments.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ function other2(x) {
6666
var r7 = foo(function (a) { return a; }, function (b) { return b; }); // T => T
6767
var r7b = foo(function (a) { return a; }, function (b) { return b; }); // {} => {}
6868
var r8 = r7(null);
69+
// BUG 835518
70+
//var r9 = r7(new Date());
6971
}
7072
function foo2(a, b) {
7173
var r;

tests/baselines/reference/genericCallWithObjectTypeArgsAndNumericIndexer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ function other3(arg) {
5454
var b;
5555
var r2 = foo(b);
5656
var d = r2[1];
57+
// BUG 821629
58+
//var u: U = r2[1]; // ok
5759
}

tests/baselines/reference/genericCallWithObjectTypeArgsAndStringIndexer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ function other3(arg) {
5555
var b;
5656
var r2 = foo(b);
5757
var d = r2['hm']; // ok
58+
// BUG 821629
59+
//var u: U = r2['hm']; // ok
5860
}

tests/baselines/reference/logicalOrOperatorWithTypeParameters.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function fn2(t, u, v) {
3737
var r4 = u || u;
3838
var r5 = u || v;
3939
var r6 = u || v;
40+
//var r7: T = u || v;
4041
}
4142
function fn3(t, u) {
4243
var r1 = t || u;

0 commit comments

Comments
 (0)