Skip to content

Commit

Permalink
Cleanup traversals: There are a number of places that addChildrenX is…
Browse files Browse the repository at this point in the history
… called when addChildX should be. Also a few places that call "getNext()" is called on detached nodes which is a clear code smell.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=128122683
  • Loading branch information
concavelenz authored and blickly committed Jul 22, 2016
1 parent 16c96ae commit 911677d
Show file tree
Hide file tree
Showing 19 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CheckSideEffects.java
Expand Up @@ -195,7 +195,7 @@ private void addExtern() {
CompilerInput input = compiler.getSynthesizedExternsInput();
name.setStaticSourceFile(input.getSourceFile());
var.setStaticSourceFile(input.getSourceFile());
input.getAstRoot(compiler).addChildrenToBack(var);
input.getAstRoot(compiler).addChildToBack(var);
compiler.reportCodeChange();
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/Es6ConvertSuper.java
Expand Up @@ -76,7 +76,7 @@ private void addSyntheticConstructor(Node classNode) {
IR.getprop(superClass.cloneTree(), IR.string("apply")),
IR.thisNode(),
IR.name("arguments")));
body.addChildrenToFront(exprResult);
body.addChildToFront(exprResult);
}
Node constructor = IR.function(
IR.name(""),
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/Es6TemplateLiterals.java
Expand Up @@ -98,7 +98,7 @@ static void visitTaggedTemplateLiteral(NodeTraversal t, Node n) {
TEMPLATELIT_VAR + t.getCompiler().getUniqueNameIdSupplier().get());
Node var = IR.var(callsiteId, siteObject).useSourceInfoIfMissingFromForTree(n);
Node script = NodeUtil.getEnclosingScript(n);
script.addChildrenToFront(var);
script.addChildToFront(var);

// Define the "raw" property on the introduced variable.
Node defineRaw = IR.exprResult(IR.assign(IR.getprop(
Expand Down
Expand Up @@ -736,7 +736,7 @@ private Node convertDeclaredTypeToJSDoc(Node type) {
member.setDeclaredTypeExpression(null);
colon.addChildToBack(member.detachFromParent());
colon.addChildToBack(memberType);
lb.addChildrenToBack(colon);
lb.addChildToBack(colon);
}
return new Node(Token.LC, lb);
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;

import java.util.LinkedList;
import java.util.List;

Expand Down Expand Up @@ -155,7 +156,7 @@ private void doExtraction(GatherExtractionInfo info) {
Node var = NodeUtil.newVarNode(prototypeAlias, null)
.useSourceInfoIfMissingFromForTree(injectionPoint);

injectionPoint.addChildrenToFront(var);
injectionPoint.addChildToFront(var);
}
// Go through all extraction instances and extract each of them.
for (ExtractionInstance instance : info.instances) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/FunctionRewriter.java
Expand Up @@ -96,7 +96,7 @@ public void process(Node externs, Node root) {
}

Node addingRoot = compiler.getNodeForCodeInsertion(null);
addingRoot.addChildrenToFront(helperCode);
addingRoot.addChildToFront(helperCode);
compiler.reportCodeChange();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/FunctionToBlockMutator.java
Expand Up @@ -354,7 +354,7 @@ private static Node replaceReturns(
Node label = IR.label(name, block).srcref(block);

Node newRoot = IR.block().srcref(block);
newRoot.addChildrenToBack(label);
newRoot.addChildToBack(label);


// The label is now the root.
Expand Down Expand Up @@ -388,7 +388,7 @@ private static void addDummyAssignment(Node node, String resultName) {
Node resultNode = createAssignStatementNode(resultName, retVal);
resultNode.useSourceInfoIfMissingFromForTree(node);

node.addChildrenToBack(resultNode);
node.addChildToBack(resultNode);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/MinimizedCondition.java
Expand Up @@ -336,7 +336,7 @@ private static MeasuredNode addNode(Node parent, MeasuredNode... children) {
int cost = 0;
boolean changed = false;
for (MeasuredNode child : children) {
parent.addChildrenToBack(child.node);
parent.addChildToBack(child.node);
cost += child.length;
changed = changed || child.changed;
}
Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/NodeUtil.java
Expand Up @@ -35,6 +35,7 @@
import com.google.javascript.rhino.dtoa.DToA;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.TernaryValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -43,6 +44,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -2604,7 +2606,7 @@ public static void removeChild(Node parent, Node node) {
static void maybeAddFinally(Node tryNode) {
Preconditions.checkState(tryNode.isTry());
if (!NodeUtil.hasFinally(tryNode)) {
tryNode.addChildrenToBack(IR.block().srcref(tryNode));
tryNode.addChildToBack(IR.block().srcref(tryNode));
}
}

Expand Down Expand Up @@ -3621,7 +3623,6 @@ static Node newUndefinedNode(Node srcReferenceNode) {
static Node newVarNode(String name, Node value) {
Node nodeName = IR.name(name);
if (value != null) {
Preconditions.checkState(value.getNext() == null);
nodeName.addChildToBack(value);
nodeName.srcref(value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/OptimizeParameters.java
Expand Up @@ -512,7 +512,7 @@ private boolean eliminateParamsAfter(Node fnNode, Node argNode) {
eliminateParamsAfter(fnNode, argNode.getNext());
argNode.detachFromParent();
Node var = IR.var(argNode).useSourceInfoIfMissingFrom(argNode);
fnNode.getLastChild().addChildrenToFront(var);
fnNode.getLastChild().addChildToFront(var);
compiler.reportCodeChange();
return true;
}
Expand Down
18 changes: 12 additions & 6 deletions src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java
Expand Up @@ -164,8 +164,10 @@ private void tryJoinForCondition(Node n) {
*/
private Node tryReplaceIf(Node n) {

Node next = null;
for (Node child = n.getFirstChild();
child != null; child = child.getNext()){
child != null; child = next){
next = child.getNext();
if (child.isIf()){
Node cond = child.getFirstChild();
Node thenBranch = cond.getNext();
Expand Down Expand Up @@ -225,6 +227,8 @@ && isReturnBlock(thenBranch)
n.replaceChild(child, returnNode);
n.removeChild(nextNode);
reportCodeChange();
// everything else in the block is dead code.
break;
} else if (elseBranch != null && statementMustExitParent(thenBranch)) {
child.removeChild(elseBranch);
n.addChildAfter(elseBranch, child);
Expand Down Expand Up @@ -591,7 +595,7 @@ private Node tryMinimizeIf(Node n) {
unnegatedCond.getNode(),
innerCond.detachFromParent())
.srcref(placeholder));
n.addChildrenToBack(innerThenBranch.detachFromParent());
n.addChildToBack(innerThenBranch.detachFromParent());
reportCodeChange();
// Not worth trying to fold the current IF-ELSE into && because
// the inner IF-ELSE wasn't able to be folded into && anyways.
Expand Down Expand Up @@ -702,13 +706,14 @@ private Node tryMinimizeIf(Node n) {
if (name1.hasChildren()
&& maybeName2.isName()
&& name1.getString().equals(maybeName2.getString())) {
Node thenExpr = name1.removeChildren();
Preconditions.checkState(name1.hasOneChild());
Node thenExpr = name1.removeFirstChild();
Node elseExpr = elseAssign.getLastChild().detachFromParent();
placeholder.detachFromParent();
Node hookNode = IR.hook(shortCond.getNode(), thenExpr, elseExpr)
.srcref(n);
var.detachFromParent();
name1.addChildrenToBack(hookNode);
name1.addChildToBack(hookNode);
parent.replaceChild(n, var);
reportCodeChange();
return var;
Expand All @@ -728,12 +733,13 @@ private Node tryMinimizeIf(Node n) {
&& maybeName1.isName()
&& maybeName1.getString().equals(name2.getString())) {
Node thenExpr = thenAssign.getLastChild().detachFromParent();
Node elseExpr = name2.removeChildren();
Preconditions.checkState(name2.hasOneChild());
Node elseExpr = name2.removeFirstChild();
placeholder.detachFromParent();
Node hookNode = IR.hook(shortCond.getNode(), thenExpr, elseExpr)
.srcref(n);
var.detachFromParent();
name2.addChildrenToBack(hookNode);
name2.addChildToBack(hookNode);
parent.replaceChild(n, var);
reportCodeChange();

Expand Down
Expand Up @@ -385,7 +385,7 @@ private void visitRequireEnsureCall(Node n) {
callbackFunction.getSecondChild().removeChildren();
n.removeChildren();
n.putBooleanProp(Node.FREE_CALL, true);
n.addChildrenToFront(callbackFunction);
n.addChildToFront(callbackFunction);

compiler.reportCodeChange();
}
Expand Down
Expand Up @@ -54,8 +54,8 @@ Node optimizeSubtree(Node subtree) {
Node firstNode = subtree.getFirstChild().detachFromParent();
Node lastNode = subtree.getLastChild().detachFromParent();

subtree.addChildrenToFront(lastNode);
subtree.addChildrenToBack(firstNode);
subtree.addChildToFront(lastNode);
subtree.addChildToBack(firstNode);
reportCodeChange();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/RescopeGlobalSymbols.java
Expand Up @@ -117,7 +117,7 @@ private boolean isCrossModuleName(String name) {
private void addExternForGlobalSymbolNamespace() {
Node varNode = IR.var(IR.name(globalSymbolNamespace));
CompilerInput input = compiler.getSynthesizedExternsInput();
input.getAstRoot(compiler).addChildrenToBack(varNode);
input.getAstRoot(compiler).addChildToBack(varNode);
compiler.reportCodeChange();
}

Expand Down
4 changes: 3 additions & 1 deletion src/com/google/javascript/jscomp/StripCode.java
Expand Up @@ -160,8 +160,10 @@ public void visit(NodeTraversal t, Node n, Node parent) {
*/
void removeVarDeclarationsByNameOrRvalue(NodeTraversal t, Node n,
Node parent) {
Node next = null;
for (Node nameNode = n.getFirstChild(); nameNode != null;
nameNode = nameNode.getNext()) {
nameNode = next) {
next = nameNode.getNext();
String name = nameNode.getString();
if (isStripName(name) ||
isCallWhoseReturnValueShouldBeStripped(nameNode.getFirstChild())) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/rhino/TypeDeclarationsIR.java
Expand Up @@ -205,7 +205,7 @@ public static TypeDeclarationNode functionType(

if (restName != null) {
Node rest = new Node(Token.REST, IR.name(restName));
node.addChildrenToBack(maybeAddType(rest, restType));
node.addChildToBack(maybeAddType(rest, restType));
}
return node;
}
Expand Down
Expand Up @@ -67,9 +67,9 @@ private static Node createPostOrderAlphabet() {
d.addChildToBack(b);
d.addChildToBack(c);

h.addChildrenToBack(e);
h.addChildrenToBack(f);
h.addChildrenToBack(g);
h.addChildToBack(e);
h.addChildToBack(f);
h.addChildToBack(g);

l.addChildToBack(i);
l.addChildToBack(j);
Expand Down
Expand Up @@ -16,8 +16,6 @@

package com.google.javascript.jscomp;

import static com.google.javascript.jscomp.CompilerTestCase.LINE_JOINER;

import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.newtypes.JSTypeCreatorFromJSDoc;

Expand Down
Expand Up @@ -72,7 +72,7 @@ public void testMotionPreservesOriginalSourceName() {

Node nodeG = mainRoot.getFirstChild().getLastChild();
mainRoot.getFirstChild().removeChild(nodeG);
mainRoot.getLastChild().addChildrenToBack(nodeG.cloneTree());
mainRoot.getLastChild().addChildToBack(nodeG.cloneTree());

FunctionInformationMap.Builder expected =
FunctionInformationMap.newBuilder();
Expand Down

0 comments on commit 911677d

Please sign in to comment.