Skip to content

Commit

Permalink
Cleanup nits around EmptyClinitPruner.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=124899416
  • Loading branch information
gkdn authored and blickly committed Jun 15, 2016
1 parent 3f847c9 commit 39fd0c4
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions src/com/google/javascript/jscomp/J2clClinitPrunerPass.java
Expand Up @@ -15,7 +15,6 @@
*/ */
package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import com.google.common.base.Preconditions;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.jscomp.NodeTraversal.Callback; import com.google.javascript.jscomp.NodeTraversal.Callback;
Expand Down Expand Up @@ -104,7 +103,7 @@ private static boolean isNewControlBranch(Node n) {
} }


/** /**
* A traversal callback that removes the boy of empty clinits. * A traversal callback that removes the body of empty clinits.
*/ */
private static final class EmptyClinitPruner extends AbstractPostOrderCallback { private static final class EmptyClinitPruner extends AbstractPostOrderCallback {


Expand All @@ -120,49 +119,38 @@ public void visit(NodeTraversal t, Node node, Node parent) {
/** /**
* Clears the body of any functions are are equivalent to empty functions. * Clears the body of any functions are are equivalent to empty functions.
*/ */
private Node trySubstituteEmptyFunction(Node fnNode, AbstractCompiler compiler) { private void trySubstituteEmptyFunction(Node fnNode, AbstractCompiler compiler) {
Preconditions.checkArgument(fnNode.isFunction());

String fnQualifiedName = NodeUtil.getName(fnNode); String fnQualifiedName = NodeUtil.getName(fnNode);


// Ignore anonymous/constructor functions. // Ignore anonymous/constructor functions.
if (Strings.isNullOrEmpty(fnQualifiedName)) { if (Strings.isNullOrEmpty(fnQualifiedName)) {
return fnNode; return;
} }


Node body = fnNode.getLastChild(); Node body = fnNode.getLastChild();
if (!body.hasChildren()) { if (!body.hasChildren()) {
return fnNode; return;
} }


// Ensure that the first expression in the body is setting itself to the empty function and // Ensure that the first expression in the body is setting itself to the empty function and
// there are no other expressions. // there are no other expressions.
Node firstExpr = body.getFirstChild(); Node firstExpr = body.getFirstChild();
if (firstExpr.isExprResult() if (!isAssignToEmptyFn(firstExpr, fnQualifiedName) || firstExpr.getNext() != null) {
&& isAssignToEmptyFn(firstExpr.getFirstChild(), fnQualifiedName) return;
&& (firstExpr.getNext() == null)) {
body.removeChildren();
compiler.reportCodeChange();
} }


return fnNode; body.removeChild(firstExpr);
compiler.reportCodeChange();
} }


private static boolean isAssignToEmptyFn(Node assignNode, String enclosingFnName) { private static boolean isAssignToEmptyFn(Node node, String enclosingFnName) {
if (!assignNode.isAssign()) { if (!NodeUtil.isExprAssign(node)) {
return false;
}

Node lhs = assignNode.getFirstChild();
Node rhs = assignNode.getLastChild();

// If the RHS is not an empty function then this isn't of interest.
if (!NodeUtil.isEmptyFunctionExpression(rhs)) {
return false; return false;
} }


// Ensure that we are actually mutating the given function. Node lhs = node.getFirstChild().getFirstChild();
return lhs.getQualifiedName() != null && lhs.matchesQualifiedName(enclosingFnName); Node rhs = node.getFirstChild().getLastChild();
return NodeUtil.isEmptyFunctionExpression(rhs) && lhs.matchesQualifiedName(enclosingFnName);
} }
} }


Expand Down

0 comments on commit 39fd0c4

Please sign in to comment.