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

* Fix class inheritance in IE9 & IE10 (T3041)

Internet Explorer 9&10 do not support __proto__ at all, don't have
Object.setPrototypeOf(), but have Object.getPrototypeOf().

Because of this setting the prototype is not possible, which makes the
babelHelpers.inherits() function to set __proto__ although not supported.

Afterwards Object.getPrototypeOf() is used, but this one is not
respecting the "custom" property __proto__ that we set.

The solution is to check for __proto__ first and afterwards fallback to
Object.getPrototypeOf().

* Do the same logic in babel-helper-replace-supers

* Fix tests

* Extract creation of prototype nodes to small helper
This commit is contained in:
Daniel Tschinder
2016-08-23 21:08:44 +02:00
committed by Henry Zhu
parent 2a4186faf3
commit 10cd6519d8
28 changed files with 87 additions and 71 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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');
}
}]);

View File

@@ -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, [{

View File

@@ -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;
@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
}
@@ -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;

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
}));
}

View File

@@ -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;

View File

@@ -1,5 +0,0 @@
var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo);
Object.getPrototypeOf(Foo).call(this);
};

View File

@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;

View File

@@ -3,7 +3,7 @@ var BaseController = function (_Chaplin$Controller) {
function BaseController() {
babelHelpers.classCallCheck(this, BaseController);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(BaseController).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (BaseController.__proto__ || Object.getPrototypeOf(BaseController)).apply(this, arguments));
}
return BaseController;
@@ -14,7 +14,7 @@ var BaseController2 = function (_Chaplin$Controller$A) {
function BaseController2() {
babelHelpers.classCallCheck(this, BaseController2);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(BaseController2).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (BaseController2.__proto__ || Object.getPrototypeOf(BaseController2)).apply(this, arguments));
}
return BaseController2;

View File

@@ -3,7 +3,7 @@ var Test = function (_Foo) {
function Test() {
babelHelpers.classCallCheck(this, Test);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Test).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).apply(this, arguments));
}
return Test;

View File

@@ -1,5 +1,5 @@
var Test = function Test() {
babelHelpers.classCallCheck(this, Test);
babelHelpers.get(Object.getPrototypeOf(Test.prototype), "hasOwnProperty", this).call(this, "test");
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "hasOwnProperty", this).call(this, "test");
};