this before super() is a runtime error, not a static one. (#6467)

* Check that super.* is after super() at runtime

* "missing super() call in constructor" runtime

* "'this' is not allowed before super()" runtime
This commit is contained in:
Nicolò Ribaudo
2017-12-06 06:46:54 +01:00
committed by GitHub
parent e270fbe7f0
commit 509dbb7302
66 changed files with 536 additions and 84 deletions

View File

@@ -1,3 +1,5 @@
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
let B = function B() {};
@@ -11,7 +13,7 @@ function (_B) {
var _this;
if (track !== undefined) _this = _B.call(this, track) || this;else _this = _B.call(this) || this;
return _this;
return _assertThisInitialized(_this);
}
return A;

View File

@@ -0,0 +1,9 @@
class Bar {}
class Foo extends Bar {
constructor() {
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,13 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inheritsLoose(Foo, _Bar);
function Foo() {
var _this;
return babelHelpers.assertThisInitialized(_this);
}
return Foo;
}(Bar);

View File

@@ -1,4 +0,0 @@
{
"throws": "missing super() call in constructor",
"plugins": ["transform-classes"]
}

View File

@@ -6,7 +6,7 @@ function (_Base) {
function Child() {
var _this;
return false || _this;
return false || babelHelpers.assertThisInitialized(_this);
}
return Child;

View File

@@ -6,7 +6,7 @@ function (_Base) {
function Child() {
var _this;
return {} || _this;
return {} || babelHelpers.assertThisInitialized(_this);
}
return Child;

View File

@@ -21,7 +21,7 @@ function (_b) {
_this = babelHelpers.possibleConstructorReturn(this, (a1.__proto__ || Object.getPrototypeOf(a1)).call(this));
_this.x = function () {
return _this;
return babelHelpers.assertThisInitialized(_this);
};
return _this;
@@ -42,7 +42,7 @@ function (_b2) {
_this2 = babelHelpers.possibleConstructorReturn(this, (a2.__proto__ || Object.getPrototypeOf(a2)).call(this));
_this2.x = function () {
return _this2;
return babelHelpers.assertThisInitialized(_this2);
};
return _this2;

View File

@@ -1,6 +1,6 @@
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

View File

@@ -11,12 +11,12 @@ function (_Foo) {
babelHelpers.classCallCheck(this, Test);
woops.super.test();
_this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).call(_this);
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", babelHelpers.assertThisInitialized(_this)).call(_this);
_this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).apply(this, arguments));
_this = babelHelpers.possibleConstructorReturn(this, (_ref = Test.__proto__ || Object.getPrototypeOf(Test)).call.apply(_ref, [this, "test"].concat(Array.prototype.slice.call(arguments))));
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).apply(_this, arguments);
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", babelHelpers.assertThisInitialized(_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)));
(_babelHelpers$get = babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", babelHelpers.assertThisInitialized(_this))).call.apply(_babelHelpers$get, [_this, "test"].concat(Array.prototype.slice.call(arguments)));
return _this;
}

View File

@@ -8,8 +8,8 @@ function (_Foo) {
babelHelpers.classCallCheck(this, Test);
_this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this);
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).whatever;
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", babelHelpers.assertThisInitialized(_this));
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", babelHelpers.assertThisInitialized(_this)).whatever;
return _this;
}

View File

@@ -8,8 +8,8 @@ function (_Foo) {
babelHelpers.classCallCheck(this, Test);
_this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).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);
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", babelHelpers.assertThisInitialized(_this)).whatever();
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", babelHelpers.assertThisInitialized(_this)).call(_this);
return _this;
}

View File

@@ -0,0 +1,5 @@
class Foo extends Bar {
constructor() {
if (eval("false")) super();
}
}

View File

@@ -0,0 +1,9 @@
class Bar {}
class Foo extends Bar {
constructor() {
if (eval("false")) super();
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,15 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
if (eval("false")) _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
return babelHelpers.possibleConstructorReturn(_this);
}
return Foo;
}(Bar);

View File

@@ -0,0 +1,6 @@
class Foo extends Bar {
constructor() {
const fn = () => super();
fn();
}
}

View File

@@ -0,0 +1,10 @@
class Bar {}
class Foo extends Bar {
constructor() {
const fn = () => super();
fn();
}
}
new Foo();

View File

@@ -0,0 +1,18 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
var fn = () => _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
fn();
return babelHelpers.possibleConstructorReturn(_this);
}
return Foo;
}(Bar);

View File

@@ -0,0 +1,5 @@
class Foo extends Bar {
constructor() {
const fn = () => super();
}
}

View File

@@ -0,0 +1,9 @@
class Bar {}
class Foo extends Bar {
constructor() {
const fn = () => super();
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,17 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
var fn = () => _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
return babelHelpers.possibleConstructorReturn(_this);
}
return Foo;
}(Bar);

View File

@@ -0,0 +1,9 @@
class Bar {}
class Foo extends Bar {
constructor() {
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,14 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
return babelHelpers.possibleConstructorReturn(_this);
}
return Foo;
}(Bar);

View File

@@ -1,4 +0,0 @@
{
"throws": "missing super() call in constructor",
"plugins": ["transform-classes"]
}

View File

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

View File

@@ -0,0 +1,9 @@
class Bar {}
class Foo extends Bar {
constructor() {
super.foo(super());
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,27 @@
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return _get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
var Foo =
/*#__PURE__*/
function (_Bar) {
_inherits(Foo, _Bar);
function Foo() {
var _this;
_classCallCheck(this, Foo);
_get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), "foo", _assertThisInitialized(_this)).call(_this, _this = _possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)));
return _this;
}
return Foo;
}(Bar);

View File

@@ -1,4 +1,3 @@
{
"throws": "'super.*' is not allowed before super()",
"plugins": ["transform-classes"]
"plugins": ["transform-classes", "transform-block-scoping"]
}

View File

@@ -0,0 +1,10 @@
class Bar {}
class Foo extends Bar {
constructor() {
super.foo();
super();
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,27 @@
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return _get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
var Foo =
/*#__PURE__*/
function (_Bar) {
_inherits(Foo, _Bar);
function Foo() {
var _this;
_classCallCheck(this, Foo);
_get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), "foo", _assertThisInitialized(_this)).call(_this);
return _this = _possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
}
return Foo;
}(Bar);

View File

@@ -1,4 +1,3 @@
{
"throws": "'super.*' is not allowed before super()",
"plugins": ["transform-classes"]
"plugins": ["transform-classes", "transform-block-scoping"]
}

View File

@@ -1,6 +1,6 @@
class Foo extends Bar {
constructor() {
const t = () => super.test()
var t = () => super.test()
super.foo();
super();
}

View File

@@ -0,0 +1,13 @@
class Bar {
test() {}
}
class Foo extends Bar {
constructor() {
const t = () => super.test()
t();
super();
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,18 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
var t = () => babelHelpers.get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), "test", babelHelpers.assertThisInitialized(_this)).call(_this);
babelHelpers.get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), "foo", babelHelpers.assertThisInitialized(_this)).call(_this);
return _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
}
return Foo;
}(Bar);

View File

@@ -1,4 +1,3 @@
{
"throws": "'super.*' is not allowed before super()",
"plugins": ["external-helpers", "transform-classes"]
"plugins": ["external-helpers", "transform-classes", "transform-block-scoping"]
}

View File

@@ -0,0 +1,7 @@
class Foo extends Bar {
constructor() {
const t = () => super.test()
super();
t();
}
}

View File

@@ -0,0 +1,13 @@
class Bar {
test() {}
}
class Foo extends Bar {
constructor() {
const t = () => super.test()
super();
t();
}
}
new Foo();

View File

@@ -0,0 +1,19 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
var t = () => babelHelpers.get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), "test", babelHelpers.assertThisInitialized(_this)).call(_this);
_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
t();
return _this;
}
return Foo;
}(Bar);

View File

@@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-classes", "transform-block-scoping"]
}

View File

@@ -0,0 +1,12 @@
class Bar {
test() {}
}
class Foo extends Bar {
constructor() {
const t = () => super.test()
super();
}
}
new Foo();

View File

@@ -8,7 +8,7 @@ function (_Bar) {
babelHelpers.classCallCheck(this, Foo);
var t = () => babelHelpers.get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), "test", _this).call(_this);
var t = () => babelHelpers.get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), "test", babelHelpers.assertThisInitialized(_this)).call(_this);
return _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
}

View File

@@ -7,7 +7,7 @@ function (_Bar) {
var _this;
babelHelpers.classCallCheck(this, Foo);
babelHelpers.get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this))).method, _this).call(_this);
babelHelpers.get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this))).method, babelHelpers.assertThisInitialized(_this)).call(_this);
return _this;
}

View File

@@ -0,0 +1,9 @@
class Bar {}
class Foo extends Bar {
constructor() {
super(this);
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,14 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
return _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, babelHelpers.assertThisInitialized(_this)));
}
return Foo;
}(Bar);

View File

@@ -1,4 +0,0 @@
{
"throws": "'this' is not allowed before super()",
"plugins": ["transform-classes"]
}

View File

@@ -0,0 +1,7 @@
class Foo extends Bar {
constructor() {
const fn = () => this;
fn();
super();
}
}

View File

@@ -0,0 +1,11 @@
class Bar {}
class Foo extends Bar {
constructor() {
const fn = () => this;
fn();
super();
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,18 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
var fn = () => babelHelpers.assertThisInitialized(_this);
fn();
return _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
}
return Foo;
}(Bar);

View File

@@ -0,0 +1,7 @@
class Foo extends Bar {
constructor() {
const fn = () => this;
super();
fn();
}
}

View File

@@ -0,0 +1,11 @@
class Bar {}
class Foo extends Bar {
constructor() {
const fn = () => this;
super();
fn();
}
}
new Foo();

View File

@@ -0,0 +1,19 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
var fn = () => babelHelpers.assertThisInitialized(_this);
_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
fn();
return _this;
}
return Foo;
}(Bar);

View File

@@ -0,0 +1,5 @@
class Foo extends Bar {
constructor() {
Foo[this];
}
}

View File

@@ -0,0 +1,9 @@
class Bar {}
class Foo extends Bar {
constructor() {
Foo[this];
}
}
assert.throws(() => new Foo(), /this hasn't been initialised/);

View File

@@ -0,0 +1,15 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
Foo[babelHelpers.assertThisInitialized(_this)];
return babelHelpers.possibleConstructorReturn(_this);
}
return Foo;
}(Bar);

View File

@@ -0,0 +1,10 @@
class Bar {}
class Foo extends Bar {
constructor() {
this.foo = "bar";
super();
}
}
assert.throws(() => new Foo());

View File

@@ -0,0 +1,15 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
_this.foo = "bar";
return _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
}
return Foo;
}(Bar);

View File

@@ -1,4 +0,0 @@
{
"throws": "'this' is not allowed before super()",
"plugins": ["transform-classes"]
}