Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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.collect.Sets;
20 import com.google.javascript.jscomp.NodeTraversal.ScopedCallback;
21 import com.google.javascript.jscomp.Scope.Var;
22 import com.google.javascript.rhino.IR;
23 import com.google.javascript.rhino.Node;
24 import java.util.Iterator;
25 import java.util.Set;
26
27 /**
28 * Groups multiple variable declarations (that may or may not be contiguous)
29 * in the same scope into a single one. i.e.
30 *
31 * <pre>
32 * var a, b = 10;
33 * f1();
34 * var c, d;
35 * ... some other code ...
36 * var x, y, z = 100;
37 * ... some other code ...
38 * var p = 200, q = 300;
39 * </pre>
40 *
41 * becomes:
42 *
43 * <pre>
44 * var a, b = 10, c, d, x, y, z;
45 * f1();
46 * ... some other code ...
47 * z = 100;
48 * ... some other code ...
49 * var p = 200, q = 300;
50 * </pre>
51 *
52 * This reduces the generated uncompressed code size.
53 *
54 * For any scope, we use the first VAR statement as the statement to collapse
55 * the other declarations into. For other VAR statements in the scope, we only
56 * consider ones that either (a) have no variable that is initialized in the
57 * the statement, or (b) have exactly one variable that is initialized (e.g.
58 * the "var x, y, z = 100;" statement in the example above. VAR statements
59 * with more than one variable initialization are not collapsed. This is
60 * because doing so would increase uncompressed code size.
61 *
62 */
63 class GroupVariableDeclarations implements CompilerPass, ScopedCallback {
64 private final AbstractCompiler compiler;
65
66 GroupVariableDeclarations(AbstractCompiler compiler) {
67 this.compiler = compiler;
68 }
69
70 @Override
71 public void process(Node externs, Node root) {
72 NodeTraversal.traverse(compiler, root, this);
73 }
74
75 @Override
76 public void enterScope(NodeTraversal t) {
77 Set<Node> varNodes = Sets.newLinkedHashSet();
78 Iterator<Var> scopeVarIter = t.getScope().getVars();
79 while (scopeVarIter.hasNext()) {
80 Node parentNode = scopeVarIter.next().getParentNode();
81 if (parentNode.isVar()) {
82 varNodes.add(parentNode);
83 }
84 }
85 if (varNodes.size() <= 1) {
86 return;
87 }
88 Iterator<Node> varNodeIter = varNodes.iterator();
89 Node firstVarNode = varNodeIter.next();
90 while (varNodeIter.hasNext()) {
91 Node varNode = varNodeIter.next();
92 applyGroupingToVar(firstVarNode, varNode);
93 }
94 }
95
96 @Override
97 public void exitScope(NodeTraversal t) {
98 }
99
100 @Override
101 public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n,
102 Node parent) {
103 return true;
104 }
105
106 @Override
107 public void visit(NodeTraversal t, Node n, Node parent) {
108 }
109
110 /**
111 * Attempts to collapse groupVar. This can only happen if groupVar has at most
112 * one variable initialization (it may have multiple variable declarations).
113 * If successful, then detaches groupVar's children and appends them to
114 * firstVar
115 *
116 * @param firstVar The first VAR {@code Node} in that scope. This is the node
117 * that we want to collapse groupVar into
118 * @param groupVar The VAR {@code Node} that we want to try collapsing
119 * into the first VAR node of that scope
120 */
121 private void applyGroupingToVar(Node firstVar, Node groupVar) {
122 Node child = groupVar.getFirstChild();
123 // if some variable is initialized, then the corresponding NAME node will be
124 // stored here
125 Node initializedName = null;
126 while (child != null) {
127 if (child.hasChildren()) {
128 // check that no more than one var is initialized
129 if (initializedName != null) {
130 return;
131 }
132 initializedName = child;
133 }
134 child = child.getNext();
135 }
136
137 // we will be modifying the groupVar subtree so get its parent
138 Node groupVarParent = groupVar.getParent();
139
140
141 if (initializedName != null) {
142 if (NodeUtil.isForIn(groupVarParent)) {
143 // The target of the for-in expression must be an assignable expression.
144 return;
145 }
146
147 // we have an initialized var in the VAR node. We will replace the
148 // VAR node with an assignment.
149
150 // first create a detached childless clone of initializedName.
151 Node clone = initializedName.cloneNode();
152 // replace
153 groupVar.replaceChild(initializedName, clone);
154 // add the assignment now.
155 Node initializedVal = initializedName.removeFirstChild();
156 Node assignmentNode = IR.assign(initializedName, initializedVal);
157 if (groupVarParent.isFor()) {
158 // Handle For and For-In Loops specially. For these, we do not need
159 // to construct an EXPR_RESULT node.
160 groupVarParent.replaceChild(groupVar, assignmentNode);
161 } else {
162 Node exprNode = NodeUtil.newExpr(assignmentNode);
163 groupVarParent.replaceChild(groupVar, exprNode);
164 }
165 } else {
166 // There is no initialized var. But we need to handle FOR and
167 // FOR-IN loops specially
168 if (groupVarParent.isFor()) {
169 if (NodeUtil.isForIn(groupVarParent)) {
170 // In For-In loop, we replace the VAR node with a NAME node
171 Node nameNodeClone = groupVar.getFirstChild().cloneNode();
172 groupVarParent.replaceChild(groupVar, nameNodeClone);
173 } else {
174 // In For loop, we replace the VAR node with an EMPTY node
175 Node emptyNode = IR.empty();
176 groupVarParent.replaceChild(groupVar, emptyNode);
177 }
178 } else {
179 // we can safely remove the VAR node
180 groupVarParent.removeChild(groupVar);
181 }
182 }
183
184 Node children = groupVar.removeChildren();
185 firstVar.addChildrenToBack(children);
186
187 compiler.reportCodeChange();
188 }
189 }