Emit leading/trailing comments for return statement

Note the detachedComments and copyright headers comment emitting is not part of this change
This commit is contained in:
Sheetal Nandi 2014-08-15 15:16:17 -07:00
parent 6ab3adfd43
commit 1b5023bad3
35 changed files with 103 additions and 70 deletions

View file

@ -212,10 +212,10 @@ module ts {
var writeEmittedFiles = writeJavaScriptFile;
/** Emit leading comments of the declaration */
var emitLeadingComments = compilerOptions.removeComments ? (d: Declaration) => { } : emitLeadingDeclarationComments;
var emitLeadingComments = compilerOptions.removeComments ? (d: Node) => { } : emitLeadingDeclarationComments;
/** Emit Trailing comments of the declaration */
var emitTrailingComments = compilerOptions.removeComments ? (d: Declaration) => { } : emitTrailingDeclarationComments;
var emitTrailingComments = compilerOptions.removeComments ? (d: Node) => { } : emitTrailingDeclarationComments;
var writeComment = writeCommentRange;
@ -1009,9 +1009,11 @@ module ts {
}
function emitReturnStatement(node: ReturnStatement) {
emitLeadingComments(node);
emitToken(SyntaxKind.ReturnKeyword, node.pos);
emitOptional(" ", node.expression);
write(";");
emitTrailingComments(node);
}
function emitWithStatement(node: WhileStatement) {
@ -1947,14 +1949,14 @@ module ts {
}
}
function emitLeadingDeclarationComments(node: Declaration) {
function emitLeadingDeclarationComments(node: Node) {
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
emitComments(leadingComments, /*trailingSeparator*/ true, writer, writeComment);
}
function emitTrailingDeclarationComments(node: Declaration) {
function emitTrailingDeclarationComments(node: Node) {
// Emit the trailing declaration comments only if the parent's end doesnt match
if (node.parent.kind === SyntaxKind.SourceFile || node.end !== node.parent.end) {
var trailingComments = getTrailingComments(currentSourceFile.text, node.end);

View file

@ -46,7 +46,7 @@ var TestClass2 = (function () {
return 0;
};
TestClass2.prototype.foo = function (x) {
return this.bar(x);
return this.bar(x); // should not error
};
return TestClass2;
})();

View file

@ -43,7 +43,7 @@ var Test;
var lineTokens = this.tokenize(line, state, true);
var tokens = lineTokens.tokens;
if (tokens.length === 0) {
return this.onEnter(line, tokens, offset);
return this.onEnter(line, tokens, offset); // <== this should produce an error since onEnter can not be called with (string, IStateToken[], offset)
}
};
Bug.prototype.tokenize = function (line, state, includeStates) {

View file

@ -75,5 +75,5 @@ var r8 = true ? function (x) {
var r10 = true ? derived : derived2; // no error since we use the contextual type in BCT
var r11 = true ? base : derived2;
function foo5(t, u) {
return true ? t : u;
return true ? t : u; // BCT is Object
}

View file

@ -61,7 +61,7 @@ function foo(t, u) {
return true ? t : u;
}
function foo2(t, u) {
return true ? t : u;
return true ? t : u; // Ok because BCT(T, U) = U
}
function foo3(t, u) {
return true ? t : u;

View file

@ -22,7 +22,7 @@ var Foo = (function () {
function inner() {
var _this = this;
console.log(_this);
return function (x) { return _this; };
return function (x) { return _this; }; // New scope. So should inject new _this capture into function inner
}
};
return Foo;

View file

@ -101,19 +101,19 @@ var Foo = (function () {
var _this = 10; // Local var. No this capture in x(), so no conflict.
function inner(_this) {
var _this = this;
return function (x) { return _this; };
return function (x) { return _this; }; // New scope. So should inject new _this capture into function inner
}
};
Foo.prototype.y = function () {
var _this = this;
var lamda = function (_this) {
return function (x) { return _this; };
return function (x) { return _this; }; // New scope. So should inject new _this capture
};
};
Foo.prototype.z = function (_this) {
var _this = this;
var lambda = function () {
return function (x) { return _this; };
return function (x) { return _this; }; // New scope. So should inject new _this capture
};
};
Foo.prototype.x1 = function () {
@ -160,7 +160,7 @@ var Foo3 = (function () {
Foo3.prototype.z = function (_this) {
var _this = this;
var lambda = function () {
return function (x) { return _this; };
return function (x) { return _this; }; // New scope. So should inject new _this capture
};
};
return Foo3;

View file

@ -40,7 +40,7 @@ var Foo2 = (function () {
function Foo2(_this) {
var _this = this;
var lambda = function () {
return function (x) { return _this; };
return function (x) { return _this; }; // New scope. So should inject new _this capture
};
}
return Foo2;
@ -50,7 +50,7 @@ var Foo3 = (function () {
var _this = this;
this._this = _this;
var lambda = function () {
return function (x) { return _this; };
return function (x) { return _this; }; // New scope. So should inject new _this capture
};
}
return Foo3;
@ -59,7 +59,7 @@ var Foo4 = (function () {
function Foo4(_this) {
var _this = this;
var lambda = function () {
return function (x) { return _this; };
return function (x) { return _this; }; // New scope. So should inject new _this capture
};
}
return Foo4;
@ -69,7 +69,7 @@ var Foo5 = (function () {
var _this = this;
this._this = _this;
var lambda = function () {
return function (x) { return _this; };
return function (x) { return _this; }; // New scope. So should inject new _this capture
};
}
return Foo5;

View file

@ -16,6 +16,7 @@ var DebugClass = (function () {
DebugClass.debugFunc = function () {
// Start Debugger Test Code
var i = 0;
// End Debugger Test Code
return true;
};
return DebugClass;

View file

@ -45,7 +45,7 @@ var C = (function () {
})();
var D = (function () {
function D() {
return 1;
return 1; // error
}
return D;
})();
@ -57,7 +57,7 @@ var E = (function () {
})();
var F = (function () {
function F() {
return { x: 1 };
return { x: 1 }; // error
}
return F;
})();

View file

@ -14,6 +14,9 @@ var TestFile = (function () {
}
TestFile.prototype.foo = function (message) {
var _this = this;
/// <summary>Test summary</summary>
/// <param name="message" type="String" />
/// <returns type="Function" />
return function () { return message + _this.name; };
};
return TestFile;

View file

@ -15,6 +15,9 @@ var TestFile = (function () {
}
TestFile.prototype.foo = function (message) {
var _this = this;
/// <summary>Test summary</summary>
/// <param name="message" type="String" />
/// <returns type="Function" />
return function () { return message + _this.name; };
};
return TestFile;

View file

@ -47,7 +47,7 @@ function f1(x) {
var C = (function () {
function C() {
var bar = (function () {
return bar;
return bar; // 'bar' should be resolvable
});
var b = f1(f1(bar));
}

View file

@ -14,6 +14,7 @@ function fn(x, y) {
var _this = this;
if (x === void 0) { x = function () { return _this; }; }
if (y === void 0) { y = x(); }
// should be 4
return y;
}
fn.call(4);

View file

@ -214,7 +214,7 @@ var b = function () {
var a = function f() {
return new Base();
return new Derived();
return f();
return f(); // ?
}();
undefined === function () {
throw undefined;

View file

@ -252,6 +252,7 @@ var Editor;
return null;
}
else if (entry.isHead) {
// Can't remove the head of a list!
return null;
}
else {

View file

@ -20,12 +20,12 @@ function f1(args) {
var v1;
var v2 = v1['test'];
v2(args);
return new v2(args);
return new v2(args); // used to give error
}
;
function f2(args) {
var v1;
var v2 = v1['test'];
var y = v2(args);
return new v2(args);
return new v2(args); // used to give error
}

View file

@ -13,8 +13,8 @@ function createInstance2<T>(ctor: INewable<T>): T {
//// [genericNewInterface.js]
function createInstance(ctor) {
return new ctor(42);
return new ctor(42); //should be an error
}
function createInstance2(ctor) {
return new ctor(1024);
return new ctor(1024); //should be an error
}

View file

@ -29,7 +29,7 @@ var A = (function () {
return 0;
};
A.two = function (source) {
return this.one(source, 42);
return this.one(source, 42); // should not error
};
return A;
})();

View file

@ -9,8 +9,8 @@ function foo2<T extends { new (): string; }>(f: T) {
//// [genericTypeWithCallableMembers2.js]
function foo1(f) {
return f();
return f(); // should return 'string', once returned 'any'
}
function foo2(f) {
return new f();
return new f(); // should be legal, once was an error
}

View file

@ -13,6 +13,6 @@ function f() {
}
catch (e) {
var stack2 = e.stack;
return stack2;
return stack2; //error TS2095: Could not find symbol 'stack2'.
}
}

View file

@ -13,7 +13,7 @@ f2(3+""); // ok + operator promotes
//// [numberToString.js]
function f1(n) {
return n;
return n; // error return type mismatch
}
function f2(s) {
}

View file

@ -47,17 +47,21 @@ var Accessor = (function () {
})();
function attr(nameOrMap, value) {
if (nameOrMap && typeof nameOrMap === "object") {
// handle map case
return new Accessor;
}
else {
// handle string case
return "s";
}
}
function attr2(nameOrMap, value) {
if (nameOrMap && typeof nameOrMap === "object") {
// handle map case
return "t";
}
else {
// handle string case
return "s";
}
}

View file

@ -31,9 +31,11 @@ var Accessor = (function () {
})();
function attr(nameOrMap, value) {
if (nameOrMap && typeof nameOrMap === "object") {
// handle map case
return new Accessor;
}
else {
// handle string case
return "s";
}
}

View file

@ -265,7 +265,7 @@ var m1;
return new C2_private();
};
C3_public.prototype.f8_public = function () {
return new C2_private();
return new C2_private(); // error
};
C3_public.prototype.f9_private = function () {
return new C1_public();
@ -277,7 +277,7 @@ var m1;
return new C2_private();
};
C3_public.prototype.f12_public = function () {
return new C2_private();
return new C2_private(); //error
};
return C3_public;
})();
@ -362,7 +362,7 @@ var m1;
return new C2_private();
}
function f8_public() {
return new C2_private();
return new C2_private(); // error
}
m1.f8_public = f8_public;
function f9_private() {
@ -376,7 +376,7 @@ var m1;
return new C2_private();
}
function f12_public() {
return new C2_private();
return new C2_private(); //error
}
m1.f12_public = f12_public;
})(m1 || (m1 = {}));

View file

@ -255,7 +255,7 @@ define(["require", "exports"], function (require, exports) {
});
Object.defineProperty(C3_public.prototype, "p4_public", {
get: function () {
return new C2_private();
return new C2_private(); //error
},
set: function (m1_c3_p4_arg) {
},
@ -452,7 +452,7 @@ define(["require", "exports"], function (require, exports) {
});
Object.defineProperty(C7_public.prototype, "p4_public", {
get: function () {
return new C5_private();
return new C5_private(); //error
},
set: function (m1_c3_p4_arg) {
},

View file

@ -567,7 +567,7 @@ define(["require", "exports"], function (require, exports) {
return new C2_private();
};
C3_public.prototype.f8_public = function () {
return new C2_private();
return new C2_private(); // error
};
C3_public.prototype.f9_private = function () {
return new C1_public();
@ -579,7 +579,7 @@ define(["require", "exports"], function (require, exports) {
return new C2_private();
};
C3_public.prototype.f12_public = function () {
return new C2_private();
return new C2_private(); //error
};
return C3_public;
})();
@ -664,7 +664,7 @@ define(["require", "exports"], function (require, exports) {
return new C2_private();
}
function f8_public() {
return new C2_private();
return new C2_private(); // error
}
m1.f8_public = f8_public;
function f9_private() {
@ -678,7 +678,7 @@ define(["require", "exports"], function (require, exports) {
return new C2_private();
}
function f12_public() {
return new C2_private();
return new C2_private(); //error
}
m1.f12_public = f12_public;
})(exports.m1 || (exports.m1 = {}));
@ -868,7 +868,7 @@ define(["require", "exports"], function (require, exports) {
return new C5_private();
};
C7_public.prototype.f8_public = function () {
return new C5_private();
return new C5_private(); //error
};
C7_public.prototype.f9_private = function () {
return new C6_public();
@ -880,7 +880,7 @@ define(["require", "exports"], function (require, exports) {
return new C5_private();
};
C7_public.prototype.f12_public = function () {
return new C5_private();
return new C5_private(); //error
};
return C7_public;
})();
@ -965,7 +965,7 @@ define(["require", "exports"], function (require, exports) {
return new C5_private();
}
function f8_public() {
return new C5_private();
return new C5_private(); //error
}
exports.f8_public = f8_public;
function f9_private() {
@ -979,7 +979,7 @@ define(["require", "exports"], function (require, exports) {
return new C5_private();
}
function f12_public() {
return new C5_private();
return new C5_private(); //error
}
exports.f12_public = f12_public;
});

View file

@ -136,7 +136,7 @@ var m1;
});
Object.defineProperty(C3_public.prototype, "p4_public", {
get: function () {
return new C2_private();
return new C2_private(); //error
},
set: function (m1_c3_p4_arg) {
},

View file

@ -17,6 +17,7 @@ foo.then((x) => {
//// [propagationOfPromiseInitialization.js]
var foo;
foo.then(function (x) {
// x is inferred to be a number
return "asdf";
}).then(function (x) {
x.length;

View file

@ -29,7 +29,7 @@ var Foo = (function () {
this._store = store; // no repro if this is first line in class body
}
Foo.prototype.foo = function () {
return this._store.length;
return this._store.length; // shouldn't be an error
};
Foo.prototype.bar = function () {
return this.store;
@ -41,7 +41,7 @@ var Bar = (function () {
this._store = store;
}
Bar.prototype.foo = function () {
return this._store.length;
return this._store.length; // shouldn't be an error
};
return Bar;
})();

View file

@ -83,7 +83,7 @@ var A = (function () {
})();
var B = (function () {
function B() {
return 1;
return 1; // error
}
B.prototype.foo = function () {
};
@ -99,7 +99,7 @@ var C = (function () {
})();
var D = (function () {
function D() {
return "test";
return "test"; // error
}
D.prototype.foo = function () {
};
@ -113,7 +113,7 @@ var E = (function () {
})();
var F = (function () {
function F() {
return { foo: 1 };
return { foo: 1 }; //error
}
return F;
})();
@ -131,7 +131,7 @@ var H = (function (_super) {
__extends(H, _super);
function H() {
_super.call(this);
return new G();
return new G(); //error
}
return H;
})(F);

View file

@ -37,6 +37,7 @@ var DebugClass = (function () {
i++;
i++;
i++;
// End Debugger Test Code
return true;
};
return DebugClass;

View file

@ -1,2 +1,2 @@
//// [sourceMapValidationWithComments.js.map]
{"version":3,"file":"sourceMapValidationWithComments.js","sourceRoot":"","sources":["sourceMapValidationWithComments.ts"],"names":["DebugClass","DebugClass.constructor","DebugClass.debugFunc"],"mappings":"AAAA,IAAM,UAAU;IAAhBA,SAAMA,UAAUA;IAoBhBC,CAACA;IAlBiBD,oBAASA,GAAvBA;QAGIE,AADAA,2BAA2BA;YACvBA,CAACA,GAAGA,CAACA,CAACA;QACVA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QAIJA,MAAMA,CAACA,IAAIA,CAACA;IAChBA,CAACA;IACLF,iBAACA;AAADA,CAACA,AApBD,IAoBC"}
{"version":3,"file":"sourceMapValidationWithComments.js","sourceRoot":"","sources":["sourceMapValidationWithComments.ts"],"names":["DebugClass","DebugClass.constructor","DebugClass.debugFunc"],"mappings":"AAAA,IAAM,UAAU;IAAhBA,SAAMA,UAAUA;IAoBhBC,CAACA;IAlBiBD,oBAASA,GAAvBA;QAGIE,AADAA,2BAA2BA;YACvBA,CAACA,GAAGA,CAACA,CAACA;QACVA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QACJA,CAACA,EAAEA,CAACA;QAIJA,AAHAA,yBAAyBA;QAGzBA,MAAMA,CAACA,IAAIA,CAACA;IAChBA,CAACA;IACLF,iBAACA;AAADA,CAACA,AApBD,IAoBC"}

View file

@ -237,7 +237,7 @@ sourceFile:sourceMapValidationWithComments.ts
2 > ^
3 > ^^
4 > ^
5 > ^^^^^^^^^->
5 > ^^^^^^^^^^^^^^^^^^^^^^->
1->
>
2 > i
@ -248,14 +248,28 @@ sourceFile:sourceMapValidationWithComments.ts
3 >Emitted(15, 12) Source(15, 12) + SourceIndex(0) name (DebugClass.debugFunc)
4 >Emitted(15, 13) Source(15, 13) + SourceIndex(0) name (DebugClass.debugFunc)
---
>>> return true;
>>> // End Debugger Test Code
1->^^^^^^^^
2 >
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^
1->
> // End Debugger Test Code
>
>
>
2 >
3 > // End Debugger Test Code
1->Emitted(16, 9) Source(19, 9) + SourceIndex(0) name (DebugClass.debugFunc)
2 >Emitted(16, 9) Source(16, 9) + SourceIndex(0) name (DebugClass.debugFunc)
3 >Emitted(16, 34) Source(16, 34) + SourceIndex(0) name (DebugClass.debugFunc)
---
>>> return true;
1 >^^^^^^^^
2 > ^^^^^^
3 > ^
4 > ^^^^
5 > ^
1->
> // End Debugger Test Code
1 >
>
>
>
@ -263,11 +277,11 @@ sourceFile:sourceMapValidationWithComments.ts
3 >
4 > true
5 > ;
1->Emitted(16, 9) Source(19, 9) + SourceIndex(0) name (DebugClass.debugFunc)
2 >Emitted(16, 15) Source(19, 15) + SourceIndex(0) name (DebugClass.debugFunc)
3 >Emitted(16, 16) Source(19, 16) + SourceIndex(0) name (DebugClass.debugFunc)
4 >Emitted(16, 20) Source(19, 20) + SourceIndex(0) name (DebugClass.debugFunc)
5 >Emitted(16, 21) Source(19, 21) + SourceIndex(0) name (DebugClass.debugFunc)
1 >Emitted(17, 9) Source(19, 9) + SourceIndex(0) name (DebugClass.debugFunc)
2 >Emitted(17, 15) Source(19, 15) + SourceIndex(0) name (DebugClass.debugFunc)
3 >Emitted(17, 16) Source(19, 16) + SourceIndex(0) name (DebugClass.debugFunc)
4 >Emitted(17, 20) Source(19, 20) + SourceIndex(0) name (DebugClass.debugFunc)
5 >Emitted(17, 21) Source(19, 21) + SourceIndex(0) name (DebugClass.debugFunc)
---
>>> };
1 >^^^^
@ -276,8 +290,8 @@ sourceFile:sourceMapValidationWithComments.ts
1 >
>
2 > }
1 >Emitted(17, 5) Source(20, 5) + SourceIndex(0) name (DebugClass.debugFunc)
2 >Emitted(17, 6) Source(20, 6) + SourceIndex(0) name (DebugClass.debugFunc)
1 >Emitted(18, 5) Source(20, 5) + SourceIndex(0) name (DebugClass.debugFunc)
2 >Emitted(18, 6) Source(20, 6) + SourceIndex(0) name (DebugClass.debugFunc)
---
>>> return DebugClass;
1->^^^^
@ -285,8 +299,8 @@ sourceFile:sourceMapValidationWithComments.ts
1->
>
2 > }
1->Emitted(18, 5) Source(21, 1) + SourceIndex(0) name (DebugClass)
2 >Emitted(18, 22) Source(21, 2) + SourceIndex(0) name (DebugClass)
1->Emitted(19, 5) Source(21, 1) + SourceIndex(0) name (DebugClass)
2 >Emitted(19, 22) Source(21, 2) + SourceIndex(0) name (DebugClass)
---
>>>})();
1 >
@ -318,9 +332,9 @@ sourceFile:sourceMapValidationWithComments.ts
> return true;
> }
> }
1 >Emitted(19, 1) Source(21, 1) + SourceIndex(0) name (DebugClass)
2 >Emitted(19, 2) Source(21, 2) + SourceIndex(0) name (DebugClass)
3 >Emitted(19, 2) Source(1, 1) + SourceIndex(0)
4 >Emitted(19, 6) Source(21, 2) + SourceIndex(0)
1 >Emitted(20, 1) Source(21, 1) + SourceIndex(0) name (DebugClass)
2 >Emitted(20, 2) Source(21, 2) + SourceIndex(0) name (DebugClass)
3 >Emitted(20, 2) Source(1, 1) + SourceIndex(0)
4 >Emitted(20, 6) Source(21, 2) + SourceIndex(0)
---
>>>//# sourceMappingURL=sourceMapValidationWithComments.js.map

View file

@ -32,7 +32,7 @@ function getMaxWidth(elementNames) {
return !e.isDisabled;
});
var widths = enabled.map(function (e) {
return e.xyxyxyx;
return e.xyxyxyx; // error expected here
});
var maxWidth = widths.reduce(function (a, b) {
return a > b ? a : b;