diff --git a/lib/6to5/transformation/helpers/replace-supers.js b/lib/6to5/transformation/helpers/replace-supers.js index a09890bb4d..d4d59b056c 100644 --- a/lib/6to5/transformation/helpers/replace-supers.js +++ b/lib/6to5/transformation/helpers/replace-supers.js @@ -212,13 +212,13 @@ ReplaceSupers.prototype.specHandle = function (getThisReference, node, parent) { var args; var thisReference; - if (t.isIdentifier(node, { name: "super" })) { - if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) { - throw this.file.errorWithNode(node, "illegal use of bare super"); - } - } else if (t.isCallExpression(node)) { + if (isIllegalBareSuper(node, parent)) { + throw this.file.errorWithNode(node, "Illegal use of bare super"); + } + + if (t.isCallExpression(node)) { var callee = node.callee; - if (t.isIdentifier(callee, { name: "super" })) { + if (isSuper(callee, node)) { // super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this); property = methodNode.key; computed = methodNode.computed; @@ -231,28 +231,19 @@ ReplaceSupers.prototype.specHandle = function (getThisReference, node, parent) { var methodName = methodNode.key.name || "METHOD_NAME"; throw this.file.errorWithNode(node, "Direct super call is illegal in non-constructor, use super." + methodName + "() instead"); } - } else { - if (!t.isMemberExpression(callee)) return; - if (callee.object.name !== "super") return; - + } else if (t.isMemberExpression(callee) && isSuper(callee.object, callee)) { // super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this); property = callee.property; computed = callee.computed; args = node.arguments; } - } else if (t.isMemberExpression(node)) { - if (!t.isIdentifier(node.object, { name: "super" })) return; - + } else if (t.isMemberExpression(node) && isSuper(node.object, node)) { // super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this); property = node.property; computed = node.computed; - } else if (t.isAssignmentExpression(node)) { - if (!t.isIdentifier(node.left.object, { name: "super" })) return; - if (methodNode.kind !== "set") return; - - thisReference = getThisReference(); + } else if (t.isAssignmentExpression(node) && isSuper(node.left.object, node.left) && methodNode.kind === "set") { // super.name = "val"; -> _set(Object.getPrototypeOf(ClassName.prototype), "name", this); - return this.setSuperProperty(node.left.property, node.right, methodNode.static, node.left.computed, thisReference); + return this.setSuperProperty(node.left.property, node.right, methodNode.static, node.left.computed, getThisReference()); } if (!property) return; @@ -276,3 +267,14 @@ ReplaceSupers.prototype.specHandle = function (getThisReference, node, parent) { return superProperty; } }; + +var isIllegalBareSuper = function (node, parent) { + if (!isSuper(node, parent)) return false; + if (t.isMemberExpression(parent, { computed: false })) return false; + if (t.isCallExpression(parent, { callee: node })) return false; + return true; +}; + +var isSuper = function (node, parent) { + return t.isIdentifier(node, { name: "super" }) && t.isReferenced(node, parent); +}; diff --git a/test/fixtures/transformation/es6-classes/bare-super/options.json b/test/fixtures/transformation/es6-classes/bare-super/options.json index f878b7f9cd..9813318b1b 100644 --- a/test/fixtures/transformation/es6-classes/bare-super/options.json +++ b/test/fixtures/transformation/es6-classes/bare-super/options.json @@ -1,3 +1,3 @@ { - "throws": "illegal use of bare super" + "throws": "Illegal use of bare super" }