Skip to content

Commit

Permalink
Add a checkState to make it a bit easier to understand what's going o…
Browse files Browse the repository at this point in the history
…n in this pass

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=154975961
  • Loading branch information
tbreisacher authored and brad4d committed May 4, 2017
1 parent f5d74cf commit 25fa111
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/com/google/javascript/jscomp/InlineObjectLiterals.java
Expand Up @@ -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;
Expand Down Expand Up @@ -162,7 +163,7 @@ private boolean isInlinableObject(List<Reference> 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) {
Expand Down Expand Up @@ -273,7 +274,7 @@ private Map<String, String> 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();
Expand All @@ -289,7 +290,7 @@ private Map<String, String> 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();
Expand All @@ -312,7 +313,7 @@ private Map<String, String> computeVarList(
*/
private void fillInitialValues(Reference init, Map<String, Node> 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());
Expand All @@ -330,7 +331,7 @@ private void replaceAssignmentExpression(Var v, Reference ref,
List<Node> nodes = new ArrayList<>();
Node val = ref.getAssignedValue();
blacklistVarReferencesInTree(val, v.scope);
Preconditions.checkState(val.isObjectLit());
checkState(val.isObjectLit(), val);
Set<String> all = new LinkedHashSet<>(varmap.keySet());
for (Node key = val.getFirstChild(); key != null;
key = key.getNext()) {
Expand Down Expand Up @@ -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<String, String> 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);
}

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 25fa111

Please sign in to comment.