Skip to content

Commit bcdf1dc

Browse files
Merge pull request #10762 from Microsoft/useReturnedThisFromSuperCalls
Use returned values from super calls as 'this'
2 parents edd8eb8 + 02b9917 commit bcdf1dc

File tree

680 files changed

+3489
-1959
lines changed

Some content is hidden

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

680 files changed

+3489
-1959
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9421,8 +9421,8 @@ namespace ts {
94219421
let container = getSuperContainer(node, /*stopOnFunctions*/ true);
94229422
let needToCaptureLexicalThis = false;
94239423

9424+
// adjust the container reference in case if super is used inside arrow functions with arbitrarily deep nesting
94249425
if (!isCallExpression) {
9425-
// adjust the container reference in case if super is used inside arrow functions with arbitrary deep nesting
94269426
while (container && container.kind === SyntaxKind.ArrowFunction) {
94279427
container = getSuperContainer(container, /*stopOnFunctions*/ true);
94289428
needToCaptureLexicalThis = languageVersion < ScriptTarget.ES6;
@@ -14439,6 +14439,7 @@ namespace ts {
1443914439
// constructors of derived classes must contain at least one super call somewhere in their function body.
1444014440
const containingClassDecl = <ClassDeclaration>node.parent;
1444114441
if (getClassExtendsHeritageClauseElement(containingClassDecl)) {
14442+
captureLexicalThis(node.parent, containingClassDecl);
1444214443
const classExtendsNull = classDeclarationExtendsNull(containingClassDecl);
1444314444
const superCall = getSuperCallInConstructor(node);
1444414445
if (superCall) {

src/compiler/transformers/es6.ts

Lines changed: 234 additions & 54 deletions
Large diffs are not rendered by default.

src/compiler/utilities.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -982,11 +982,11 @@ namespace ts {
982982
}
983983

984984
/**
985-
* Given an super call\property node returns a closest node where either
986-
* - super call\property is legal in the node and not legal in the parent node the node.
985+
* Given an super call/property node, returns the closest node where
986+
* - a super call/property access is legal in the node and not legal in the parent node the node.
987987
* i.e. super call is legal in constructor but not legal in the class body.
988-
* - node is arrow function (so caller might need to call getSuperContainer in case it needs to climb higher)
989-
* - super call\property is definitely illegal in the node (but might be legal in some subnode)
988+
* - the container is an arrow function (so caller might need to call getSuperContainer again in case it needs to climb higher)
989+
* - a super call/property is definitely illegal in the container (but might be legal in some subnode)
990990
* i.e. super property access is illegal in function declaration but can be legal in the statement list
991991
*/
992992
export function getSuperContainer(node: Node, stopOnFunctions: boolean): Node {

tests/baselines/reference/ExportClassWithAccessibleTypesInTypeParameterConstraintsClassHeritageListMemberTypeAnnotations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var A;
3838
var Point3d = (function (_super) {
3939
__extends(Point3d, _super);
4040
function Point3d() {
41-
_super.apply(this, arguments);
41+
return _super.apply(this, arguments) || this;
4242
}
4343
return Point3d;
4444
}(Point));

tests/baselines/reference/ExportClassWithInaccessibleTypeInTypeParameterConstraint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var A;
4141
var Point3d = (function (_super) {
4242
__extends(Point3d, _super);
4343
function Point3d() {
44-
_super.apply(this, arguments);
44+
return _super.apply(this, arguments) || this;
4545
}
4646
return Point3d;
4747
}(Point));

tests/baselines/reference/abstractClassInLocalScope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var __extends = (this && this.__extends) || function (d, b) {
2222
var B = (function (_super) {
2323
__extends(B, _super);
2424
function B() {
25-
_super.apply(this, arguments);
25+
return _super.apply(this, arguments) || this;
2626
}
2727
return B;
2828
}(A));

tests/baselines/reference/abstractClassInLocalScopeIsAbstract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var __extends = (this && this.__extends) || function (d, b) {
2222
var B = (function (_super) {
2323
__extends(B, _super);
2424
function B() {
25-
_super.apply(this, arguments);
25+
return _super.apply(this, arguments) || this;
2626
}
2727
return B;
2828
}(A));

tests/baselines/reference/abstractProperty.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ var B = (function () {
3535
var C = (function (_super) {
3636
__extends(C, _super);
3737
function C() {
38-
_super.apply(this, arguments);
39-
this.raw = "edge";
40-
this.ro = "readonly please";
38+
var _this = _super.apply(this, arguments) || this;
39+
_this.raw = "edge";
40+
_this.ro = "readonly please";
41+
return _this;
4142
}
4243
Object.defineProperty(C.prototype, "prop", {
4344
get: function () { return "foo"; },

tests/baselines/reference/abstractPropertyNegative.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ var B = (function () {
5757
var C = (function (_super) {
5858
__extends(C, _super);
5959
function C() {
60-
_super.apply(this, arguments);
61-
this.ro = "readonly please";
60+
var _this = _super.apply(this, arguments) || this;
61+
_this.ro = "readonly please";
62+
return _this;
6263
}
6364
Object.defineProperty(C.prototype, "concreteWithNoBody", {
6465
get: function () { },
@@ -77,8 +78,9 @@ var WrongTypeProperty = (function () {
7778
var WrongTypePropertyImpl = (function (_super) {
7879
__extends(WrongTypePropertyImpl, _super);
7980
function WrongTypePropertyImpl() {
80-
_super.apply(this, arguments);
81-
this.num = "nope, wrong";
81+
var _this = _super.apply(this, arguments) || this;
82+
_this.num = "nope, wrong";
83+
return _this;
8284
}
8385
return WrongTypePropertyImpl;
8486
}(WrongTypeProperty));
@@ -90,7 +92,7 @@ var WrongTypeAccessor = (function () {
9092
var WrongTypeAccessorImpl = (function (_super) {
9193
__extends(WrongTypeAccessorImpl, _super);
9294
function WrongTypeAccessorImpl() {
93-
_super.apply(this, arguments);
95+
return _super.apply(this, arguments) || this;
9496
}
9597
Object.defineProperty(WrongTypeAccessorImpl.prototype, "num", {
9698
get: function () { return "nope, wrong"; },
@@ -102,8 +104,9 @@ var WrongTypeAccessorImpl = (function (_super) {
102104
var WrongTypeAccessorImpl2 = (function (_super) {
103105
__extends(WrongTypeAccessorImpl2, _super);
104106
function WrongTypeAccessorImpl2() {
105-
_super.apply(this, arguments);
106-
this.num = "nope, wrong";
107+
var _this = _super.apply(this, arguments) || this;
108+
_this.num = "nope, wrong";
109+
return _this;
107110
}
108111
return WrongTypeAccessorImpl2;
109112
}(WrongTypeAccessor));

tests/baselines/reference/accessOverriddenBaseClassMember1.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ var Point = (function () {
3434
var ColoredPoint = (function (_super) {
3535
__extends(ColoredPoint, _super);
3636
function ColoredPoint(x, y, color) {
37-
_super.call(this, x, y);
38-
this.color = color;
37+
var _this = _super.call(this, x, y) || this;
38+
_this.color = color;
39+
return _this;
3940
}
4041
ColoredPoint.prototype.toString = function () {
4142
return _super.prototype.toString.call(this) + " color=" + this.color;

0 commit comments

Comments
 (0)