| 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.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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 63 |
|
| 64 |
|
| 65 |
private void maybeCollapseIntoForStatements(Node n, Node parent) { |
| 66 |
|
| 67 |
|
| 68 |
if (parent == null || !NodeUtil.isStatementBlock(parent)) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
if (!n.isExprResult() && !n.isVar()) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 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 |
|
| 90 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if (NodeUtil.containsType(n, Token.IN)) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 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 |
|
| 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 |
} |