use of com.google.template.soy.jssrc.dsl.ConditionalBuilder in project closure-templates by google.
the class GenJsCodeVisitorAssistantForMsgs method generateMsgGroupVariable.
/**
* Returns a code chunk representing a variable declaration for an {@link MsgFallbackGroupNode}
* that contains fallback(s).
*/
private CodeChunk.WithValue generateMsgGroupVariable(MsgFallbackGroupNode node, String tmpVarName) {
checkState(node.numChildren() == 2);
// Generate the goog.getMsg calls for all children.
GoogMsgCodeGenInfo primaryCodeGenInfo = genGoogGetMsgCallHelper(buildGoogMsgVarNameHelper(node.getChild(0)), node.getChild(0));
GoogMsgCodeGenInfo fallbackCodeGenInfo = genGoogGetMsgCallHelper(buildGoogMsgVarNameHelper(node.getChild(1)), node.getChild(1));
// Declare a temporary variable to hold the getMsgWithFallback() call so that we can apply any
// MessageFormats from any of the fallbacks. This is also the variable name that we return to
// the caller.
CodeChunk.WithValue selectedMsg = VariableDeclaration.builder(tmpVarName).setRhs(CodeChunk.dottedIdNoRequire("goog.getMsgWithFallback").call(primaryCodeGenInfo.googMsgVar, fallbackCodeGenInfo.googMsgVar)).build().ref();
// We use id() here instead of using the corresponding code chunks because the stupid
// jscodebuilder system causes us to regenerate the msg vars multiple times because it doesn't
// detect that they were already generated.
// TODO(b/33382980): clean this up
CodeChunk.WithValue isPrimaryMsgInUse = CodeChunk.id(tmpVarName).doubleEquals(CodeChunk.id(primaryCodeGenInfo.googMsgVarName));
translationContext.soyToJsVariableMappings().setIsPrimaryMsgInUse(node, isPrimaryMsgInUse);
if (primaryCodeGenInfo.placeholders == null && fallbackCodeGenInfo.placeholders == null) {
// all placeholders have already been substituted, just return
return selectedMsg;
}
// Generate the goog.i18n.MessageFormat calls for child plural/select messages (if any), each
// wrapped in an if-block that will only execute if that child is the chosen message.
CodeChunk condition;
if (primaryCodeGenInfo.placeholders != null) {
ConditionalBuilder builder = CodeChunk.ifStatement(selectedMsg.doubleEquals(primaryCodeGenInfo.googMsgVar), selectedMsg.assign(getMessageFormatCall(primaryCodeGenInfo)));
if (fallbackCodeGenInfo.placeholders != null) {
builder.else_(selectedMsg.assign(getMessageFormatCall(fallbackCodeGenInfo)));
}
condition = builder.build();
} else {
condition = CodeChunk.ifStatement(selectedMsg.doubleEquals(fallbackCodeGenInfo.googMsgVar), selectedMsg.assign(getMessageFormatCall(fallbackCodeGenInfo))).build();
}
return CodeChunk.id(tmpVarName).withInitialStatement(condition);
}
use of com.google.template.soy.jssrc.dsl.ConditionalBuilder in project closure-templates by google.
the class GenJsCodeVisitor method generateNonExpressionIfNode.
/**
* Generates the JavaScript code for an {if} block that cannot be done as an expression.
*
* <p>TODO(user): Instead of interleaving JsCodeBuilders like this, consider refactoring
* GenJsCodeVisitor to return CodeChunks for each sub-Template level SoyNode.
*/
protected void generateNonExpressionIfNode(IfNode node) {
ConditionalBuilder conditional = null;
for (SoyNode child : node.getChildren()) {
if (child instanceof IfCondNode) {
IfCondNode condNode = (IfCondNode) child;
// Convert predicate.
CodeChunk.WithValue predicate = translateExpr(condNode.getExpr());
// Convert body.
CodeChunk consequent = visitChildrenReturningCodeChunk(condNode);
// Add if-block to conditional.
if (conditional == null) {
conditional = ifStatement(predicate, consequent);
} else {
conditional.elseif_(predicate, consequent);
}
} else if (child instanceof IfElseNode) {
// Convert body.
CodeChunk trailingElse = visitChildrenReturningCodeChunk((IfElseNode) child);
// Add else-block to conditional.
conditional.else_(trailingElse);
} else {
throw new AssertionError();
}
}
jsCodeBuilder.append(conditional.build());
}
Aggregations