Skip to content

Commit 58ad85a

Browse files
authored
Merge pull request #13277 from Microsoft/fix13276
Fix super/this capture for extends null
2 parents 9a62db2 + 9abcddc commit 58ad85a

File tree

585 files changed

+1227
-1225
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

585 files changed

+1227
-1225
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 25 additions & 12 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
}
@@ -1091,7 +1094,7 @@ namespace ts {
10911094
}
10921095

10931096
// Perform the capture.
1094-
captureThisForNode(statements, ctor, superCallExpression, firstStatement);
1097+
captureThisForNode(statements, ctor, superCallExpression || createActualThis(), firstStatement);
10951098

10961099
// If we're actually replacing the original statement, we need to signal this to the caller.
10971100
if (superCallExpression) {
@@ -1101,15 +1104,25 @@ namespace ts {
11011104
return SuperCaptureResult.NoReplacement;
11021105
}
11031106

1107+
function createActualThis() {
1108+
return setEmitFlags(createThis(), EmitFlags.NoSubstitution);
1109+
}
1110+
11041111
function createDefaultSuperCallOrThis() {
1105-
const actualThis = createThis();
1106-
setEmitFlags(actualThis, EmitFlags.NoSubstitution);
1107-
const superCall = createFunctionApply(
1108-
createIdentifier("_super"),
1109-
actualThis,
1110-
createIdentifier("arguments"),
1112+
return createLogicalOr(
1113+
createLogicalAnd(
1114+
createStrictInequality(
1115+
createIdentifier("_super"),
1116+
createNull()
1117+
),
1118+
createFunctionApply(
1119+
createIdentifier("_super"),
1120+
createActualThis(),
1121+
createIdentifier("arguments"),
1122+
)
1123+
),
1124+
createActualThis()
11111125
);
1112-
return createLogicalOr(superCall, actualThis);
11131126
}
11141127

11151128
/**

tests/baselines/reference/ExportClassWithAccessibleTypesInTypeParameterConstraintsClassHeritageListMemberTypeAnnotations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var A;
4343
var Point3d = (function (_super) {
4444
__extends(Point3d, _super);
4545
function Point3d() {
46-
return _super.apply(this, arguments) || this;
46+
return _super !== null && _super.apply(this, arguments) || this;
4747
}
4848
return Point3d;
4949
}(Point));

tests/baselines/reference/ExportClassWithInaccessibleTypeInTypeParameterConstraint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var A;
4646
var Point3d = (function (_super) {
4747
__extends(Point3d, _super);
4848
function Point3d() {
49-
return _super.apply(this, arguments) || this;
49+
return _super !== null && _super.apply(this, arguments) || this;
5050
}
5151
return Point3d;
5252
}(Point));

tests/baselines/reference/abstractClassInLocalScope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var __extends = (this && this.__extends) || (function () {
2727
var B = (function (_super) {
2828
__extends(B, _super);
2929
function B() {
30-
return _super.apply(this, arguments) || this;
30+
return _super !== null && _super.apply(this, arguments) || this;
3131
}
3232
return B;
3333
}(A));

tests/baselines/reference/abstractClassInLocalScopeIsAbstract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var __extends = (this && this.__extends) || (function () {
2727
var B = (function (_super) {
2828
__extends(B, _super);
2929
function B() {
30-
return _super.apply(this, arguments) || this;
30+
return _super !== null && _super.apply(this, arguments) || this;
3131
}
3232
return B;
3333
}(A));

tests/baselines/reference/abstractProperty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var B = (function () {
4040
var C = (function (_super) {
4141
__extends(C, _super);
4242
function C() {
43-
var _this = _super.apply(this, arguments) || this;
43+
var _this = _super !== null && _super.apply(this, arguments) || this;
4444
_this.raw = "edge";
4545
_this.ro = "readonly please";
4646
return _this;

tests/baselines/reference/abstractPropertyNegative.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var B = (function () {
6262
var C = (function (_super) {
6363
__extends(C, _super);
6464
function C() {
65-
var _this = _super.apply(this, arguments) || this;
65+
var _this = _super !== null && _super.apply(this, arguments) || this;
6666
_this.ro = "readonly please";
6767
return _this;
6868
}
@@ -83,7 +83,7 @@ var WrongTypeProperty = (function () {
8383
var WrongTypePropertyImpl = (function (_super) {
8484
__extends(WrongTypePropertyImpl, _super);
8585
function WrongTypePropertyImpl() {
86-
var _this = _super.apply(this, arguments) || this;
86+
var _this = _super !== null && _super.apply(this, arguments) || this;
8787
_this.num = "nope, wrong";
8888
return _this;
8989
}
@@ -97,7 +97,7 @@ var WrongTypeAccessor = (function () {
9797
var WrongTypeAccessorImpl = (function (_super) {
9898
__extends(WrongTypeAccessorImpl, _super);
9999
function WrongTypeAccessorImpl() {
100-
return _super.apply(this, arguments) || this;
100+
return _super !== null && _super.apply(this, arguments) || this;
101101
}
102102
Object.defineProperty(WrongTypeAccessorImpl.prototype, "num", {
103103
get: function () { return "nope, wrong"; },
@@ -109,7 +109,7 @@ var WrongTypeAccessorImpl = (function (_super) {
109109
var WrongTypeAccessorImpl2 = (function (_super) {
110110
__extends(WrongTypeAccessorImpl2, _super);
111111
function WrongTypeAccessorImpl2() {
112-
var _this = _super.apply(this, arguments) || this;
112+
var _this = _super !== null && _super.apply(this, arguments) || this;
113113
_this.num = "nope, wrong";
114114
return _this;
115115
}

tests/baselines/reference/accessors_spec_section-4.5_inference.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var A = (function () {
4343
var B = (function (_super) {
4444
__extends(B, _super);
4545
function B() {
46-
return _super.apply(this, arguments) || this;
46+
return _super !== null && _super.apply(this, arguments) || this;
4747
}
4848
return B;
4949
}(A));

tests/baselines/reference/aliasUsageInAccessorsOfClass.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var Backbone = require("./aliasUsage1_backbone");
5151
var VisualizationModel = (function (_super) {
5252
__extends(VisualizationModel, _super);
5353
function VisualizationModel() {
54-
return _super.apply(this, arguments) || this;
54+
return _super !== null && _super.apply(this, arguments) || this;
5555
}
5656
return VisualizationModel;
5757
}(Backbone.Model));

tests/baselines/reference/aliasUsageInArray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var Backbone = require("./aliasUsageInArray_backbone");
4545
var VisualizationModel = (function (_super) {
4646
__extends(VisualizationModel, _super);
4747
function VisualizationModel() {
48-
return _super.apply(this, arguments) || this;
48+
return _super !== null && _super.apply(this, arguments) || this;
4949
}
5050
return VisualizationModel;
5151
}(Backbone.Model));

0 commit comments

Comments
 (0)