diff --git a/NOTES.md b/NOTES.md index f86eb840f1..731ad31e9a 100644 --- a/NOTES.md +++ b/NOTES.md @@ -1,7 +1,3 @@ # Notes * Wildcard exports/imports wont normalise if `export default` is a non-object. See [#224](https://github.com/6to5/6to5/issues/224). - -## 3.0.0 breaking changes - - * Remove `super()` inside non-constructors - add descriptive error message. diff --git a/lib/6to5/transformation/helpers/replace-supers.js b/lib/6to5/transformation/helpers/replace-supers.js index fe6845b905..56c3c9fdf7 100644 --- a/lib/6to5/transformation/helpers/replace-supers.js +++ b/lib/6to5/transformation/helpers/replace-supers.js @@ -202,6 +202,14 @@ ReplaceSupers.prototype.specHandle = function (getThisReference, node, parent) { throw this.file.errorWithNode(node, "illegal use of bare super"); } } else if (t.isCallExpression(node)) { + // super(); is illegal inside non-constructors + // https://esdiscuss.org/topic/super-call-in-methods + // https://twitter.com/wycats/status/544553184396836864 + if (methodNode.key.name !== "constructor") { + var methodName = methodNode.key.name || "METHOD_NAME"; + throw this.file.errorWithNode(node, "Direct super call is illegal in non-constructor, use super." + methodName + "() instead"); + } + var callee = node.callee; if (t.isIdentifier(callee, { name: "super" })) { // super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this); diff --git a/test/fixtures/transformation/es6-classes/super-illegal-non-constructor-call/actual.js b/test/fixtures/transformation/es6-classes/super-illegal-non-constructor-call/actual.js new file mode 100644 index 0000000000..04646da20f --- /dev/null +++ b/test/fixtures/transformation/es6-classes/super-illegal-non-constructor-call/actual.js @@ -0,0 +1,5 @@ +class Test { + foo() { + super(); + } +} diff --git a/test/fixtures/transformation/es6-classes/super-illegal-non-constructor-call/options.json b/test/fixtures/transformation/es6-classes/super-illegal-non-constructor-call/options.json new file mode 100644 index 0000000000..38d176ffd0 --- /dev/null +++ b/test/fixtures/transformation/es6-classes/super-illegal-non-constructor-call/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Direct super call is illegal in non-constructor" +}