Skip to content

Commit

Permalink
Update checkState calls to give better information in case they fail
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=154995451
  • Loading branch information
tbreisacher authored and brad4d committed May 4, 2017
1 parent 5b74485 commit a886c62
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/com/google/javascript/jscomp/InlineObjectLiterals.java
Expand Up @@ -16,7 +16,8 @@


package com.google.javascript.jscomp; 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.base.Supplier;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.javascript.jscomp.ReferenceCollectingCallback.Behavior; import com.google.javascript.jscomp.ReferenceCollectingCallback.Behavior;
Expand Down Expand Up @@ -162,7 +163,7 @@ private boolean isInlinableObject(List<Reference> refs) {
// since the function referenced by y might reference 'this'). // since the function referenced by y might reference 'this').
// //
if (parent.isGetProp()) { if (parent.isGetProp()) {
Preconditions.checkState(parent.getFirstChild() == name); checkState(parent.getFirstChild() == name);
// A call target may be using the object as a 'this' value. // A call target may be using the object as a 'this' value.
if (grandparent.isCall() if (grandparent.isCall()
&& grandparent.getFirstChild() == parent) { && grandparent.getFirstChild() == parent) {
Expand Down Expand Up @@ -273,7 +274,7 @@ private Map<String, String> computeVarList(
if (ref.isLvalue() || ref.isInitializingDeclaration()) { if (ref.isLvalue() || ref.isInitializingDeclaration()) {
Node val = ref.getAssignedValue(); Node val = ref.getAssignedValue();
if (val != null) { if (val != null) {
Preconditions.checkState(val.isObjectLit()); checkState(val.isObjectLit(), val);
for (Node child = val.getFirstChild(); child != null; for (Node child = val.getFirstChild(); child != null;
child = child.getNext()) { child = child.getNext()) {
String varname = child.getString(); String varname = child.getString();
Expand All @@ -289,7 +290,7 @@ private Map<String, String> computeVarList(
// This is the var. There is no value. // This is the var. There is no value.
} else { } else {
Node getprop = ref.getParent(); Node getprop = ref.getParent();
Preconditions.checkState(getprop.isGetProp()); checkState(getprop.isGetProp(), getprop);


// The key being looked up in the original map. // The key being looked up in the original map.
String varname = getprop.getLastChild().getString(); String varname = getprop.getLastChild().getString();
Expand All @@ -312,7 +313,7 @@ private Map<String, String> computeVarList(
*/ */
private void fillInitialValues(Reference init, Map<String, Node> initvals) { private void fillInitialValues(Reference init, Map<String, Node> initvals) {
Node object = init.getAssignedValue(); Node object = init.getAssignedValue();
Preconditions.checkState(object.isObjectLit()); checkState(object.isObjectLit(), object);
for (Node key = object.getFirstChild(); key != null; for (Node key = object.getFirstChild(); key != null;
key = key.getNext()) { key = key.getNext()) {
initvals.put(key.getString(), key.removeFirstChild()); initvals.put(key.getString(), key.removeFirstChild());
Expand All @@ -330,7 +331,7 @@ private void replaceAssignmentExpression(Var v, Reference ref,
List<Node> nodes = new ArrayList<>(); List<Node> nodes = new ArrayList<>();
Node val = ref.getAssignedValue(); Node val = ref.getAssignedValue();
blacklistVarReferencesInTree(val, v.scope); blacklistVarReferencesInTree(val, v.scope);
Preconditions.checkState(val.isObjectLit()); checkState(val.isObjectLit(), val);
Set<String> all = new LinkedHashSet<>(varmap.keySet()); Set<String> all = new LinkedHashSet<>(varmap.keySet());
for (Node key = val.getFirstChild(); key != null; for (Node key = val.getFirstChild(); key != null;
key = key.getNext()) { key = key.getNext()) {
Expand Down Expand Up @@ -414,14 +415,14 @@ private void splitObject(Var v, Reference init,


for (Map.Entry<String, String> entry : varmap.entrySet()) { for (Map.Entry<String, String> entry : varmap.entrySet()) {
Node val = initvals.get(entry.getKey()); Node val = initvals.get(entry.getKey());
Node varnode = NodeUtil.newVarNode(entry.getValue(), val); Node newVarNode = NodeUtil.newVarNode(entry.getValue(), val);
if (val == null) { if (val == null) {
// is this right? // is this right?
varnode.useSourceInfoIfMissingFromForTree(vnode); newVarNode.useSourceInfoIfMissingFromForTree(vnode);
} else { } else {
blacklistVarReferencesInTree(val, v.scope); blacklistVarReferencesInTree(val, v.scope);
} }
vnode.getParent().addChildBefore(varnode, vnode); vnode.getParent().addChildBefore(newVarNode, vnode);
compiler.reportChangeToEnclosingScope(vnode); compiler.reportChangeToEnclosingScope(vnode);
} }


Expand Down Expand Up @@ -449,14 +450,14 @@ private void splitObject(Var v, Reference init,
} else { } else {
// Make sure that the reference is a GETPROP as we expect it to be. // Make sure that the reference is a GETPROP as we expect it to be.
Node getprop = ref.getParent(); Node getprop = ref.getParent();
Preconditions.checkState(getprop.isGetProp()); checkState(getprop.isGetProp(), getprop);


// The key being looked up in the original map. // The key being looked up in the original map.
String var = getprop.getSecondChild().getString(); String var = getprop.getSecondChild().getString();


// If the variable hasn't already been declared, add an empty // If the variable hasn't already been declared, add an empty
// declaration near all the other declarations. // declaration near all the other declarations.
Preconditions.checkState(varmap.containsKey(var)); checkState(varmap.containsKey(var));


// Replace the GETPROP node with a NAME. // Replace the GETPROP node with a NAME.
Node replacement = IR.name(varmap.get(var)); Node replacement = IR.name(varmap.get(var));
Expand Down

0 comments on commit a886c62

Please sign in to comment.