// and remove all children after it, if any. Can stop processing after doing this, because
// the new SwitchDefaultNode is now the last child.
// (b) If the case has all constant exprs and none match: Remove the child.
for (SoyNode child : Lists.newArrayList(node.getChildren()) /*copy*/) {
if (child instanceof SwitchCaseNode) {
SwitchCaseNode caseNode = (SwitchCaseNode) child;
boolean hasMatchingConstant = false;
boolean hasAllNonmatchingConstants = true;
for (ExprRootNode<?> caseExpr : caseNode.getExprList()) {
SoyData caseExprValue = getConstantOrNull(caseExpr);
if (caseExprValue == null) {
hasAllNonmatchingConstants = false;
} else if (caseExprValue.equals(switchExprValue)) {
hasMatchingConstant = true;
hasAllNonmatchingConstants = false;
break;
}
}
if (hasMatchingConstant) {
// ------ Has a constant expr that matches. ------
// Remove all children after this child.
int caseIndex = node.getChildIndex(caseNode);
for (int i = node.numChildren() - 1; i > caseIndex; i--) {
node.removeChild(i);
}
// Replace this child with a new SwitchDefaultNode.
SwitchDefaultNode newDefaultNode = new SwitchDefaultNode(nodeIdGen.genId());
newDefaultNode.addChildren(caseNode.getChildren());
node.replaceChild(caseIndex, newDefaultNode);
// Stop processing.
break;
} else if (hasAllNonmatchingConstants) {