Skip to content

Commit 1164df2

Browse files
authored
Merge pull request #12197 from Microsoft/commentsSuperCallReturns
Fix up comments for super calls rewritten as returns
2 parents 6e3bbcc + f6a570c commit 1164df2

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ namespace ts {
279279
else if (node.transformFlags & TransformFlags.ContainsES2015 || (isInConstructorWithCapturedSuper && !isExpression(node))) {
280280
// we want to dive in this branch either if node has children with ES2015 specific syntax
281281
// or we are inside constructor that captures result of the super call so all returns without expression should be
282-
// rewritten. Note: we skip expressions since returns should never appear there
282+
// rewritten. Note: we skip expressions since returns should never appear there
283283
return visitEachChild(node, visitor, context);
284284
}
285285
else {
@@ -1011,7 +1011,20 @@ namespace ts {
10111011

10121012
// Return the result if we have an immediate super() call on the last statement.
10131013
if (superCallExpression && statementOffset === ctorStatements.length - 1) {
1014-
statements.push(createReturn(superCallExpression));
1014+
const returnStatement = createReturn(superCallExpression);
1015+
1016+
if (superCallExpression.kind !== SyntaxKind.BinaryExpression
1017+
|| (superCallExpression as BinaryExpression).left.kind !== SyntaxKind.CallExpression) {
1018+
Debug.fail("Assumed generated super call would have form 'super.call(...) || this'.");
1019+
}
1020+
1021+
// Shift comments from the original super call to the return statement.
1022+
setCommentRange(returnStatement, getCommentRange(
1023+
setEmitFlags(
1024+
(superCallExpression as BinaryExpression).left,
1025+
EmitFlags.NoComments)));
1026+
1027+
statements.push(returnStatement);
10151028
return SuperCaptureResult.ReplaceWithReturn;
10161029
}
10171030

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [superCallWithCommentEmit01.ts]
2+
class A {
3+
constructor(public text: string) { }
4+
}
5+
6+
class B extends A {
7+
constructor(text: string) {
8+
// this is subclass constructor
9+
super(text)
10+
}
11+
}
12+
13+
//// [superCallWithCommentEmit01.js]
14+
var __extends = (this && this.__extends) || function (d, b) {
15+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
16+
function __() { this.constructor = d; }
17+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
18+
};
19+
var A = (function () {
20+
function A(text) {
21+
this.text = text;
22+
}
23+
return A;
24+
}());
25+
var B = (function (_super) {
26+
__extends(B, _super);
27+
function B(text) {
28+
// this is subclass constructor
29+
return _super.call(this, text) || this;
30+
}
31+
return B;
32+
}(A));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/superCallWithCommentEmit01.ts ===
2+
class A {
3+
>A : Symbol(A, Decl(superCallWithCommentEmit01.ts, 0, 0))
4+
5+
constructor(public text: string) { }
6+
>text : Symbol(A.text, Decl(superCallWithCommentEmit01.ts, 1, 16))
7+
}
8+
9+
class B extends A {
10+
>B : Symbol(B, Decl(superCallWithCommentEmit01.ts, 2, 1))
11+
>A : Symbol(A, Decl(superCallWithCommentEmit01.ts, 0, 0))
12+
13+
constructor(text: string) {
14+
>text : Symbol(text, Decl(superCallWithCommentEmit01.ts, 5, 16))
15+
16+
// this is subclass constructor
17+
super(text)
18+
>super : Symbol(A, Decl(superCallWithCommentEmit01.ts, 0, 0))
19+
>text : Symbol(text, Decl(superCallWithCommentEmit01.ts, 5, 16))
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/superCallWithCommentEmit01.ts ===
2+
class A {
3+
>A : A
4+
5+
constructor(public text: string) { }
6+
>text : string
7+
}
8+
9+
class B extends A {
10+
>B : B
11+
>A : A
12+
13+
constructor(text: string) {
14+
>text : string
15+
16+
// this is subclass constructor
17+
super(text)
18+
>super(text) : void
19+
>super : typeof A
20+
>text : string
21+
}
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class A {
2+
constructor(public text: string) { }
3+
}
4+
5+
class B extends A {
6+
constructor(text: string) {
7+
// this is subclass constructor
8+
super(text)
9+
}
10+
}

0 commit comments

Comments
 (0)