Skip to content

Commit 9abcddc

Browse files
committed
Simplify emit for syntactic 'extends null' case
1 parent 6c29e58 commit 9abcddc

File tree

8 files changed

+12
-20
lines changed

8 files changed

+12
-20
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,10 @@ namespace ts {
925925

926926
}
927927

928-
const superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, constructor, !!extendsClauseElement, hasSynthesizedSuper, statementOffset);
928+
// determine whether the class is known syntactically to be a derived class (e.g. a
929+
// class that extends a value that is not syntactically known to be `null`).
930+
const isDerivedClass = extendsClauseElement && skipOuterExpressions(extendsClauseElement.expression).kind !== SyntaxKind.NullKeyword;
931+
const superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, constructor, isDerivedClass, hasSynthesizedSuper, statementOffset);
929932

930933
// The last statement expression was replaced. Skip it.
931934
if (superCaptureStatus === SuperCaptureResult.ReplaceSuperCapture || superCaptureStatus === SuperCaptureResult.ReplaceWithReturn) {
@@ -942,7 +945,7 @@ namespace ts {
942945

943946
// Return `_this` unless we're sure enough that it would be pointless to add a return statement.
944947
// If there's a constructor that we can tell returns in enough places, then we *do not* want to add a return.
945-
if (extendsClauseElement
948+
if (isDerivedClass
946949
&& superCaptureStatus !== SuperCaptureResult.ReplaceWithReturn
947950
&& !(constructor && isSufficientlyCoveredByReturnStatements(constructor.body))) {
948951
statements.push(
@@ -1011,11 +1014,11 @@ namespace ts {
10111014
function declareOrCaptureOrReturnThisForConstructorIfNeeded(
10121015
statements: Statement[],
10131016
ctor: ConstructorDeclaration | undefined,
1014-
hasExtendsClause: boolean,
1017+
isDerivedClass: boolean,
10151018
hasSynthesizedSuper: boolean,
10161019
statementOffset: number) {
10171020
// If this isn't a derived class, just capture 'this' for arrow functions if necessary.
1018-
if (!hasExtendsClause) {
1021+
if (!isDerivedClass) {
10191022
if (ctor) {
10201023
addCaptureThisForNodeIfNeeded(statements, ctor);
10211024
}

tests/baselines/reference/classExtendingNull.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ var __extends = (this && this.__extends) || (function () {
1717
var C1 = (function (_super) {
1818
__extends(C1, _super);
1919
function C1() {
20-
return _super !== null && _super.apply(this, arguments) || this;
2120
}
2221
return C1;
2322
}(null));
2423
var C2 = (function (_super) {
2524
__extends(C2, _super);
2625
function C2() {
27-
return _super !== null && _super.apply(this, arguments) || this;
2826
}
2927
return C2;
3028
}((null)));

tests/baselines/reference/classExtendingPrimitive.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ var C5 = (function (_super) {
7070
var C5a = (function (_super) {
7171
__extends(C5a, _super);
7272
function C5a() {
73-
return _super !== null && _super.apply(this, arguments) || this;
7473
}
7574
return C5a;
7675
}(null));

tests/baselines/reference/classExtendingPrimitive2.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ void {};
2525
var C5a = (function (_super) {
2626
__extends(C5a, _super);
2727
function C5a() {
28-
return _super !== null && _super.apply(this, arguments) || this;
2928
}
3029
return C5a;
3130
}(null));

tests/baselines/reference/classExtendsNull.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ var __extends = (this && this.__extends) || (function () {
2626
var C = (function (_super) {
2727
__extends(C, _super);
2828
function C() {
29-
var _this = _super.call(this) || this;
29+
_this = _super.call(this) || this;
3030
return Object.create(null);
3131
}
3232
return C;
3333
}(null));
3434
var D = (function (_super) {
3535
__extends(D, _super);
3636
function D() {
37-
var _this = this;
3837
return Object.create(null);
3938
}
4039
return D;

tests/baselines/reference/declFileClassExtendsNull.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var __extends = (this && this.__extends) || (function () {
1717
var ExtendsNull = (function (_super) {
1818
__extends(ExtendsNull, _super);
1919
function ExtendsNull() {
20-
return _super !== null && _super.apply(this, arguments) || this;
2120
}
2221
return ExtendsNull;
2322
}(null));

tests/baselines/reference/superCallBeforeThisAccessing4.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,16 @@ var __extends = (this && this.__extends) || (function () {
2929
var D = (function (_super) {
3030
__extends(D, _super);
3131
function D() {
32-
var _this = this;
33-
_this._t;
32+
this._t;
3433
_this = _super.call(this) || this;
35-
return _this;
3634
}
3735
return D;
3836
}(null));
3937
var E = (function (_super) {
4038
__extends(E, _super);
4139
function E() {
42-
var _this = _super.call(this) || this;
43-
_this._t;
44-
return _this;
40+
_this = _super.call(this) || this;
41+
this._t;
4542
}
4643
return E;
4744
}(null));

tests/baselines/reference/superCallBeforeThisAccessing5.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ var __extends = (this && this.__extends) || (function () {
2121
var D = (function (_super) {
2222
__extends(D, _super);
2323
function D() {
24-
var _this = this;
25-
_this._t; // No error
26-
return _this;
24+
this._t; // No error
2725
}
2826
return D;
2927
}(null));

0 commit comments

Comments
 (0)