From 486f156a698ac6bfdfe07e8ee0edf758dffec045 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Sat, 12 Nov 2016 01:40:37 -0800 Subject: [PATCH 1/5] Added test. --- tests/cases/compiler/superCallWithCommentEmit01.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/cases/compiler/superCallWithCommentEmit01.ts diff --git a/tests/cases/compiler/superCallWithCommentEmit01.ts b/tests/cases/compiler/superCallWithCommentEmit01.ts new file mode 100644 index 00000000000..800fad29bfb --- /dev/null +++ b/tests/cases/compiler/superCallWithCommentEmit01.ts @@ -0,0 +1,10 @@ +class A { + constructor(public text: string) { } +} + +class B extends A { + constructor(text: string) { + // this is subclass constructor + super(text) + } +} \ No newline at end of file From 63f70dc09abc6cb27349fe20df73e9a0ade608b0 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Sat, 12 Nov 2016 01:54:53 -0800 Subject: [PATCH 2/5] Accepted baselines. --- .../reference/superCallWithCommentEmit01.js | 33 +++++++++++++++++++ .../superCallWithCommentEmit01.symbols | 21 ++++++++++++ .../superCallWithCommentEmit01.types | 22 +++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 tests/baselines/reference/superCallWithCommentEmit01.js create mode 100644 tests/baselines/reference/superCallWithCommentEmit01.symbols create mode 100644 tests/baselines/reference/superCallWithCommentEmit01.types diff --git a/tests/baselines/reference/superCallWithCommentEmit01.js b/tests/baselines/reference/superCallWithCommentEmit01.js new file mode 100644 index 00000000000..10f13b98879 --- /dev/null +++ b/tests/baselines/reference/superCallWithCommentEmit01.js @@ -0,0 +1,33 @@ +//// [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) { + return + // this is subclass constructor + _super.call(this, text) || this; + } + return B; +}(A)); diff --git a/tests/baselines/reference/superCallWithCommentEmit01.symbols b/tests/baselines/reference/superCallWithCommentEmit01.symbols new file mode 100644 index 00000000000..9476ee1c4e5 --- /dev/null +++ b/tests/baselines/reference/superCallWithCommentEmit01.symbols @@ -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)) + } +} diff --git a/tests/baselines/reference/superCallWithCommentEmit01.types b/tests/baselines/reference/superCallWithCommentEmit01.types new file mode 100644 index 00000000000..dfa0b1ce333 --- /dev/null +++ b/tests/baselines/reference/superCallWithCommentEmit01.types @@ -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 + } +} From 2f6ba0876c456591e1ce42974e9d96547c8f2abc Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Sat, 12 Nov 2016 02:56:25 -0800 Subject: [PATCH 3/5] Move comments from super calls to their generated return statements. --- src/compiler/transformers/es2015.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index e4fb8a0aaf0..1cb63f55e91 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -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; } From 03ac7ccee496337640748c15a6a4c3d56cb92e96 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Sat, 12 Nov 2016 02:56:42 -0800 Subject: [PATCH 4/5] Accepted baselines. --- tests/baselines/reference/superCallWithCommentEmit01.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/baselines/reference/superCallWithCommentEmit01.js b/tests/baselines/reference/superCallWithCommentEmit01.js index 10f13b98879..c7ea3cc8b85 100644 --- a/tests/baselines/reference/superCallWithCommentEmit01.js +++ b/tests/baselines/reference/superCallWithCommentEmit01.js @@ -25,9 +25,8 @@ var A = (function () { var B = (function (_super) { __extends(B, _super); function B(text) { - return // this is subclass constructor - _super.call(this, text) || this; + return _super.call(this, text) || this; } return B; }(A)); From f6a570cc7902ba04fcd26931bcfa516d2a03f6e4 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 14 Nov 2016 11:01:05 -0800 Subject: [PATCH 5/5] && -> || --- src/compiler/transformers/es2015.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 1cb63f55e91..07c42eb1fb3 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1014,7 +1014,7 @@ namespace ts { const returnStatement = createReturn(superCallExpression); if (superCallExpression.kind !== SyntaxKind.BinaryExpression - && (superCallExpression as BinaryExpression).left.kind !== SyntaxKind.CallExpression) { + || (superCallExpression as BinaryExpression).left.kind !== SyntaxKind.CallExpression) { Debug.fail("Assumed generated super call would have form 'super.call(...) || this'."); }