Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.jscomp.NodeTraversal.Callback;
22 import com.google.javascript.rhino.Node;
23 import com.google.javascript.rhino.Token;
24
25 /**
26 * The goal with this pass is to reverse the simplifications done in the
27 * normalization pass that are not handled by other passes (such as
28 * CollapseVariableDeclarations) to avoid making the resulting code larger.
29 *
30 * Currently this pass only does one thing pushing statements into for-loop
31 * initializer. This:
32 * var a = 0; for(;a<0;a++) {}
33 * becomes:
34 * for(var a = 0;a<0;a++) {}
35 *
36 * @author [email protected] (johnlenz)
37 */
38 class Denormalize implements CompilerPass, Callback {
39
40 private final AbstractCompiler compiler;
41
42 Denormalize(AbstractCompiler compiler) {
43 this.compiler = compiler;
44 }
45
46 @Override
47 public void process(Node externs, Node root) {
48 NodeTraversal.traverse(compiler, root, this);
49 }
50
51 @Override
52 public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
53 return true;
54 }
55
56 @Override
57 public void visit(NodeTraversal t, Node n, Node parent) {
58 maybeCollapseIntoForStatements(n, parent);
59 }
60
61 /**
62 * Collapse VARs and EXPR_RESULT node into FOR loop initializers where
63 * possible.
64 */
65 private void maybeCollapseIntoForStatements(Node n, Node parent) {
66 // Only SCRIPT, BLOCK, and LABELs can have FORs that can be collapsed into.
67 // LABELs are not supported here.
68 if (parent == null || !NodeUtil.isStatementBlock(parent)) {
69 return;
70 }
71
72 // Is the current node something that can be in a for loop initializer?
73 if (!n.isExprResult() && !n.isVar()) {
74 return;
75 }
76
77 // Is the next statement a valid FOR?
78 Node nextSibling = n.getNext();
79 if (nextSibling == null) {
80 return;
81 } else if (NodeUtil.isForIn(nextSibling)) {
82 Node forNode = nextSibling;
83 Node forVar = forNode.getFirstChild();
84 if (forVar.isName()
85 && n.isVar() && n.hasOneChild()) {
86 Node name = n.getFirstChild();
87 if (!name.hasChildren()
88 && forVar.getString().equals(name.getString())) {
89 // OK, the names match, and the var declaration does not have an
90 // initializer. Move it into the loop.
91 parent.removeChild(n);
92 forNode.replaceChild(forVar, n);
93 compiler.reportCodeChange();
94 }
95 }
96 } else if (nextSibling.isFor()
97 && nextSibling.getFirstChild().isEmpty()) {
98
99 // Does the current node contain an in operator? If so, embedding
100 // the expression in a for loop can cause some JavaScript parsers (such
101 // as the PlayStation 3's browser based on Access's NetFront
102 // browser) to fail to parse the code.
103 // See bug 1778863 for details.
104 if (NodeUtil.containsType(n, Token.IN)) {
105 return;
106 }
107
108 // Move the current node into the FOR loop initializer.
109 Node forNode = nextSibling;
110 Node oldInitializer = forNode.getFirstChild();
111 parent.removeChild(n);
112
113 Node newInitializer;
114 if (n.isVar()) {
115 newInitializer = n;
116 } else {
117 // Extract the expression from EXPR_RESULT node.
118 Preconditions.checkState(n.hasOneChild());
119 newInitializer = n.getFirstChild();
120 n.removeChild(newInitializer);
121 }
122
123 forNode.replaceChild(oldInitializer, newInitializer);
124
125 compiler.reportCodeChange();
126 }
127 }
128
129 static class StripConstantAnnotations
130 extends AbstractPostOrderCallback
131 implements CompilerPass {
132 private AbstractCompiler compiler;
133
134 StripConstantAnnotations(AbstractCompiler compiler) {
135 this.compiler = compiler;
136 }
137
138 @Override
139 public void process(Node externs, Node js) {
140 NodeTraversal.traverse(compiler, externs, this);
141 NodeTraversal.traverse(compiler, js, this);
142 }
143
144 @Override
145 public void visit(NodeTraversal t, Node node, Node parent) {
146 if (node.isName() || node.isString() || node.isStringKey()) {
147 node.removeProp(Node.IS_CONSTANT_NAME);
148 }
149 }
150 }
151 }