Skip to content

Commit

Permalink
Use String-based switch instead of if statements in ProcessClosurePri…
Browse files Browse the repository at this point in the history
…mitives

Also, replace unnecessary usage of SimpleFormat with String concatenation.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=149121319
  • Loading branch information
Dominator008 authored and dimvar committed Mar 6, 2017
1 parent 1f7d74b commit fe883cb
Showing 1 changed file with 53 additions and 44 deletions.
97 changes: 53 additions & 44 deletions src/com/google/javascript/jscomp/ProcessClosurePrimitives.java
Expand Up @@ -20,7 +20,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.jscomp.parsing.parser.util.format.SimpleFormat;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSTypeExpression;
Expand Down Expand Up @@ -259,44 +258,56 @@ public void visit(NodeTraversal t, Node n, Node parent) {
// when we see a provides/requires, and don't worry about
// reporting the change when we actually do the replacement.
String methodName = name.getNext().getString();
if ("base".equals(methodName)) {
processBaseClassCall(t, n);
} else if ("define".equals(methodName)) {
if (validPrimitiveCall(t, n)) {
processDefineCall(t, n, parent);
}
} else if ("require".equals(methodName)) {
if (validPrimitiveCall(t, n)) {
processRequireCall(t, n, parent);
}
} else if ("provide".equals(methodName)) {
if (validPrimitiveCall(t, n)) {
processProvideCall(t, n, parent);
}
} else if ("inherits".equals(methodName)) {
// Note: inherits is allowed in local scope
processInheritsCall(n);
} else if ("exportSymbol".equals(methodName)) {
// Note: exportSymbol is allowed in local scope
Node arg = left.getNext();
if (arg.isString()) {
int dot = arg.getString().indexOf('.');
if (dot == -1) {
exportedVariables.add(arg.getString());
} else {
exportedVariables.add(arg.getString().substring(0, dot));
switch (methodName) {
case "base":
processBaseClassCall(t, n);
break;
case "define":
if (validPrimitiveCall(t, n)) {
processDefineCall(t, n, parent);
}
}
} else if ("forwardDeclare".equals(methodName)){
if (validPrimitiveCall(t, n)) {
processForwardDeclare(t, n, parent);
}
} else if ("addDependency".equals(methodName)) {
if (validPrimitiveCall(t, n)) {
processAddDependency(n, parent);
}
} else if ("setCssNameMapping".equals(methodName)) {
processSetCssNameMapping(t, n, parent);
break;
case "require":
if (validPrimitiveCall(t, n)) {
processRequireCall(t, n, parent);
}
break;
case "provide":
if (validPrimitiveCall(t, n)) {
processProvideCall(t, n, parent);
}
break;
case "inherits":
// Note: inherits is allowed in local scope
processInheritsCall(n);
break;
case "exportSymbol":
// Note: exportSymbol is allowed in local scope
Node arg = left.getNext();
if (arg.isString()) {
String argString = arg.getString();
int dot = argString.indexOf('.');
if (dot == -1) {
exportedVariables.add(argString);
} else {
exportedVariables.add(argString.substring(0, dot));
}
}
break;
case "forwardDeclare":
if (validPrimitiveCall(t, n)) {
processForwardDeclare(t, n, parent);
}
break;
case "addDependency":
if (validPrimitiveCall(t, n)) {
processAddDependency(n, parent);
}
break;
case "setCssNameMapping":
processSetCssNameMapping(t, n, parent);
break;
default: // fall out
}
} else if (left.getLastChild().getString().equals("base")) {
// maybe an "base" setup by goog.inherits
Expand Down Expand Up @@ -620,7 +631,7 @@ private void processBaseClassCall(NodeTraversal t, Node n) {
callee,
NodeUtil.newQName(
compiler,
SimpleFormat.format("%s.call", baseClassNode.getQualifiedName()),
baseClassNode.getQualifiedName() + ".call",
callee, "goog.base"));
compiler.reportCodeChange();
} else {
Expand All @@ -646,8 +657,7 @@ private void processBaseClassCall(NodeTraversal t, Node n) {
callee,
NodeUtil.newQName(
compiler,
SimpleFormat.format("%s.superClass_.%s.call",
className.getQualifiedName(), methodName),
className.getQualifiedName() + ".superClass_." + methodName + ".call",
callee, "goog.base"));
n.removeChild(methodNameNode);
compiler.reportCodeChange();
Expand Down Expand Up @@ -769,7 +779,7 @@ private void maybeProcessClassBaseCall(NodeTraversal t, Node n) {
callee,
NodeUtil.newQName(
compiler,
SimpleFormat.format("%s.call", baseClassNode.getQualifiedName()),
baseClassNode.getQualifiedName() + ".call",
callee, enclosingQname + ".base"));
n.removeChild(methodNameNode);
compiler.reportCodeChange();
Expand Down Expand Up @@ -821,8 +831,7 @@ private void maybeProcessClassBaseCall(NodeTraversal t, Node n) {
callee,
NodeUtil.newQName(
compiler,
SimpleFormat.format("%s.superClass_.%s.call",
className.getQualifiedName(), methodName),
className.getQualifiedName() + ".superClass_." + methodName + ".call",
callee, enclosingQname + ".base"));
n.removeChild(methodNameNode);
compiler.reportCodeChange();
Expand Down

0 comments on commit fe883cb

Please sign in to comment.