From 25fa111ba70ea100dd83ac3ef5d08a1e85fc6ef8 Mon Sep 17 00:00:00 2001 From: tbreisacher Date: Wed, 3 May 2017 10:56:33 -0700 Subject: [PATCH] Add a checkState to make it a bit easier to understand what's going on in this pass ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=154975961 --- .../jscomp/InlineObjectLiterals.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/com/google/javascript/jscomp/InlineObjectLiterals.java b/src/com/google/javascript/jscomp/InlineObjectLiterals.java index e939e007664..99de63608b5 100644 --- a/src/com/google/javascript/jscomp/InlineObjectLiterals.java +++ b/src/com/google/javascript/jscomp/InlineObjectLiterals.java @@ -16,7 +16,8 @@ package com.google.javascript.jscomp; -import com.google.common.base.Preconditions; +import static com.google.common.base.Preconditions.checkState; + import com.google.common.base.Supplier; import com.google.common.collect.Lists; import com.google.javascript.jscomp.ReferenceCollectingCallback.Behavior; @@ -162,7 +163,7 @@ private boolean isInlinableObject(List refs) { // since the function referenced by y might reference 'this'). // if (parent.isGetProp()) { - Preconditions.checkState(parent.getFirstChild() == name); + checkState(parent.getFirstChild() == name); // A call target may be using the object as a 'this' value. if (grandparent.isCall() && grandparent.getFirstChild() == parent) { @@ -273,7 +274,7 @@ private Map computeVarList( if (ref.isLvalue() || ref.isInitializingDeclaration()) { Node val = ref.getAssignedValue(); if (val != null) { - Preconditions.checkState(val.isObjectLit()); + checkState(val.isObjectLit(), val); for (Node child = val.getFirstChild(); child != null; child = child.getNext()) { String varname = child.getString(); @@ -289,7 +290,7 @@ private Map computeVarList( // This is the var. There is no value. } else { Node getprop = ref.getParent(); - Preconditions.checkState(getprop.isGetProp()); + checkState(getprop.isGetProp(), getprop); // The key being looked up in the original map. String varname = getprop.getLastChild().getString(); @@ -312,7 +313,7 @@ private Map computeVarList( */ private void fillInitialValues(Reference init, Map initvals) { Node object = init.getAssignedValue(); - Preconditions.checkState(object.isObjectLit()); + checkState(object.isObjectLit(), object); for (Node key = object.getFirstChild(); key != null; key = key.getNext()) { initvals.put(key.getString(), key.removeFirstChild()); @@ -330,7 +331,7 @@ private void replaceAssignmentExpression(Var v, Reference ref, List nodes = new ArrayList<>(); Node val = ref.getAssignedValue(); blacklistVarReferencesInTree(val, v.scope); - Preconditions.checkState(val.isObjectLit()); + checkState(val.isObjectLit(), val); Set all = new LinkedHashSet<>(varmap.keySet()); for (Node key = val.getFirstChild(); key != null; key = key.getNext()) { @@ -411,17 +412,18 @@ private void splitObject(Var v, Reference init, // Find the beginning of the function / script. vnode = v.getScope().getClosestHoistScope().getRootNode().getLastChild().getFirstChild(); } + checkState(vnode.isVar() || vnode.isExprResult(), vnode); for (Map.Entry entry : varmap.entrySet()) { Node val = initvals.get(entry.getKey()); - Node varnode = NodeUtil.newVarNode(entry.getValue(), val); + Node newVarNode = NodeUtil.newVarNode(entry.getValue(), val); if (val == null) { // is this right? - varnode.useSourceInfoIfMissingFromForTree(vnode); + newVarNode.useSourceInfoIfMissingFromForTree(vnode); } else { blacklistVarReferencesInTree(val, v.scope); } - vnode.getParent().addChildBefore(varnode, vnode); + vnode.getParent().addChildBefore(newVarNode, vnode); compiler.reportChangeToEnclosingScope(vnode); } @@ -449,14 +451,14 @@ private void splitObject(Var v, Reference init, } else { // Make sure that the reference is a GETPROP as we expect it to be. Node getprop = ref.getParent(); - Preconditions.checkState(getprop.isGetProp()); + checkState(getprop.isGetProp(), getprop); // The key being looked up in the original map. String var = getprop.getSecondChild().getString(); // If the variable hasn't already been declared, add an empty // declaration near all the other declarations. - Preconditions.checkState(varmap.containsKey(var)); + checkState(varmap.containsKey(var)); // Replace the GETPROP node with a NAME. Node replacement = IR.name(varmap.get(var));