-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Should work when a child class has class properties and no constructor, and transpiling spreads on super. (T7309) #4254
Comments
I encountered this when defining a custom Error, so I made the following test case to demonstrate the issue and the workaround with class SomeError extends Error {
name = 'SomeError'
}
throw new SomeError(...['arguments']) Output with class SomeError extends Error {
constructor(...args) {
var _temp;
return _temp = super(...args), this.name = 'SomeError', _temp;
}
}
throw new (Function.prototype.bind.apply(SomeError, [null].concat(['arguments'])))(); Adding Output with class SomeError extends Error {
constructor() {
var _temp;
return _temp = super(...arguments), this.name = 'SomeError', _temp;
}
}
throw new (Function.prototype.bind.apply(SomeError, [null].concat(['arguments'])))(); The Now for the big one. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } 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; }
let SomeError = function (_Error) {
_inherits(SomeError, _Error);
function SomeError() {
var _Object$getPrototypeO;
var _temp, _this, _ret;
_classCallCheck(this, SomeError);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(SomeError)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.name = 'SomeError', _temp), _possibleConstructorReturn(_this, _ret);
}
return SomeError;
}(Error);
function withSpread() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
throw new (Function.prototype.bind.apply(SomeError, [null].concat(args)))();
}
withSpread(1, 2); The ordering was also important when adding the This is still current as of |
Any update for this? |
Any idea about how this could be fixed? I don't think that it is possible. |
Bug information
Options
Input code
Description
When using class properties on a child class without an explicit constructor, a default constructor is created by Babel that includes a spread in the call in
super
. This causes a syntax error in Node 4. Here's the transpiled output:A temporary workaround is to also include
babel-plugin-transform-es2015-classes
or to explicitly provideconstructor() { super() }
in the child class.The text was updated successfully, but these errors were encountered: