use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.
the class TranslateExprNodeVisitor method visitV1ExpressionFunction.
private CodeChunk.WithValue visitV1ExpressionFunction(FunctionNode node) {
StringNode expr = (StringNode) node.getChild(0);
JsExpr jsExpr = V1JsExprTranslator.translateToJsExpr(expr.getValue(), expr.getSourceLocation(), variableMappings, errorReporter);
return CodeChunk.fromExpr(jsExpr, ImmutableList.<GoogRequire>of());
}
use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.
the class GenIncrementalDomCodeVisitor method tryGenerateFunctionCall.
/**
* Emits a call to a value of type ATTRIBUTES or HTML, which is actually a JS function. Currently,
* the only supported expressions for this operation are direct variable references and {X ?: ''}.
*
* @param expectedKind The kind of content that the expression must match.
*/
private GenerateFunctionCallResult tryGenerateFunctionCall(SoyType.Kind expectedKind, ExprNode expr) {
IncrementalDomCodeBuilder jsCodeBuilder = getJsCodeBuilder();
if (expr instanceof VarRefNode && expr.getType().getKind() == expectedKind) {
VarRefNode varRefNode = (VarRefNode) expr;
CodeChunk.WithValue call = templateTranslationContext.soyToJsVariableMappings().get(varRefNode.getName()).call();
jsCodeBuilder.append(call);
return GenerateFunctionCallResult.EMITTED;
}
if (!(expr instanceof NullCoalescingOpNode)) {
return GenerateFunctionCallResult.INDIRECT_NODE;
}
// ResolveExpressionTypesVisitor will resolve {$attributes ?: ''} to String because '' is not of
// type ATTRIBUTES. Therefore, we must check the type of the first operand, not the whole node.
NullCoalescingOpNode opNode = (NullCoalescingOpNode) expr;
if (!(opNode.getLeftChild() instanceof VarRefNode) || !(opNode.getRightChild() instanceof StringNode) || opNode.getLeftChild().getType().getKind() != expectedKind) {
return GenerateFunctionCallResult.INDIRECT_NODE;
}
if (!((StringNode) opNode.getRightChild()).getValue().isEmpty()) {
errorReporter.report(expr.getSourceLocation(), NULL_COALESCING_NON_EMPTY);
return GenerateFunctionCallResult.ILLEGAL_NODE;
}
VarRefNode varRefNode = (VarRefNode) opNode.getLeftChild();
CodeChunk.WithValue varName = templateTranslationContext.soyToJsVariableMappings().get(varRefNode.getName());
CodeChunk conditionalCall = CodeChunk.ifStatement(varName, varName.call()).build();
jsCodeBuilder.append(conditionalCall);
return GenerateFunctionCallResult.EMITTED;
}
use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.
the class MsgIdFunctionPass method badFunctionCall.
private void badFunctionCall(FunctionNode fn, String explanation) {
errorReporter.report(fn.getChild(0).getSourceLocation(), MSG_VARIABLE_NOT_IN_SCOPE, explanation);
fn.getParent().replaceChild(fn, new StringNode("error", QuoteStyle.SINGLE, fn.getSourceLocation()));
}
use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.
the class XidPass method run.
@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
for (FunctionNode fn : SoyTreeUtils.getAllNodesOfType(file, FunctionNode.class)) {
if (fn.getSoyFunction() == BuiltinFunction.XID) {
if (fn.numChildren() != 1) {
// if it isn't == 1, then an error has already been reported, move along.
continue;
}
ExprNode child = fn.getChild(0);
switch(child.getKind()) {
case GLOBAL_NODE:
GlobalNode global = (GlobalNode) child;
if (global.isResolved()) {
// This doesn't have to be an error. but it is confusing if it is is since it is
// unclear if the user intended to xid the identifier or the value.
reporter.report(global.getSourceLocation(), GLOBAL_XID_ARG_IS_RESOLVED, global.getType().toString(), global.getValue().toSourceString());
}
fn.replaceChild(0, new StringNode(global.getName(), QuoteStyle.SINGLE, global.getSourceLocation()));
break;
case STRING_NODE:
break;
default:
reporter.report(child.getSourceLocation(), STRING_OR_GLOBAL_REQUIRED);
}
}
}
}
use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.
the class TemplateDelegateNodeBuilder method setCmdTextInfo.
/**
* Alternative to {@code setCmdText()} that sets command text info directly as opposed to having
* it parsed from the command text string. The cmdText field will be set to a canonical string
* generated from the given info.
*
* @param delTemplateName The delegate template name.
* @param delTemplateVariant The delegate template variant.
* @param delPriority The delegate priority.
* @param autoescapeMode The mode of autoescaping for this template.
* @param contentKind Strict mode context. Nonnull iff autoescapeMode is strict.
* @param requiredCssNamespaces CSS namespaces required to render the template.
* @return This builder.
*/
public TemplateDelegateNodeBuilder setCmdTextInfo(String delTemplateName, String delTemplateVariant, Priority delPriority, AutoescapeMode autoescapeMode, SanitizedContentKind contentKind, ImmutableList<String> requiredCssNamespaces) {
Preconditions.checkState(this.sourceLocation != null);
Preconditions.checkState(this.cmdText == null);
Preconditions.checkArgument(BaseUtils.isDottedIdentifier(delTemplateName));
Preconditions.checkArgument(delTemplateVariant.length() == 0 || BaseUtils.isIdentifier(delTemplateVariant));
Preconditions.checkArgument((contentKind != null) == (autoescapeMode == AutoescapeMode.STRICT));
this.delTemplateName = delTemplateName;
this.delTemplateVariantExpr = new ExprRootNode(new StringNode(delTemplateVariant, QuoteStyle.SINGLE, this.sourceLocation));
this.delPriority = delPriority;
setAutoescapeInfo(autoescapeMode, contentKind, sourceLocation);
setRequiredCssNamespaces(requiredCssNamespaces);
String cmdText = delTemplateName + ((delTemplateVariant.length() == 0) ? "" : " variant=\"" + delTemplateVariant + "\"") + " autoescape=\"" + autoescapeMode.getAttributeValue() + "\"";
if (contentKind != null) {
cmdText += " kind=\"" + contentKind.asAttributeValue() + '"';
}
if (!requiredCssNamespaces.isEmpty()) {
cmdText += " requirecss=\"" + Joiner.on(", ").join(requiredCssNamespaces) + "\"";
}
this.cmdText = cmdText;
genInternalTemplateNameHelper();
return this;
}
Aggregations