Merge pull request #12197 from Microsoft/commentsSuperCallReturns

Fix up comments for super calls rewritten as returns
This commit is contained in:
Mohamed Hegazy
2016-11-14 16:10:03 -08:00
committed by GitHub
5 changed files with 100 additions and 2 deletions

View File

@@ -279,7 +279,7 @@ namespace ts {
else if (node.transformFlags & TransformFlags.ContainsES2015 || (isInConstructorWithCapturedSuper && !isExpression(node))) {
// we want to dive in this branch either if node has children with ES2015 specific syntax
// or we are inside constructor that captures result of the super call so all returns without expression should be
// rewritten. Note: we skip expressions since returns should never appear there
// rewritten. Note: we skip expressions since returns should never appear there
return visitEachChild(node, visitor, context);
}
else {
@@ -1011,7 +1011,20 @@ namespace ts {
// Return the result if we have an immediate super() call on the last statement.
if (superCallExpression && statementOffset === ctorStatements.length - 1) {
statements.push(createReturn(superCallExpression));
const returnStatement = createReturn(superCallExpression);
if (superCallExpression.kind !== SyntaxKind.BinaryExpression
|| (superCallExpression as BinaryExpression).left.kind !== SyntaxKind.CallExpression) {
Debug.fail("Assumed generated super call would have form 'super.call(...) || this'.");
}
// Shift comments from the original super call to the return statement.
setCommentRange(returnStatement, getCommentRange(
setEmitFlags(
(superCallExpression as BinaryExpression).left,
EmitFlags.NoComments)));
statements.push(returnStatement);
return SuperCaptureResult.ReplaceWithReturn;
}

View File

@@ -0,0 +1,32 @@
//// [superCallWithCommentEmit01.ts]
class A {
constructor(public text: string) { }
}
class B extends A {
constructor(text: string) {
// this is subclass constructor
super(text)
}
}
//// [superCallWithCommentEmit01.js]
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var A = (function () {
function A(text) {
this.text = text;
}
return A;
}());
var B = (function (_super) {
__extends(B, _super);
function B(text) {
// this is subclass constructor
return _super.call(this, text) || this;
}
return B;
}(A));

View File

@@ -0,0 +1,21 @@
=== tests/cases/compiler/superCallWithCommentEmit01.ts ===
class A {
>A : Symbol(A, Decl(superCallWithCommentEmit01.ts, 0, 0))
constructor(public text: string) { }
>text : Symbol(A.text, Decl(superCallWithCommentEmit01.ts, 1, 16))
}
class B extends A {
>B : Symbol(B, Decl(superCallWithCommentEmit01.ts, 2, 1))
>A : Symbol(A, Decl(superCallWithCommentEmit01.ts, 0, 0))
constructor(text: string) {
>text : Symbol(text, Decl(superCallWithCommentEmit01.ts, 5, 16))
// this is subclass constructor
super(text)
>super : Symbol(A, Decl(superCallWithCommentEmit01.ts, 0, 0))
>text : Symbol(text, Decl(superCallWithCommentEmit01.ts, 5, 16))
}
}

View File

@@ -0,0 +1,22 @@
=== tests/cases/compiler/superCallWithCommentEmit01.ts ===
class A {
>A : A
constructor(public text: string) { }
>text : string
}
class B extends A {
>B : B
>A : A
constructor(text: string) {
>text : string
// this is subclass constructor
super(text)
>super(text) : void
>super : typeof A
>text : string
}
}

View File

@@ -0,0 +1,10 @@
class A {
constructor(public text: string) { }
}
class B extends A {
constructor(text: string) {
// this is subclass constructor
super(text)
}
}