Skip to content

Commit

Permalink
When validating per-scope changes skip inner change scopes instead of…
Browse files Browse the repository at this point in the history
… short circuiting. Previous logic was considering changed scopes as unchanged.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=152845434
  • Loading branch information
concavelenz authored and brad4d committed Apr 12, 2017
1 parent 120d1c7 commit b4aafc1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/com/google/javascript/jscomp/NodeUtil.java
Expand Up @@ -4735,18 +4735,9 @@ private static void mtocHelper(Map<Node, Node> map, Node main, Node clone) {


/** Checks that the scope roots marked as changed have indeed changed */ /** Checks that the scope roots marked as changed have indeed changed */
public static void verifyScopeChanges( public static void verifyScopeChanges(
String passName, Map<Node, Node> map, Node main) { String passName, final Map<Node, Node> mtoc, Node main) {
final String passNameMsg = passName.isEmpty() ? "" : passName + ": "; final String passNameMsg = passName.isEmpty() ? "" : passName + ": ";


// compiler is passed only to call compiler.toSource during debugging to see
// mismatches in scopes

// If verifyUnchangedNodes is false, we are comparing the initial AST to the
// final AST. Don't check unmarked nodes b/c they may have been changed by
// non-loopable passes.
// If verifyUnchangedNodes is true, we are comparing the ASTs before & after
// a pass. Check all scope roots.
final Map<Node, Node> mtoc = map;
Node clone = mtoc.get(main); Node clone = mtoc.get(main);
if (main.getChangeTime() > clone.getChangeTime()) { if (main.getChangeTime() > clone.getChangeTime()) {
Preconditions.checkState(!isEquivalentToExcludingFunctions(main, clone)); Preconditions.checkState(!isEquivalentToExcludingFunctions(main, clone));
Expand Down Expand Up @@ -4820,9 +4811,10 @@ private static boolean isEquivalentToExcludingFunctions(
while (thisChild != null && thatChild != null) { while (thisChild != null && thatChild != null) {
if (thisChild.isFunction() || thisChild.isScript()) { if (thisChild.isFunction() || thisChild.isScript()) {
// don't compare function name, parameters or bodies. // don't compare function name, parameters or bodies.
return thatChild.getToken() == thisChild.getToken(); if (thatChild.getToken() != thisChild.getToken()) {
} return false;
if (!isEquivalentToExcludingFunctions(thisChild, thatChild)) { }
} else if (!isEquivalentToExcludingFunctions(thisChild, thatChild)) {
return false; return false;
} }
thisChild = thisChild.getNext(); thisChild = thisChild.getNext();
Expand Down
19 changes: 19 additions & 0 deletions test/com/google/javascript/jscomp/NodeUtilTest.java
Expand Up @@ -32,6 +32,7 @@
import com.google.javascript.rhino.jstype.TernaryValue; import com.google.javascript.rhino.jstype.TernaryValue;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import junit.framework.TestCase; import junit.framework.TestCase;


Expand Down Expand Up @@ -2490,6 +2491,24 @@ public void testIsObjectDefinePropertyDefinition() {
getCallNode("Object.defineProperty();"))); getCallNode("Object.defineProperty();")));
} }


public void testCorrectValidationOfScriptWithChangeAfterFunction() {
Node script = parse("function A() {} if (0) { A(); }");
Preconditions.checkState(script.isScript());

Node clone = script.cloneTree();
Map<Node, Node> mtoc = NodeUtil.mapMainToClone(script, clone);

// Here we make a change in that doesn't change the script node
// child count.
getCallNode(script).detach();

// Mark the script as changed
script.setChangeTime(100);

// will throw if no change is detected.
NodeUtil.verifyScopeChanges("test", mtoc, script);
}

private boolean executedOnceTestCase(String code) { private boolean executedOnceTestCase(String code) {
Node ast = parse(code); Node ast = parse(code);
Node nameNode = getNameNode(ast, "x"); Node nameNode = getNameNode(ast, "x");
Expand Down

0 comments on commit b4aafc1

Please sign in to comment.