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

Fix class inheritance in IE <=10 (T3041) #3527

Merged
merged 4 commits into from Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -8,7 +8,7 @@ var Foo = function (_Bar) {
parentOptions.init = function () {
this;
};
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Foo).call(this, parentOptions));
return babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, parentOptions));
}

return Foo;
Expand Down
45 changes: 31 additions & 14 deletions packages/babel-helper-replace-supers/src/index.js
Expand Up @@ -19,6 +19,33 @@ function isMemberExpressionSuper(node) {
return t.isMemberExpression(node) && t.isSuper(node.object);
}

/**
* Creates an expression which result is the proto of objectRef.
* Uses CLASS.__proto__ first for InternetExplorer <= 10 support
*
* @example <caption>isStatic === true</caption>
*
* CLASS.__proto__ || Object.getPrototypeOf(CLASS)
*
* @example <caption>isStatic === false</caption>
*
* CLASS.prototype.__proto__ || Object.getPrototypeOf(CLASS.prototype)
*/
function getPrototypeOfExpression(objectRef, isStatic) {
const targetRef = isStatic ? objectRef : t.memberExpression(objectRef, t.identifier("prototype"));

return t.logicalExpression(
"||",
t.memberExpression(targetRef, t.identifier("__proto__")),
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[
targetRef
]
),
);
}

let visitor = {
Function(path) {
if (!path.inShadow("this")) {
Expand Down Expand Up @@ -117,20 +144,15 @@ export default class ReplaceSupers {
*
* @example
*
* _set(Object.getPrototypeOf(CLASS.prototype), "METHOD", "VALUE", this)
* _set(CLASS.prototype.__proto__ || Object.getPrototypeOf(CLASS.prototype), "METHOD", "VALUE", this)
*
*/

setSuperProperty(property: Object, value: Object, isComputed: boolean): Object {
return t.callExpression(
this.file.addHelper("set"),
[
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[
this.isStatic ? this.getObjectRef() : t.memberExpression(this.getObjectRef(), t.identifier("prototype"))
]
),
getPrototypeOfExpression(this.getObjectRef(), this.isStatic),
isComputed ? property : t.stringLiteral(property.name),
value,
t.thisExpression()
Expand All @@ -143,20 +165,15 @@ export default class ReplaceSupers {
*
* @example
*
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
* _get(CLASS.prototype.__proto__ || Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
*
*/

getSuperProperty(property: Object, isComputed: boolean): Object {
return t.callExpression(
this.file.addHelper("get"),
[
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[
this.isStatic ? this.getObjectRef() : t.memberExpression(this.getObjectRef(), t.identifier("prototype"))
]
),
getPrototypeOfExpression(this.getObjectRef(), this.isStatic),
isComputed ? property : t.stringLiteral(property.name),
t.thisExpression()
]
Expand Down
Expand Up @@ -5,7 +5,7 @@ var Foo = function (_Bar) {
var _temp, _this, _ret;

babelHelpers.classCallCheck(this, Foo);
return _ret = (_temp = (_this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Foo).call(this, ...args)), _this), _this.bar = "foo", _temp), babelHelpers.possibleConstructorReturn(_this, _ret);
return _ret = (_temp = (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, ...args)), _this), _this.bar = "foo", _temp), babelHelpers.possibleConstructorReturn(_this, _ret);
}

return Foo;
Expand Down
Expand Up @@ -6,7 +6,7 @@ var Child = function (_Parent) {
function Child() {
babelHelpers.classCallCheck(this, Child);

var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Child).call(this));
var _this = babelHelpers.possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this));

_this.scopedFunctionWithThis = function () {
_this.name = {};
Expand Down
Expand Up @@ -6,7 +6,7 @@ var Foo = function (_Bar) {

babelHelpers.classCallCheck(this, Foo);

foo((_temp = (_this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Foo).call(this)), _this), _this.bar = "foo", _temp));
foo((_temp = (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)), _this), _this.bar = "foo", _temp));
return _this;
}

Expand Down
Expand Up @@ -4,7 +4,7 @@ var Foo = function (_Bar) {
function Foo() {
babelHelpers.classCallCheck(this, Foo);

var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Foo).call(this));
var _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));

_this.bar = "foo";
return _this;
Expand Down
Expand Up @@ -6,7 +6,7 @@ function withContext(ComposedComponent) {

function WithContext() {
babelHelpers.classCallCheck(this, WithContext);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(WithContext).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (WithContext.__proto__ || Object.getPrototypeOf(WithContext)).apply(this, arguments));
}

return WithContext;
Expand Down
10 changes: 7 additions & 3 deletions packages/babel-plugin-transform-es2015-classes/src/vanilla.js
Expand Up @@ -345,9 +345,13 @@ export default class ClassTransformer {
}
} else {
bareSuperNode = optimiseCall(
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[this.classRef]
t.logicalExpression(
"||",
t.memberExpression(this.classRef, t.identifier("__proto__")),
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[this.classRef]
)
),
t.thisExpression(),
bareSuperNode.arguments
Expand Down
Expand Up @@ -22,7 +22,7 @@ var Connection = function (_EventEmitter) {
function Connection(endpoint, joinKey, joinData, roomId) {
babelHelpers.classCallCheck(this, Connection);

var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Connection).call(this));
var _this = babelHelpers.possibleConstructorReturn(this, (Connection.__proto__ || Object.getPrototypeOf(Connection)).call(this));

_this.isConnected = false;
_this.roomId = roomId;
Expand Down
Expand Up @@ -13,13 +13,13 @@ var SubFoo = function (_BaseFoo) {

function SubFoo() {
babelHelpers.classCallCheck(this, SubFoo);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(SubFoo).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (SubFoo.__proto__ || Object.getPrototypeOf(SubFoo)).apply(this, arguments));
}

babelHelpers.createClass(SubFoo, null, [{
key: 'talk',
value: function talk() {
babelHelpers.get(Object.getPrototypeOf(SubFoo), 'talk', this).call(this);
babelHelpers.get(SubFoo.__proto__ || Object.getPrototypeOf(SubFoo), 'talk', this).call(this);
console.log('SubFoo.talk');
}
}]);
Expand Down
Expand Up @@ -13,7 +13,7 @@ var RandomComponent = function (_Component) {

function RandomComponent() {
babelHelpers.classCallCheck(this, RandomComponent);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(RandomComponent).call(this));
return babelHelpers.possibleConstructorReturn(this, (RandomComponent.__proto__ || Object.getPrototypeOf(RandomComponent)).call(this));
}

babelHelpers.createClass(RandomComponent, [{
Expand Down
Expand Up @@ -14,7 +14,7 @@ var a1 = function (_b) {
function a1() {
babelHelpers.classCallCheck(this, a1);

var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(a1).call(this));
var _this = babelHelpers.possibleConstructorReturn(this, (a1.__proto__ || Object.getPrototypeOf(a1)).call(this));

_this.x = function () {
return _this;
Expand All @@ -31,7 +31,7 @@ var a2 = function (_b2) {
function a2() {
babelHelpers.classCallCheck(this, a2);

var _this2 = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(a2).call(this));
var _this2 = babelHelpers.possibleConstructorReturn(this, (a2.__proto__ || Object.getPrototypeOf(a2)).call(this));

_this2.x = function () {
return _this2;
Expand Down
Expand Up @@ -6,7 +6,7 @@ var x = {

function _class() {
babelHelpers.classCallCheck(this, _class);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(_class).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
}

return _class;
Expand Down
Expand Up @@ -12,7 +12,7 @@ var B = function (_A) {

babelHelpers.classCallCheck(this, B);

return _ret = (_this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(B).call(this)), _this), babelHelpers.possibleConstructorReturn(_this, _ret);
return _ret = (_this = babelHelpers.possibleConstructorReturn(this, (B.__proto__ || Object.getPrototypeOf(B)).call(this)), _this), babelHelpers.possibleConstructorReturn(_this, _ret);
}

return B;
Expand Down
Expand Up @@ -2,22 +2,22 @@ var Test = function (_Foo) {
babelHelpers.inherits(Test, _Foo);

function Test() {
var _Object$getPrototypeO, _babelHelpers$get;
var _ref, _babelHelpers$get;

babelHelpers.classCallCheck(this, Test);

woops.super.test();

var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Test).call(this));
var _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));

babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", _this).call(_this);
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).call(_this);

var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Test).apply(this, arguments));
var _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).apply(this, arguments));

var _this = babelHelpers.possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Test)).call.apply(_Object$getPrototypeO, [this, "test"].concat(Array.prototype.slice.call(arguments))));
var _this = babelHelpers.possibleConstructorReturn(this, (_ref = Test.__proto__ || Object.getPrototypeOf(Test)).call.apply(_ref, [this, "test"].concat(Array.prototype.slice.call(arguments))));

babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", _this).apply(_this, arguments);
(_babelHelpers$get = babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", _this)).call.apply(_babelHelpers$get, [_this, "test"].concat(Array.prototype.slice.call(arguments)));
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).apply(_this, arguments);
(_babelHelpers$get = babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this)).call.apply(_babelHelpers$get, [_this, "test"].concat(Array.prototype.slice.call(arguments)));
return _this;
}

Expand All @@ -26,18 +26,18 @@ var Test = function (_Foo) {
value: function test() {
var _babelHelpers$get2;

babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", this).call(this);
babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", this).apply(this, arguments);
(_babelHelpers$get2 = babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_babelHelpers$get2, [this, "test"].concat(Array.prototype.slice.call(arguments)));
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", this).call(this);
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", this).apply(this, arguments);
(_babelHelpers$get2 = babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_babelHelpers$get2, [this, "test"].concat(Array.prototype.slice.call(arguments)));
}
}], [{
key: "foo",
value: function foo() {
var _babelHelpers$get3;

babelHelpers.get(Object.getPrototypeOf(Test), "foo", this).call(this);
babelHelpers.get(Object.getPrototypeOf(Test), "foo", this).apply(this, arguments);
(_babelHelpers$get3 = babelHelpers.get(Object.getPrototypeOf(Test), "foo", this)).call.apply(_babelHelpers$get3, [this, "test"].concat(Array.prototype.slice.call(arguments)));
babelHelpers.get(Test.__proto__ || Object.getPrototypeOf(Test), "foo", this).call(this);
babelHelpers.get(Test.__proto__ || Object.getPrototypeOf(Test), "foo", this).apply(this, arguments);
(_babelHelpers$get3 = babelHelpers.get(Test.__proto__ || Object.getPrototypeOf(Test), "foo", this)).call.apply(_babelHelpers$get3, [this, "test"].concat(Array.prototype.slice.call(arguments)));
}
}]);
return Test;
Expand Down
Expand Up @@ -4,10 +4,10 @@ var Test = function (_Foo) {
function Test() {
babelHelpers.classCallCheck(this, Test);

var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Test).call(this));
var _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));

babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", _this);
babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", _this).whatever;
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this);
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).whatever;
return _this;
}

Expand Down
Expand Up @@ -4,17 +4,17 @@ var Test = function (_Foo) {
function Test() {
babelHelpers.classCallCheck(this, Test);

var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Test).call(this));
var _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));

babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", _this).whatever();
babelHelpers.get(Object.getPrototypeOf(Test.prototype), "test", _this).call(_this);
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).whatever();
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).call(_this);
return _this;
}

babelHelpers.createClass(Test, null, [{
key: "test",
value: function test() {
return babelHelpers.get(Object.getPrototypeOf(Test), "wow", this).call(this);
return babelHelpers.get(Test.__proto__ || Object.getPrototypeOf(Test), "wow", this).call(this);
}
}]);
return Test;
Expand Down
Expand Up @@ -10,7 +10,7 @@ var Foo = function (_Bar) {
function Foo() {
babelHelpers.classCallCheck(this, Foo);

var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Foo).call(this));
var _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));

_this.state = "test";
return _this;
Expand Down
Expand Up @@ -5,7 +5,7 @@ var Foo = function (_Bar) {
var _this;

babelHelpers.classCallCheck(this, Foo);
return _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Foo).call(this, () => {
return _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, () => {
_this.test;
}));
}
Expand Down
Expand Up @@ -3,7 +3,7 @@ var _class = function (_A) {

function _class() {
babelHelpers.classCallCheck(this, _class);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(_class).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
}

return _class;
Expand Down

This file was deleted.

Expand Up @@ -3,7 +3,7 @@ var TestEmpty = function (_ref) {

function TestEmpty() {
babelHelpers.classCallCheck(this, TestEmpty);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(TestEmpty).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (TestEmpty.__proto__ || Object.getPrototypeOf(TestEmpty)).apply(this, arguments));
}

return TestEmpty;
Expand All @@ -20,7 +20,7 @@ var TestConstructorOnly = function (_ref2) {

function TestConstructorOnly() {
babelHelpers.classCallCheck(this, TestConstructorOnly);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(TestConstructorOnly).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (TestConstructorOnly.__proto__ || Object.getPrototypeOf(TestConstructorOnly)).apply(this, arguments));
}

return TestConstructorOnly;
Expand All @@ -37,7 +37,7 @@ var TestMethodOnly = function (_ref3) {

function TestMethodOnly() {
babelHelpers.classCallCheck(this, TestMethodOnly);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(TestMethodOnly).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (TestMethodOnly.__proto__ || Object.getPrototypeOf(TestMethodOnly)).apply(this, arguments));
}

return TestMethodOnly;
Expand All @@ -58,7 +58,7 @@ var TestConstructorAndMethod = function (_ref4) {

function TestConstructorAndMethod() {
babelHelpers.classCallCheck(this, TestConstructorAndMethod);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(TestConstructorAndMethod).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (TestConstructorAndMethod.__proto__ || Object.getPrototypeOf(TestConstructorAndMethod)).apply(this, arguments));
}

return TestConstructorAndMethod;
Expand All @@ -79,7 +79,7 @@ var TestMultipleMethods = function (_ref5) {

function TestMultipleMethods() {
babelHelpers.classCallCheck(this, TestMultipleMethods);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(TestMultipleMethods).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (TestMultipleMethods.__proto__ || Object.getPrototypeOf(TestMultipleMethods)).apply(this, arguments));
}

return TestMultipleMethods;
Expand Down