Skip to content

Commit 1b5023b

Browse files
committed
Emit leading/trailing comments for return statement
Note the detachedComments and copyright headers comment emitting is not part of this change
1 parent 6ab3adf commit 1b5023b

35 files changed

+103
-70
lines changed

src/compiler/emitter.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ module ts {
212212
var writeEmittedFiles = writeJavaScriptFile;
213213

214214
/** Emit leading comments of the declaration */
215-
var emitLeadingComments = compilerOptions.removeComments ? (d: Declaration) => { } : emitLeadingDeclarationComments;
215+
var emitLeadingComments = compilerOptions.removeComments ? (d: Node) => { } : emitLeadingDeclarationComments;
216216

217217
/** Emit Trailing comments of the declaration */
218-
var emitTrailingComments = compilerOptions.removeComments ? (d: Declaration) => { } : emitTrailingDeclarationComments;
218+
var emitTrailingComments = compilerOptions.removeComments ? (d: Node) => { } : emitTrailingDeclarationComments;
219219

220220
var writeComment = writeCommentRange;
221221

@@ -1009,9 +1009,11 @@ module ts {
10091009
}
10101010

10111011
function emitReturnStatement(node: ReturnStatement) {
1012+
emitLeadingComments(node);
10121013
emitToken(SyntaxKind.ReturnKeyword, node.pos);
10131014
emitOptional(" ", node.expression);
10141015
write(";");
1016+
emitTrailingComments(node);
10151017
}
10161018

10171019
function emitWithStatement(node: WhileStatement) {
@@ -1947,14 +1949,14 @@ module ts {
19471949
}
19481950
}
19491951

1950-
function emitLeadingDeclarationComments(node: Declaration) {
1952+
function emitLeadingDeclarationComments(node: Node) {
19511953
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
19521954
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
19531955
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
19541956
emitComments(leadingComments, /*trailingSeparator*/ true, writer, writeComment);
19551957
}
19561958

1957-
function emitTrailingDeclarationComments(node: Declaration) {
1959+
function emitTrailingDeclarationComments(node: Node) {
19581960
// Emit the trailing declaration comments only if the parent's end doesnt match
19591961
if (node.parent.kind === SyntaxKind.SourceFile || node.end !== node.parent.end) {
19601962
var trailingComments = getTrailingComments(currentSourceFile.text, node.end);

tests/baselines/reference/ambiguousCallsWhereReturnTypesAgree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var TestClass2 = (function () {
4646
return 0;
4747
};
4848
TestClass2.prototype.foo = function (x) {
49-
return this.bar(x);
49+
return this.bar(x); // should not error
5050
};
5151
return TestClass2;
5252
})();

tests/baselines/reference/arrayAssignmentTest5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var Test;
4343
var lineTokens = this.tokenize(line, state, true);
4444
var tokens = lineTokens.tokens;
4545
if (tokens.length === 0) {
46-
return this.onEnter(line, tokens, offset);
46+
return this.onEnter(line, tokens, offset); // <== this should produce an error since onEnter can not be called with (string, IStateToken[], offset)
4747
}
4848
};
4949
Bug.prototype.tokenize = function (line, state, includeStates) {

tests/baselines/reference/bestCommonTypeOfConditionalExpressions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ var r8 = true ? function (x) {
7575
var r10 = true ? derived : derived2; // no error since we use the contextual type in BCT
7676
var r11 = true ? base : derived2;
7777
function foo5(t, u) {
78-
return true ? t : u;
78+
return true ? t : u; // BCT is Object
7979
}

tests/baselines/reference/bestCommonTypeOfConditionalExpressions2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function foo(t, u) {
6161
return true ? t : u;
6262
}
6363
function foo2(t, u) {
64-
return true ? t : u;
64+
return true ? t : u; // Ok because BCT(T, U) = U
6565
}
6666
function foo3(t, u) {
6767
return true ? t : u;

tests/baselines/reference/collisionThisExpressionAndNameResolution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var Foo = (function () {
2222
function inner() {
2323
var _this = this;
2424
console.log(_this);
25-
return function (x) { return _this; };
25+
return function (x) { return _this; }; // New scope. So should inject new _this capture into function inner
2626
}
2727
};
2828
return Foo;

tests/baselines/reference/collisionThisExpressionAndParameter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,19 @@ var Foo = (function () {
101101
var _this = 10; // Local var. No this capture in x(), so no conflict.
102102
function inner(_this) {
103103
var _this = this;
104-
return function (x) { return _this; };
104+
return function (x) { return _this; }; // New scope. So should inject new _this capture into function inner
105105
}
106106
};
107107
Foo.prototype.y = function () {
108108
var _this = this;
109109
var lamda = function (_this) {
110-
return function (x) { return _this; };
110+
return function (x) { return _this; }; // New scope. So should inject new _this capture
111111
};
112112
};
113113
Foo.prototype.z = function (_this) {
114114
var _this = this;
115115
var lambda = function () {
116-
return function (x) { return _this; };
116+
return function (x) { return _this; }; // New scope. So should inject new _this capture
117117
};
118118
};
119119
Foo.prototype.x1 = function () {
@@ -160,7 +160,7 @@ var Foo3 = (function () {
160160
Foo3.prototype.z = function (_this) {
161161
var _this = this;
162162
var lambda = function () {
163-
return function (x) { return _this; };
163+
return function (x) { return _this; }; // New scope. So should inject new _this capture
164164
};
165165
};
166166
return Foo3;

tests/baselines/reference/collisionThisExpressionAndPropertyNameAsConstuctorParameter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var Foo2 = (function () {
4040
function Foo2(_this) {
4141
var _this = this;
4242
var lambda = function () {
43-
return function (x) { return _this; };
43+
return function (x) { return _this; }; // New scope. So should inject new _this capture
4444
};
4545
}
4646
return Foo2;
@@ -50,7 +50,7 @@ var Foo3 = (function () {
5050
var _this = this;
5151
this._this = _this;
5252
var lambda = function () {
53-
return function (x) { return _this; };
53+
return function (x) { return _this; }; // New scope. So should inject new _this capture
5454
};
5555
}
5656
return Foo3;
@@ -59,7 +59,7 @@ var Foo4 = (function () {
5959
function Foo4(_this) {
6060
var _this = this;
6161
var lambda = function () {
62-
return function (x) { return _this; };
62+
return function (x) { return _this; }; // New scope. So should inject new _this capture
6363
};
6464
}
6565
return Foo4;
@@ -69,7 +69,7 @@ var Foo5 = (function () {
6969
var _this = this;
7070
this._this = _this;
7171
var lambda = function () {
72-
return function (x) { return _this; };
72+
return function (x) { return _this; }; // New scope. So should inject new _this capture
7373
};
7474
}
7575
return Foo5;

tests/baselines/reference/commentsOnReturnStatement1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var DebugClass = (function () {
1616
DebugClass.debugFunc = function () {
1717
// Start Debugger Test Code
1818
var i = 0;
19+
// End Debugger Test Code
1920
return true;
2021
};
2122
return DebugClass;

tests/baselines/reference/constructorWithAssignableReturnExpression.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var C = (function () {
4545
})();
4646
var D = (function () {
4747
function D() {
48-
return 1;
48+
return 1; // error
4949
}
5050
return D;
5151
})();
@@ -57,7 +57,7 @@ var E = (function () {
5757
})();
5858
var F = (function () {
5959
function F() {
60-
return { x: 1 };
60+
return { x: 1 }; // error
6161
}
6262
return F;
6363
})();

0 commit comments

Comments
 (0)