Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e6cd3e1

Browse filesBrowse files
committed
Merge pull request microsoft#509 from Microsoft/emitLeadingCommentsForCurly
Emit leading comments for '}' of function/constructor block
2 parents eb41b21 + 53d79a2 commit e6cd3e1
Copy full SHA for e6cd3e1

26 files changed

+194-40Lines changed: 194 additions & 40 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/emitter.ts‎

Copy file name to clipboardExpand all lines: src/compiler/emitter.ts
+46-13Lines changed: 46 additions & 13 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);
@@ -1648,8 +1652,11 @@ module ts {
16481652
if (superCall) statements = statements.slice(1);
16491653
emitLines(statements);
16501654
}
1651-
decreaseIndent();
16521655
writeLine();
1656+
if (ctor) {
1657+
emitLeadingCommentsOfPosition((<Block>ctor.body).statements.end);
1658+
}
1659+
decreaseIndent();
16531660
emitToken(SyntaxKind.CloseBraceToken, ctor ? (<Block>ctor.body).statements.end : node.members.end);
16541661
scopeEmitEnd();
16551662
emitEnd(<Node>ctor || node);
@@ -2077,23 +2084,34 @@ module ts {
20772084
}
20782085
}
20792086

2087+
function hasDetachedComments(pos: number) {
2088+
return detachedCommentsInfo !== undefined && detachedCommentsInfo[detachedCommentsInfo.length - 1].nodePos === pos;
2089+
}
2090+
2091+
function getLeadingCommentsWithoutDetachedComments() {
2092+
// get the leading comments from detachedPos
2093+
var leadingComments = getLeadingComments(currentSourceFile.text, detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
2094+
if (detachedCommentsInfo.length - 1) {
2095+
detachedCommentsInfo.pop();
2096+
}
2097+
else {
2098+
detachedCommentsInfo = undefined;
2099+
}
2100+
2101+
return leadingComments;
2102+
}
2103+
20802104
function emitLeadingDeclarationComments(node: Node) {
20812105
// Emit the leading comments only if the parent's pos doesnt match because parent should take care of emitting these comments
20822106
if (node.parent.kind === SyntaxKind.SourceFile || node.pos !== node.parent.pos) {
20832107
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);
2108+
if (hasDetachedComments(node.pos)) {
2109+
// get comments without detached comments
2110+
leadingComments = getLeadingCommentsWithoutDetachedComments();
20872111
}
20882112
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-
}
2113+
// get the leading comments from the node
2114+
leadingComments = getLeadingCommentsOfNode(node, currentSourceFile);
20972115
}
20982116
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
20992117
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
@@ -2110,6 +2128,21 @@ module ts {
21102128
}
21112129
}
21122130

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

‎tests/baselines/reference/callOverloads1.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/callOverloads1.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Foo();
2020
//// [callOverloads1.js]
2121
var Foo = (function () {
2222
function Foo(x) {
23+
// WScript.Echo("Constructor function has executed");
2324
}
2425
Foo.prototype.bar1 = function () {
2526
};
Collapse file

‎tests/baselines/reference/callOverloads2.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/callOverloads2.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Foo();
2828
//// [callOverloads2.js]
2929
var Foo = (function () {
3030
function Foo(x) {
31+
// WScript.Echo("Constructor function has executed");
3132
}
3233
Foo.prototype.bar1 = function () {
3334
};
Collapse file

‎tests/baselines/reference/callOverloads3.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/callOverloads3.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Foo("s");
2121
//// [callOverloads3.js]
2222
var Foo = (function () {
2323
function Foo(x) {
24+
// WScript.Echo("Constructor function has executed");
2425
}
2526
Foo.prototype.bar1 = function () {
2627
};
Collapse file

‎tests/baselines/reference/callOverloads4.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/callOverloads4.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Foo("s");
2121
//// [callOverloads4.js]
2222
var Foo = (function () {
2323
function Foo(x) {
24+
// WScript.Echo("Constructor function has executed");
2425
}
2526
Foo.prototype.bar1 = function () {
2627
};
Collapse file

‎tests/baselines/reference/callOverloads5.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/callOverloads5.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Foo("s");
2222
//// [callOverloads5.js]
2323
var Foo = (function () {
2424
function Foo(x) {
25+
// WScript.Echo("Constructor function has executed");
2526
}
2627
Foo.prototype.bar1 = function (a) {
2728
};
Collapse file

‎tests/baselines/reference/classMemberInitializerWithLamdaScoping.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/classMemberInitializerWithLamdaScoping.js
+2Lines 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 () {
Collapse file

‎tests/baselines/reference/classMemberInitializerWithLamdaScoping2.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/classMemberInitializerWithLamdaScoping2.js
+2Lines 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;
Collapse file

‎tests/baselines/reference/classOrder1.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/classOrder1.js
+1Lines 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
})();
Collapse file

‎tests/baselines/reference/commentsClass.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/commentsClass.js
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ class c8 {
6363
}
6464
var i8 = new c8();
6565
var i8_c = c8;
66+
67+
class c9 {
68+
constructor() {
69+
/// This is some detached comment
70+
71+
// should emit this leading comment of } too
72+
}
73+
}
6674

6775

6876
//// [commentsClass.js]
@@ -123,11 +131,20 @@ var c8 = (function () {
123131
/** constructor comment
124132
*/
125133
function c8() {
134+
/** constructor comment2
135+
*/
126136
}
127137
return c8;
128138
})();
129139
var i8 = new c8();
130140
var i8_c = c8;
141+
var c9 = (function () {
142+
function c9() {
143+
/// This is some detached comment
144+
// should emit this leading comment of } too
145+
}
146+
return c9;
147+
})();
131148

132149

133150
//// [commentsClass.d.ts]
@@ -178,3 +195,6 @@ declare class c8 {
178195
}
179196
declare var i8: c8;
180197
declare var i8_c: typeof c8;
198+
declare class c9 {
199+
constructor();
200+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.