| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.common.collect.Sets; |
| 22 |
import com.google.javascript.rhino.IR; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import com.google.javascript.rhino.Token; |
| 25 |
|
| 26 |
import java.util.List; |
| 27 |
import java.util.Set; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class RemoveUnusedClassProperties |
| 39 |
implements CompilerPass, NodeTraversal.Callback { |
| 40 |
final AbstractCompiler compiler; |
| 41 |
private boolean inExterns; |
| 42 |
private Set<String> used = Sets.newHashSet(); |
| 43 |
private List<Node> candidates = Lists.newArrayList(); |
| 44 |
|
| 45 |
RemoveUnusedClassProperties(AbstractCompiler compiler) { |
| 46 |
this.compiler = compiler; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void process(Node externs, Node root) { |
| 51 |
NodeTraversal.traverseRoots(compiler, this, externs, root); |
| 52 |
removeUnused(); |
| 53 |
} |
| 54 |
|
| 55 |
private void removeUnused() { |
| 56 |
for (Node n : candidates) { |
| 57 |
Preconditions.checkState(n.isGetProp()); |
| 58 |
if (!used.contains(n.getLastChild().getString())) { |
| 59 |
Node parent = n.getParent(); |
| 60 |
if (NodeUtil.isAssignmentOp(parent)) { |
| 61 |
Node assign = parent; |
| 62 |
Preconditions.checkState(assign != null |
| 63 |
&& NodeUtil.isAssignmentOp(assign) |
| 64 |
&& assign.getFirstChild() == n); |
| 65 |
|
| 66 |
assign.getParent().replaceChild(assign, |
| 67 |
assign.getLastChild().detachFromParent()); |
| 68 |
} else if (parent.isInc() || parent.isDec()) { |
| 69 |
parent.getParent().replaceChild(parent, IR.number(0)); |
| 70 |
} else { |
| 71 |
throw new IllegalStateException("unexpected: "+ parent); |
| 72 |
} |
| 73 |
compiler.reportCodeChange(); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { |
| 80 |
if (n.isScript()) { |
| 81 |
this.inExterns = n.getStaticSourceFile().isExtern(); |
| 82 |
} |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 88 |
switch (n.getType()) { |
| 89 |
case Token.GETPROP: { |
| 90 |
String propName = n.getLastChild().getString(); |
| 91 |
if (inExterns || isPinningPropertyUse(n)) { |
| 92 |
used.add(propName); |
| 93 |
} else { |
| 94 |
|
| 95 |
|
| 96 |
if (n.getFirstChild().isThis()) { |
| 97 |
candidates.add(n); |
| 98 |
} |
| 99 |
} |
| 100 |
break; |
| 101 |
} |
| 102 |
|
| 103 |
case Token.CALL: |
| 104 |
|
| 105 |
Node target = n.getFirstChild(); |
| 106 |
if (n.hasMoreThanOneChild() |
| 107 |
&& target.isName() |
| 108 |
&& target.getString().equals(NodeUtil.JSC_PROPERTY_NAME_FN)) { |
| 109 |
Node propName = target.getNext(); |
| 110 |
if (propName.isString()) { |
| 111 |
used.add(propName.getString()); |
| 112 |
} |
| 113 |
} |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private boolean isPinningPropertyUse(Node n) { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
Node parent = n.getParent(); |
| 129 |
if (n == parent.getFirstChild()) { |
| 130 |
if (parent.isAssign()) { |
| 131 |
|
| 132 |
return false; |
| 133 |
} else if (NodeUtil.isAssignmentOp(parent) |
| 134 |
|| parent.isInc() || parent.isDec()) { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
return NodeUtil.isExpressionResultUsed(parent); |
| 142 |
} |
| 143 |
} |
| 144 |
return true; |
| 145 |
} |
| 146 |
} |