Search in sources :

Example 1 with StringNode

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());
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) StringNode(com.google.template.soy.exprtree.StringNode)

Example 2 with StringNode

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;
}
Also used : NullCoalescingOpNode(com.google.template.soy.exprtree.OperatorNodes.NullCoalescingOpNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) StringNode(com.google.template.soy.exprtree.StringNode)

Example 3 with StringNode

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()));
}
Also used : StringNode(com.google.template.soy.exprtree.StringNode)

Example 4 with StringNode

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);
            }
        }
    }
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode) GlobalNode(com.google.template.soy.exprtree.GlobalNode)

Example 5 with StringNode

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;
}
Also used : StringNode(com.google.template.soy.exprtree.StringNode) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Aggregations

StringNode (com.google.template.soy.exprtree.StringNode)20 Test (org.junit.Test)9 ExprNode (com.google.template.soy.exprtree.ExprNode)7 FunctionNode (com.google.template.soy.exprtree.FunctionNode)7 IntegerNode (com.google.template.soy.exprtree.IntegerNode)6 PrintNode (com.google.template.soy.soytree.PrintNode)6 GlobalNode (com.google.template.soy.exprtree.GlobalNode)4 TemplateNode (com.google.template.soy.soytree.TemplateNode)4 BooleanNode (com.google.template.soy.exprtree.BooleanNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 FloatNode (com.google.template.soy.exprtree.FloatNode)2 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)2 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)2 ImmutableList (com.google.common.collect.ImmutableList)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 NullData (com.google.template.soy.data.restricted.NullData)1 PrimitiveNode (com.google.template.soy.exprtree.ExprNode.PrimitiveNode)1 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)1 ListLiteralNode (com.google.template.soy.exprtree.ListLiteralNode)1