Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Look for internal properties set using "this" but never read. Explicitly
31 * ignored is the possibility that these properties
32 * may be indirectly referenced using "for-in" or "Object.keys". This is the
33 * same assumption used with RemoveUnusedPrototypeProperties but is by slightly
34 * wider in scope.
35 *
36 * @author [email protected] (John Lenz)
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 // 'this.x = y' to 'y'
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 // This is a definition of a property but it is only removable
95 // if it is defined on "this".
96 if (n.getFirstChild().isThis()) {
97 candidates.add(n);
98 }
99 }
100 break;
101 }
102
103 case Token.CALL:
104 // Look for properties referenced through "JSCompiler_propertyRename".
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 * @return Whether the property is used in a way that prevents its removal.
120 */
121 private boolean isPinningPropertyUse(Node n) {
122 // Rather than looking for cases that are uses, we assume all references are
123 // pinning uses unless they are:
124 // - a simple assignment (x.a = 1)
125 // - a compound assignment or increment (x++, x += 1) whose result is
126 // otherwise unused
127
128 Node parent = n.getParent();
129 if (n == parent.getFirstChild()) {
130 if (parent.isAssign()) {
131 // A simple assignment doesn't pin the property.
132 return false;
133 } else if (NodeUtil.isAssignmentOp(parent)
134 || parent.isInc() || parent.isDec()) {
135 // In general, compound assignments are both reads and writes, but
136 // if the property is never otherwise read we can consider it simply
137 // a write.
138 // However if the assign expression is used as part of a larger
139 // expression, we much consider it a read. For example:
140 // x = (y.a += 1);
141 return NodeUtil.isExpressionResultUsed(parent);
142 }
143 }
144 return true;
145 }
146 }