Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use returned values from super calls as 'this' #10762

Merged
merged 44 commits into from
Sep 30, 2016
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
9dbab97
Emit '_this' declaration for derived classes; initialize it when call…
DanielRosenwasser Sep 2, 2016
99f1df4
Accepted baselines.
DanielRosenwasser Sep 2, 2016
d778e78
Transform other instances of 'this' to '_this' when in the constructo…
DanielRosenwasser Sep 2, 2016
c21cc24
Accepted baselines for all tests apart from one with source map changes.
DanielRosenwasser Sep 2, 2016
9c6e148
Accepted baselines for sourcemap test.
DanielRosenwasser Sep 2, 2016
cd5e76b
Always tack on a return statement for '_this' in derived classes.
DanielRosenwasser Sep 2, 2016
30ec599
Accepted baselines.
DanielRosenwasser Sep 2, 2016
bab6d6f
Ensure prologue directives occur first.
DanielRosenwasser Sep 2, 2016
ce953ce
Accepted baselines.
DanielRosenwasser Sep 2, 2016
d6e548b
Consolidate '_this' declaration and assignments for first statements …
DanielRosenwasser Sep 3, 2016
11bc6c4
Fixed issue where last function context & parent node wasn't being pr…
DanielRosenwasser Sep 3, 2016
d8846dd
Accepted baselines.
DanielRosenwasser Sep 3, 2016
86088d0
Always perform `this` captures after default & rest parameters.
DanielRosenwasser Sep 3, 2016
dd27139
Accepted baselines.
DanielRosenwasser Sep 3, 2016
f3dcaae
Added a test for branched returns at the end of a constructor.
DanielRosenwasser Sep 3, 2016
1be4cee
Accepted baselines.
DanielRosenwasser Sep 3, 2016
c87a773
Don't emit a return statement at the end in most useful cases.
DanielRosenwasser Sep 3, 2016
d144665
Accepted baselines.
DanielRosenwasser Sep 3, 2016
b476815
Added test for '_this'.
DanielRosenwasser Sep 6, 2016
230737f
Accepted baselines.
DanielRosenwasser Sep 6, 2016
b11db57
Mark constructors as this-capturing if they are defined in a derived …
DanielRosenwasser Sep 7, 2016
25f9555
Merge branch 'master' into useReturnedThisFromSuperCalls
DanielRosenwasser Sep 7, 2016
3a5fb0c
Accepted baselines.
DanielRosenwasser Sep 8, 2016
f11c646
Added missing semicolons.
DanielRosenwasser Sep 8, 2016
a63a0d8
Merge remote-tracking branch 'origin/master' into useReturnedThisFrom…
DanielRosenwasser Sep 19, 2016
138e18c
Accepted baselines.
DanielRosenwasser Sep 19, 2016
da29813
Added test and comment.
DanielRosenwasser Sep 19, 2016
60bcd7e
Accepted baselines.
DanielRosenwasser Sep 19, 2016
5b3a93d
Always mark derived constructors as 'this'-capturing.
DanielRosenwasser Sep 19, 2016
bc4cf6d
Accepted baselines.
DanielRosenwasser Sep 19, 2016
dc58fb5
Immediately return the result of super calls when they are the first …
DanielRosenwasser Sep 20, 2016
cd787cc
Accepted baselines.
DanielRosenwasser Sep 20, 2016
6580262
Initialize instead of letting the value be potentially undefined.
DanielRosenwasser Sep 20, 2016
3e8fb37
Merge remote-tracking branch 'origin/master' into useReturnedThisFrom…
DanielRosenwasser Sep 20, 2016
5fadfd4
Merge remote-tracking branch 'origin/master' into useReturnedThisFrom…
DanielRosenwasser Sep 27, 2016
4a5830d
Remove usage of 'useCapturedThis'.
DanielRosenwasser Sep 27, 2016
c668644
Collapse 'super()' capture/returns into a single return statement for…
DanielRosenwasser Sep 27, 2016
1fbdb86
Accepted baselines.
DanielRosenwasser Sep 27, 2016
e0c35f2
Restore arrow function.
DanielRosenwasser Sep 27, 2016
f8fbc22
Reuse the 'captureThisForNode' function.
DanielRosenwasser Sep 27, 2016
fb4b503
Removed pointlessish overloads.
DanielRosenwasser Sep 27, 2016
b5a1031
Moved code around, fixed indent, reworded comment.
DanielRosenwasser Sep 27, 2016
c29ec6f
Consolidated 'super()' transformation logic into one function.
DanielRosenwasser Sep 28, 2016
02b9917
Merge remote-tracking branch 'origin/master' into useReturnedThisFrom…
DanielRosenwasser Sep 30, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9330,8 +9330,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 @@ -14323,6 +14323,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
269 changes: 233 additions & 36 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,8 @@ var A;
var Point3d = (function (_super) {
__extends(Point3d, _super);
function Point3d() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
Copy link
Member

@rbuckton rbuckton Sep 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more succinct to write this as:

  return _super.apply(this, arguments) || this;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

return _this;
}
return Point3d;
}(Point));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var A;
var Point3d = (function (_super) {
__extends(Point3d, _super);
function Point3d() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return Point3d;
}(Point));
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/abstractClassInLocalScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var __extends = (this && this.__extends) || function (d, b) {
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return B;
}(A));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var __extends = (this && this.__extends) || function (d, b) {
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _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
18 changes: 11 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,8 @@ var WrongTypeAccessor = (function () {
var WrongTypeAccessorImpl = (function (_super) {
__extends(WrongTypeAccessorImpl, _super);
function WrongTypeAccessorImpl() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
Object.defineProperty(WrongTypeAccessorImpl.prototype, "num", {
get: function () { return "nope, wrong"; },
Expand All @@ -102,8 +105,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,8 @@ var A = (function () {
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return B;
}(A));
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/aliasUsageInAccessorsOfClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ var Backbone = require("./aliasUsage1_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/aliasUsageInArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var Backbone = require("./aliasUsageInArray_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/aliasUsageInFunctionExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ var Backbone = require("./aliasUsageInFunctionExpression_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/aliasUsageInGenericFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var Backbone = require("./aliasUsageInGenericFunction_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/aliasUsageInIndexerOfClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ var Backbone = require("./aliasUsageInIndexerOfClass_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/aliasUsageInObjectLiteral.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var Backbone = require("./aliasUsageInObjectLiteral_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/aliasUsageInOrExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var Backbone = require("./aliasUsageInOrExpression_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var Backbone = require("./aliasUsageInTypeArgumentOfExtendsClause_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return VisualizationModel;
}(Backbone.Model));
Expand All @@ -64,8 +65,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));
3 changes: 2 additions & 1 deletion tests/baselines/reference/aliasUsageInVarAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ var Backbone = require("./aliasUsageInVarAssignment_backbone");
var VisualizationModel = (function (_super) {
__extends(VisualizationModel, _super);
function VisualizationModel() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return VisualizationModel;
}(Backbone.Model));
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/ambiguousOverloadResolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var A = (function () {
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return B;
}(A));
Expand Down
6 changes: 4 additions & 2 deletions tests/baselines/reference/apparentTypeSubtyping.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ var Base = (function () {
var Derived = (function (_super) {
__extends(Derived, _super);
function Derived() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return Derived;
}(Base));
Expand All @@ -51,7 +52,8 @@ var Base2 = (function () {
var Derived2 = (function (_super) {
__extends(Derived2, _super);
function Derived2() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return Derived2;
}(Base2));
3 changes: 2 additions & 1 deletion tests/baselines/reference/apparentTypeSupertype.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var Base = (function () {
var Derived = (function (_super) {
__extends(Derived, _super);
function Derived() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return Derived;
}(Base));
3 changes: 2 additions & 1 deletion tests/baselines/reference/arrayAssignmentTest1.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ var C1 = (function () {
var C2 = (function (_super) {
__extends(C2, _super);
function C2() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
C2.prototype.C2M1 = function () { return null; };
return C2;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/arrayAssignmentTest2.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ var C1 = (function () {
var C2 = (function (_super) {
__extends(C2, _super);
function C2() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
C2.prototype.C2M1 = function () { return null; };
return C2;
Expand Down
6 changes: 4 additions & 2 deletions tests/baselines/reference/arrayBestCommonTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ var EmptyTypes;
var derived = (function (_super) {
__extends(derived, _super);
function derived() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return derived;
}(base));
Expand Down Expand Up @@ -187,7 +188,8 @@ var NonEmptyTypes;
var derived = (function (_super) {
__extends(derived, _super);
function derived() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return derived;
}(base));
Expand Down
6 changes: 4 additions & 2 deletions tests/baselines/reference/arrayLiteralTypeInference.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ var Action = (function () {
var ActionA = (function (_super) {
__extends(ActionA, _super);
function ActionA() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return ActionA;
}(Action));
var ActionB = (function (_super) {
__extends(ActionB, _super);
function ActionB() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return ActionB;
}(Action));
Expand Down
6 changes: 4 additions & 2 deletions tests/baselines/reference/arrayLiterals.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ var Base = (function () {
var Derived1 = (function (_super) {
__extends(Derived1, _super);
function Derived1() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return Derived1;
}(Base));
;
var Derived2 = (function (_super) {
__extends(Derived2, _super);
function Derived2() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return Derived2;
}(Base));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ var List = (function () {
var DerivedList = (function (_super) {
__extends(DerivedList, _super);
function DerivedList() {
_super.apply(this, arguments);
var _this = _super.apply(this, arguments) || this;
return _this;
}
return DerivedList;
}(List));
Expand Down
Loading