Skip to content

Commit

Permalink
Rollback of 5884e77 because it breaks some Google-internal code.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=152956678
  • Loading branch information
tbreisacher authored and brad4d committed Apr 12, 2017
1 parent bafa491 commit 5a1bc0b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 492 deletions.
8 changes: 0 additions & 8 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ protected List<PassFactory> getOptimizations() {
return passes;
}

passes.add(hoistVars);
passes.add(normalize);

// Create extern exports after the normalize because externExports depends on unique names.
Expand Down Expand Up @@ -2591,13 +2590,6 @@ protected CompilerPass create(final AbstractCompiler compiler) {
}
};

private final PassFactory hoistVars = new PassFactory("hoistVars", true) {
@Override
protected CompilerPass create(AbstractCompiler compiler) {
return new HoistVarsOutOfBlocks(compiler);
}
};

private final PassFactory normalize = new PassFactory("normalize", true) {
@Override
protected CompilerPass create(AbstractCompiler compiler) {
Expand Down
65 changes: 4 additions & 61 deletions src/com/google/javascript/jscomp/Denormalize.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
import static com.google.common.base.Preconditions.checkState;

import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.jscomp.ReferenceCollectingCallback.Behavior;
import com.google.javascript.jscomp.ReferenceCollectingCallback.Reference;
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection;
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceMap;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;

Expand All @@ -32,22 +27,16 @@
* not handled by other passes (such as CollapseVariableDeclarations) to avoid making the resulting
* code larger.
*
* <p>Currently this pass only does a few things:
* <p>Currently this pass only does two things:
*
* <p>1. Push statements into for-loop initializer. This: var a = 0; for(;a<0;a++) {} becomes:
* for(var a = 0;a<0;a++) {}
* <p>1. Push statements into for-loop initializer. This: <pre>var a = 0; for(;a<0;a++) {}</pre>
* becomes: <pre>for(var a = 0;a<0;a++) {}</pre>
*
* <p>2. Fold assignments like x = x + 1 into x += 1
*
* <p>3. Inline 'var' keyword. For instance: <code>
* var x;
* if (y) { x = 0; }
* </code> becomes <code>if (y) { var x = 0; }</code>, effectively undoing what {@link
* Normalize.HoistVarsOutOfBlocks} does.
*
* @author johnlenz@google.com (johnlenz)
*/
class Denormalize implements CompilerPass, Callback, Behavior {
class Denormalize implements CompilerPass, Callback {

private final AbstractCompiler compiler;

Expand All @@ -58,59 +47,13 @@ class Denormalize implements CompilerPass, Callback, Behavior {
@Override
public void process(Node externs, Node root) {
NodeTraversal.traverseEs6(compiler, root, this);
// Don't inline the VAR declaration if this compilation involves old-style ctemplates.
if (compiler.getOptions().syntheticBlockStartMarker == null) {
(new ReferenceCollectingCallback(compiler, this, new Es6SyntacticScopeCreator(compiler)))
.process(root);
}
}

@Override
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
return true;
}

/**
* Implements step 3 (inlining the var keyword).
*/
@Override
public void afterExitScope(NodeTraversal t, ReferenceMap referenceMap) {
if (t.getScopeRoot().isNormalBlock() && t.getScopeRoot().getParent().isFunction()) {
for (Var v : t.getScope().getVarIterable()) {
ReferenceCollection references = referenceMap.getReferences(v);
Reference declaration = null;
Reference assign = null;
for (Reference r : references) {
if (r.isVarDeclaration()
&& NodeUtil.isStatement(r.getNode().getParent())
&& !r.isInitializingDeclaration()) {
declaration = r;
} else if (assign == null
&& r.isSimpleAssignmentToName()
&& r.getScope().getClosestHoistScope().equals(t.getScope())) {
assign = r;
}
}
if (declaration != null && assign != null) {
Node lhs = assign.getNode();
Node assignNode = lhs.getParent();
if (assignNode.getParent().isExprResult()) {
Node rhs = lhs.getNext();
assignNode
.getGrandparent()
.replaceChild(assignNode.getParent(), IR.var(lhs.detach(), rhs.detach()));

checkState(
declaration.getNode().getParent().isVar(), declaration.getNode().getParent());
NodeUtil.removeChild(declaration.getNode().getParent(), declaration.getNode());

compiler.reportCodeChange();
}
}
}
}
}

@Override
public void visit(NodeTraversal t, Node n, Node parent) {
maybeCollapseIntoForStatements(n, parent);
Expand Down
138 changes: 0 additions & 138 deletions src/com/google/javascript/jscomp/HoistVarsOutOfBlocks.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/com/google/javascript/rhino/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -1565,16 +1565,6 @@ public Node getAncestor(int level) {
return node;
}

/** @return True if this Node is {@code node} or a descendant of {@code node}. */
public boolean isDescendantOf(Node node) {
for (Node n = this; n != null; n = n.parent) {
if (n == node) {
return true;
}
}
return false;
}

/**
* Iterates all of the node's ancestors excluding itself.
*/
Expand Down
36 changes: 1 addition & 35 deletions test/com/google/javascript/jscomp/DenormalizeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,6 @@ protected int getNumRepetitions() {
return 1;
}

public void testInlineVarKeyword1() {
test(
LINE_JOINER.join(
"function f() {",
" var x;",
" function g() { x = 2; }",
" if (y) { x = -1; }",
" alert(x);",
"}"),
LINE_JOINER.join(
"function f() {",
" function g() { x = 2; }",
" if (y) { var x = -1; }",
" alert(x);",
"}"));
}

public void testInlineVarKeyword2() {
test(
LINE_JOINER.join(
"function f() {",
" var x;",
" function g() { x = 2; }",
" if (y) { x = -1; } else { x = 3; }",
" alert(x);",
"}"),
LINE_JOINER.join(
"function f() {",
" function g() { x = 2; }",
" if (y) { var x = -1; } else { x = 3; }",
" alert(x);",
"}"));
}

public void testFor() {
// Verify assignments are moved into the FOR init node.
test("a = 0; for(; a < 2 ; a++) foo()",
Expand Down Expand Up @@ -178,7 +144,7 @@ public NormalizeAndDenormalizePass(AbstractCompiler compiler) {
@Override
public void process(Node externs, Node root) {
NodeTraversal.traverseEs6(compiler, root, normalizePass);
denormalizePass.process(externs, root);
NodeTraversal.traverseEs6(compiler, root, denormalizePass);
}
}

Expand Down

0 comments on commit 5a1bc0b

Please sign in to comment.