Skip to content

Commit

Permalink
Merge pull request #10762 from Microsoft/useReturnedThisFromSuperCalls
Browse files Browse the repository at this point in the history
Use returned values from super calls as 'this'
  • Loading branch information
DanielRosenwasser authored Sep 30, 2016
2 parents edd8eb8 + 02b9917 commit bcdf1dc
Show file tree
Hide file tree
Showing 680 changed files with 3,489 additions and 1,959 deletions.
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9421,8 +9421,8 @@ namespace ts {
let container = getSuperContainer(node, /*stopOnFunctions*/ true);
let needToCaptureLexicalThis = false;

// adjust the container reference in case if super is used inside arrow functions with arbitrarily deep nesting
if (!isCallExpression) {
// adjust the container reference in case if super is used inside arrow functions with arbitrary deep nesting
while (container && container.kind === SyntaxKind.ArrowFunction) {
container = getSuperContainer(container, /*stopOnFunctions*/ true);
needToCaptureLexicalThis = languageVersion < ScriptTarget.ES6;
Expand Down Expand Up @@ -14439,6 +14439,7 @@ namespace ts {
// constructors of derived classes must contain at least one super call somewhere in their function body.
const containingClassDecl = <ClassDeclaration>node.parent;
if (getClassExtendsHeritageClauseElement(containingClassDecl)) {
captureLexicalThis(node.parent, containingClassDecl);
const classExtendsNull = classDeclarationExtendsNull(containingClassDecl);
const superCall = getSuperCallInConstructor(node);
if (superCall) {
Expand Down
288 changes: 234 additions & 54 deletions src/compiler/transformers/es6.ts

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,11 +982,11 @@ namespace ts {
}

/**
* Given an super call\property node returns a closest node where either
* - super call\property is legal in the node and not legal in the parent node the node.
* Given an super call/property node, returns the closest node where
* - a super call/property access is legal in the node and not legal in the parent node the node.
* i.e. super call is legal in constructor but not legal in the class body.
* - node is arrow function (so caller might need to call getSuperContainer in case it needs to climb higher)
* - super call\property is definitely illegal in the node (but might be legal in some subnode)
* - the container is an arrow function (so caller might need to call getSuperContainer again in case it needs to climb higher)
* - a super call/property is definitely illegal in the container (but might be legal in some subnode)
* i.e. super property access is illegal in function declaration but can be legal in the statement list
*/
export function getSuperContainer(node: Node, stopOnFunctions: boolean): Node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var A;
var Point3d = (function (_super) {
__extends(Point3d, _super);
function Point3d() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return Point3d;
}(Point));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var A;
var Point3d = (function (_super) {
__extends(Point3d, _super);
function Point3d() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return Point3d;
}(Point));
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/abstractClassInLocalScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var __extends = (this && this.__extends) || function (d, b) {
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return B;
}(A));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var __extends = (this && this.__extends) || function (d, b) {
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return B;
}(A));
Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/abstractProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ var B = (function () {
var C = (function (_super) {
__extends(C, _super);
function C() {
_super.apply(this, arguments);
this.raw = "edge";
this.ro = "readonly please";
var _this = _super.apply(this, arguments) || this;
_this.raw = "edge";
_this.ro = "readonly please";
return _this;
}
Object.defineProperty(C.prototype, "prop", {
get: function () { return "foo"; },
Expand Down
17 changes: 10 additions & 7 deletions tests/baselines/reference/abstractPropertyNegative.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ var B = (function () {
var C = (function (_super) {
__extends(C, _super);
function C() {
_super.apply(this, arguments);
this.ro = "readonly please";
var _this = _super.apply(this, arguments) || this;
_this.ro = "readonly please";
return _this;
}
Object.defineProperty(C.prototype, "concreteWithNoBody", {
get: function () { },
Expand All @@ -77,8 +78,9 @@ var WrongTypeProperty = (function () {
var WrongTypePropertyImpl = (function (_super) {
__extends(WrongTypePropertyImpl, _super);
function WrongTypePropertyImpl() {
_super.apply(this, arguments);
this.num = "nope, wrong";
var _this = _super.apply(this, arguments) || this;
_this.num = "nope, wrong";
return _this;
}
return WrongTypePropertyImpl;
}(WrongTypeProperty));
Expand All @@ -90,7 +92,7 @@ var WrongTypeAccessor = (function () {
var WrongTypeAccessorImpl = (function (_super) {
__extends(WrongTypeAccessorImpl, _super);
function WrongTypeAccessorImpl() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
Object.defineProperty(WrongTypeAccessorImpl.prototype, "num", {
get: function () { return "nope, wrong"; },
Expand All @@ -102,8 +104,9 @@ var WrongTypeAccessorImpl = (function (_super) {
var WrongTypeAccessorImpl2 = (function (_super) {
__extends(WrongTypeAccessorImpl2, _super);
function WrongTypeAccessorImpl2() {
_super.apply(this, arguments);
this.num = "nope, wrong";
var _this = _super.apply(this, arguments) || this;
_this.num = "nope, wrong";
return _this;
}
return WrongTypeAccessorImpl2;
}(WrongTypeAccessor));
Expand Down
5 changes: 3 additions & 2 deletions tests/baselines/reference/accessOverriddenBaseClassMember1.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ var Point = (function () {
var ColoredPoint = (function (_super) {
__extends(ColoredPoint, _super);
function ColoredPoint(x, y, color) {
_super.call(this, x, y);
this.color = color;
var _this = _super.call(this, x, y) || this;
_this.color = color;
return _this;
}
ColoredPoint.prototype.toString = function () {
return _super.prototype.toString.call(this) + " color=" + this.color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var A = (function () {
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return B;
}(A));
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasUsageInAccessorsOfClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var Backbone = require("./aliasUsage1_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasUsageInArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var Backbone = require("./aliasUsageInArray_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var Backbone = require("./aliasUsageInFunctionExpression_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasUsageInGenericFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var Backbone = require("./aliasUsageInGenericFunction_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasUsageInIndexerOfClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var Backbone = require("./aliasUsageInIndexerOfClass_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasUsageInObjectLiteral.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var Backbone = require("./aliasUsageInObjectLiteral_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasUsageInOrExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var Backbone = require("./aliasUsageInOrExpression_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var Backbone = require("./aliasUsageInTypeArgumentOfExtendsClause_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return VisualizationModel;
}(Backbone.Model));
Expand All @@ -64,8 +64,9 @@ var C = (function () {
var D = (function (_super) {
__extends(D, _super);
function D() {
_super.apply(this, arguments);
this.x = moduleA;
var _this = _super.apply(this, arguments) || this;
_this.x = moduleA;
return _this;
}
return D;
}(C));
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasUsageInVarAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var Backbone = require("./aliasUsageInVarAssignment_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/ambiguousOverloadResolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var A = (function () {
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return B;
}(A));
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/apparentTypeSubtyping.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var Base = (function () {
var Derived = (function (_super) {
__extends(Derived, _super);
function Derived() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return Derived;
}(Base));
Expand All @@ -51,7 +51,7 @@ var Base2 = (function () {
var Derived2 = (function (_super) {
__extends(Derived2, _super);
function Derived2() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return Derived2;
}(Base2));
2 changes: 1 addition & 1 deletion tests/baselines/reference/apparentTypeSupertype.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var Base = (function () {
var Derived = (function (_super) {
__extends(Derived, _super);
function Derived() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return Derived;
}(Base));
2 changes: 1 addition & 1 deletion tests/baselines/reference/arrayAssignmentTest1.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var C1 = (function () {
var C2 = (function (_super) {
__extends(C2, _super);
function C2() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
C2.prototype.C2M1 = function () { return null; };
return C2;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/arrayAssignmentTest2.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var C1 = (function () {
var C2 = (function (_super) {
__extends(C2, _super);
function C2() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
C2.prototype.C2M1 = function () { return null; };
return C2;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayBestCommonTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ var EmptyTypes;
var derived = (function (_super) {
__extends(derived, _super);
function derived() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return derived;
}(base));
Expand Down Expand Up @@ -187,7 +187,7 @@ var NonEmptyTypes;
var derived = (function (_super) {
__extends(derived, _super);
function derived() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return derived;
}(base));
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayLiteralTypeInference.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ var Action = (function () {
var ActionA = (function (_super) {
__extends(ActionA, _super);
function ActionA() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return ActionA;
}(Action));
var ActionB = (function (_super) {
__extends(ActionB, _super);
function ActionB() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return ActionB;
}(Action));
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayLiterals.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ var Base = (function () {
var Derived1 = (function (_super) {
__extends(Derived1, _super);
function Derived1() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return Derived1;
}(Base));
;
var Derived2 = (function (_super) {
__extends(Derived2, _super);
function Derived2() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return Derived2;
}(Base));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var List = (function () {
var DerivedList = (function (_super) {
__extends(DerivedList, _super);
function DerivedList() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return DerivedList;
}(List));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ var A = (function () {
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return B;
}(A));
var C = (function (_super) {
__extends(C, _super);
function C() {
_super.apply(this, arguments);
return _super.apply(this, arguments) || this;
}
return C;
}(Array));
Expand Down
6 changes: 2 additions & 4 deletions tests/baselines/reference/arrowFunctionContexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ var Base = (function () {
var Derived = (function (_super) {
__extends(Derived, _super);
function Derived() {
var _this = this;
_super.call(this, function () { return _this; });
return _super.call(this, function () { return _this; }) || this;
}
return Derived;
}(Base));
Expand Down Expand Up @@ -158,8 +157,7 @@ var M2;
var Derived = (function (_super) {
__extends(Derived, _super);
function Derived() {
var _this = this;
_super.call(this, function () { return _this; });
return _super.call(this, function () { return _this; }) || this;
}
return Derived;
}(Base));
Expand Down
Loading

0 comments on commit bcdf1dc

Please sign in to comment.