Search in sources :

Example 11 with VarRefNode

use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.

the class ExpressionSubject method isValidVarNamed.

void isValidVarNamed(String name) {
    VarRefNode varNode = (VarRefNode) parseExpression();
    assertThat(errorReporter.hasErrors()).isFalse();
    String actualName = varNode.getName();
    if (!actualName.equals(name)) {
        failWithBadResults("is var named", name, "is named", actualName);
    }
    if (!varNode.toSourceString().equals("$" + name)) {
        failWithBadResults("has sourceString", "$" + name, "is named", varNode.toSourceString());
    }
}
Also used : VarRefNode(com.google.template.soy.exprtree.VarRefNode)

Example 12 with VarRefNode

use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.

the class SharedTestUtils method createTemplateBodyForExpression.

/**
 * Returns a template body for the given soy expression. With type specializations.
 */
public static String createTemplateBodyForExpression(String soyExpr, final Map<String, SoyType> typeMap) {
    ExprNode expr = SoyFileParser.parseExpression(soyExpr, PluginResolver.nullResolver(Mode.ALLOW_UNDEFINED, ErrorReporter.exploding()), ErrorReporter.exploding());
    final Set<String> loopVarNames = new HashSet<>();
    final Set<String> names = new HashSet<>();
    new AbstractExprNodeVisitor<Void>() {

        @Override
        protected void visitVarRefNode(VarRefNode node) {
            if (!node.isDollarSignIjParameter()) {
                names.add(node.getName());
            }
        }

        @Override
        protected void visitFunctionNode(FunctionNode node) {
            switch(node.getFunctionName()) {
                case "index":
                case "isFirst":
                case "isLast":
                    loopVarNames.add(((VarRefNode) node.getChild(0)).getName());
                    break;
                // fall out
                default:
            }
            visitChildren(node);
        }

        @Override
        protected void visitExprNode(ExprNode node) {
            if (node instanceof ParentExprNode) {
                visitChildren((ParentExprNode) node);
            }
        }
    }.exec(expr);
    final StringBuilder templateBody = new StringBuilder();
    for (String varName : Sets.difference(names, loopVarNames)) {
        SoyType type = typeMap.get(varName);
        if (type == null) {
            type = UnknownType.getInstance();
        }
        templateBody.append("{@param " + varName + ": " + type + "}\n");
    }
    String contents = "{" + soyExpr + "}\n";
    for (String loopVar : loopVarNames) {
        contents = "{for $" + loopVar + " in [null]}\n" + contents + "\n{/for}";
    }
    templateBody.append(contents);
    return templateBody.toString();
}
Also used : ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) SoyType(com.google.template.soy.types.SoyType) FunctionNode(com.google.template.soy.exprtree.FunctionNode) HashSet(java.util.HashSet)

Example 13 with VarRefNode

use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.

the class VeLogInstrumentationVisitor method visitHtmlAttributeNode.

/**
 * For HtmlAttributeNode that has a logging function as its value, replace the logging function
 * with its place holder, and append a new data attribute that contains all the desired
 * information that are used later by the runtime library.
 */
@Override
protected void visitHtmlAttributeNode(HtmlAttributeNode node) {
    // Skip attributes that do not have a value.
    if (!node.hasValue()) {
        return;
    }
    SourceLocation insertionLocation = node.getSourceLocation();
    for (FunctionNode function : SoyTreeUtils.getAllNodesOfType(node, FunctionNode.class)) {
        if (!(function.getSoyFunction() instanceof LoggingFunction)) {
            continue;
        }
        FunctionNode funcNode = new FunctionNode(VeLogJsSrcLoggingFunction.INSTANCE, insertionLocation);
        funcNode.addChild(new StringNode(function.getFunctionName(), QuoteStyle.SINGLE, insertionLocation));
        funcNode.addChild(new ListLiteralNode(function.getChildren(), insertionLocation));
        StandaloneNode attributeName = node.getChild(0);
        if (attributeName instanceof RawTextNode) {
            // If attribute name is a plain text, directly pass it as a function argument.
            funcNode.addChild(new StringNode(((RawTextNode) attributeName).getRawText(), QuoteStyle.SINGLE, insertionLocation));
        } else {
            // Otherwise wrap the print node or call node into a let block, and use the let variable
            // as a function argument.
            String varName = "soy_logging_function_attribute_" + counter;
            LetContentNode letNode = LetContentNode.forVariable(nodeIdGen.genId(), attributeName.getSourceLocation(), varName, null);
            // Adds a let var which references to the original attribute name, and move the name to
            // the let block.
            node.replaceChild(attributeName, new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
            true, /* expr= */
            new VarRefNode(varName, insertionLocation, false, letNode.getVar()), /* attributes= */
            ImmutableList.of(), ErrorReporter.exploding()));
            letNode.addChild(attributeName);
            node.getParent().addChild(node.getParent().getChildIndex(node), letNode);
            funcNode.addChild(new VarRefNode(varName, insertionLocation, false, letNode.getVar()));
        }
        funcNode.addChild(new IntegerNode(counter++, insertionLocation));
        PrintNode loggingFunctionAttribute = new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
        true, /* expr= */
        funcNode, /* attributes= */
        ImmutableList.of(), ErrorReporter.exploding());
        // Append the logging function attribute to its parent
        int appendIndex = node.getParent().getChildIndex(node) + 1;
        node.getParent().addChild(appendIndex, loggingFunctionAttribute);
        // Replace the original attribute value to the placeholder.
        HtmlAttributeValueNode placeHolder = new HtmlAttributeValueNode(nodeIdGen.genId(), insertionLocation, Quotes.DOUBLE);
        placeHolder.addChild(new RawTextNode(nodeIdGen.genId(), ((LoggingFunction) function.getSoyFunction()).getPlaceholder(), insertionLocation));
        node.replaceChild(node.getChild(1), placeHolder);
        // logging function in a html attribute value.
        break;
    }
    visitChildren(node);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) ListLiteralNode(com.google.template.soy.exprtree.ListLiteralNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) StringNode(com.google.template.soy.exprtree.StringNode) LoggingFunction(com.google.template.soy.logging.LoggingFunction) PrintNode(com.google.template.soy.soytree.PrintNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) LetContentNode(com.google.template.soy.soytree.LetContentNode) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode)

Example 14 with VarRefNode

use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.

the class EvalVisitor method visitIsFirstFunction.

private SoyValue visitIsFirstFunction(FunctionNode node) {
    int localVarIndex;
    try {
        VarRefNode dataRef = (VarRefNode) node.getChild(0);
        localVarIndex = env.getIndex((LoopVar) dataRef.getDefnDecl());
    } catch (Exception e) {
        throw RenderException.create("Failed to evaluate function call " + node.toSourceString() + ".", e);
    }
    return convertResult(localVarIndex == 0);
}
Also used : LoopVar(com.google.template.soy.soytree.defn.LoopVar) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IOException(java.io.IOException) SoyDataException(com.google.template.soy.data.SoyDataException)

Example 15 with VarRefNode

use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.

the class EvalVisitor method visitIsLastFunction.

private SoyValue visitIsLastFunction(FunctionNode node) {
    boolean isLast;
    try {
        VarRefNode dataRef = (VarRefNode) node.getChild(0);
        isLast = env.isLast((LoopVar) dataRef.getDefnDecl());
    } catch (Exception e) {
        throw RenderException.create("Failed to evaluate function call " + node.toSourceString() + ".", e);
    }
    return convertResult(isLast);
}
Also used : LoopVar(com.google.template.soy.soytree.defn.LoopVar) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IOException(java.io.IOException) SoyDataException(com.google.template.soy.data.SoyDataException)

Aggregations

VarRefNode (com.google.template.soy.exprtree.VarRefNode)22 Test (org.junit.Test)9 SourceLocation (com.google.template.soy.base.SourceLocation)5 ExprNode (com.google.template.soy.exprtree.ExprNode)4 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)4 IntegerNode (com.google.template.soy.exprtree.IntegerNode)4 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)4 SoyDataException (com.google.template.soy.data.SoyDataException)3 PrintNode (com.google.template.soy.soytree.PrintNode)3 TemplateNode (com.google.template.soy.soytree.TemplateNode)3 LoopVar (com.google.template.soy.soytree.defn.LoopVar)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)2 FunctionNode (com.google.template.soy.exprtree.FunctionNode)2 GlobalNode (com.google.template.soy.exprtree.GlobalNode)2 ItemAccessNode (com.google.template.soy.exprtree.ItemAccessNode)2 StringNode (com.google.template.soy.exprtree.StringNode)2 IfCondNode (com.google.template.soy.soytree.IfCondNode)2 IfNode (com.google.template.soy.soytree.IfNode)2