Skip to content

Commit 3bac804

Browse files
committed
Translate super calls made in a static context of the form super() to call the corresponding method on the super class.
------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=70413769
1 parent c7d196e commit 3bac804

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/com/google/javascript/jscomp/Es6ToEs3Converter.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
150150
visitForOf(n, parent);
151151
break;
152152
case Token.SUPER:
153-
visitSuper(n);
153+
visitSuper(n, parent);
154154
break;
155155
case Token.STRING_KEY:
156156
visitStringKey(n);
@@ -238,14 +238,14 @@ private void checkClassReassignment(Node clazz) {
238238
NodeTraversal.traverse(compiler, enclosingFunction, checkAssigns);
239239
}
240240

241-
private void visitSuper(Node node) {
242-
Node enclosing = node.getParent();
241+
private void visitSuper(Node node, Node parent) {
242+
Node enclosing = parent;
243243
Node potentialCallee = node;
244-
if (!enclosing.isCall()) {
245-
enclosing = enclosing.getParent();
246-
potentialCallee = potentialCallee.getParent();
244+
if (!parent.isCall()) {
245+
enclosing = parent.getParent();
246+
potentialCallee = parent;
247247
}
248-
if (!(enclosing.isCall() && enclosing.getFirstChild() == potentialCallee)) {
248+
if (!enclosing.isCall() || enclosing.getFirstChild() != potentialCallee) {
249249
cannotConvertYet(node, "Only calls to super or to a method of super are supported.");
250250
return;
251251
}
@@ -265,11 +265,22 @@ private void visitSuper(Node node) {
265265

266266
Node enclosingMemberDef = NodeUtil.getEnclosingClassMember(node);
267267
if (enclosingMemberDef.isStaticMember()) {
268-
node.getParent().replaceChild(node, clazz.getFirstChild().getNext().cloneTree());
269-
Node callTarget = IR.getprop(potentialCallee.detachFromParent(), IR.string("call"));
270-
callTarget.useSourceInfoIfMissingFromForTree(enclosing);
268+
Node superName = clazz.getFirstChild().getNext();
269+
Node callTarget;
270+
potentialCallee.detachFromParent();
271+
if (potentialCallee == node) {
272+
// of the form super()
273+
potentialCallee =
274+
IR.getprop(superName.cloneTree(), IR.string(enclosingMemberDef.getString()));
275+
enclosing.putBooleanProp(Node.FREE_CALL, false);
276+
} else {
277+
// of the form super.method()
278+
potentialCallee.replaceChild(node, superName.cloneTree());
279+
}
280+
callTarget = IR.getprop(potentialCallee, IR.string("call"));
271281
enclosing.addChildToFront(callTarget);
272282
enclosing.addChildAfter(IR.thisNode(), callTarget);
283+
enclosing.useSourceInfoIfMissingFromForTree(enclosing);
273284
compiler.reportCodeChange();
274285
return;
275286
}

src/com/google/javascript/rhino/IR.java

+1
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ private static boolean mayBeExpression(Node n) {
657657
case Token.STRING:
658658
case Token.SUB:
659659
case Token.THIS:
660+
case Token.SUPER:
660661
case Token.TYPEOF:
661662
case Token.TRUE:
662663
case Token.URSH:

test/com/google/javascript/jscomp/Es6ToEs3ConverterTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,22 @@ public void testSuperNew() {
587587
), Es6ToEs3Converter.CANNOT_CONVERT_YET);
588588
}
589589

590+
public void testSuperCallNonConstructor() {
591+
test("class S extends B { static f() { super(); } }", Joiner.on('\n').join(
592+
"/** @constructor @struct @extends {B} */",
593+
"var S = function() {};",
594+
"$jscomp$inherits(S, B);",
595+
"$jscomp$copy$properties(S, B);",
596+
"S.f=function() { B.f.call(this) }"));
597+
598+
test("class S extends B { f() { super(); } }", Joiner.on('\n').join(
599+
"/** @constructor @struct @extends {B} */",
600+
"var S = function() {};",
601+
"$jscomp$inherits(S, B);",
602+
"$jscomp$copy$properties(S, B);",
603+
"S.prototype.f=function() { S.base(this, \"f\") }"));
604+
}
605+
590606
public void testStaticMethods() {
591607
test(Joiner.on('\n').join(
592608
"class C {",

0 commit comments

Comments
 (0)