Search in sources :

Example 1 with ListLiteralNode

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

the class TemplateAnalysis method isListExpressionEmpty.

// consider moving this to SoyTreeUtils or some similar place.
private static StaticAnalysisResult isListExpressionEmpty(ForNode node) {
    Optional<RangeArgs> rangeArgs = RangeArgs.createFromNode(node);
    if (rangeArgs.isPresent()) {
        return isRangeExpressionEmpty(rangeArgs.get());
    }
    ExprNode expr = node.getExpr().getRoot();
    if (expr instanceof ListLiteralNode) {
        return ((ListLiteralNode) expr).numChildren() > 0 ? StaticAnalysisResult.FALSE : StaticAnalysisResult.TRUE;
    }
    return StaticAnalysisResult.UNKNOWN;
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) ListLiteralNode(com.google.template.soy.exprtree.ListLiteralNode) RangeArgs(com.google.template.soy.shared.RangeArgs)

Example 2 with ListLiteralNode

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

the class ParseExpressionTest method testParseListsAndMaps.

@Test
public void testParseListsAndMaps() throws Exception {
    ExprNode expr = assertThatExpression("[]").isValidExpression();
    assertThat(((ListLiteralNode) expr).numChildren()).isEqualTo(0);
    expr = assertThatExpression("[55]").isValidExpression();
    assertThat(((ListLiteralNode) expr).numChildren()).isEqualTo(1);
    expr = assertThatExpression("[55,]").isValidExpression();
    assertThat(((ListLiteralNode) expr).numChildren()).isEqualTo(1);
    expr = assertThatExpression("['blah', 123, $boo]").isValidExpression();
    assertThat(((ListLiteralNode) expr).numChildren()).isEqualTo(3);
    expr = assertThatExpression("['blah', 123, $boo,]").isValidExpression();
    assertThat(((ListLiteralNode) expr).numChildren()).isEqualTo(3);
    expr = assertThatExpression("[:]").isValidExpression();
    assertThat(((LegacyObjectMapLiteralNode) expr).numChildren()).isEqualTo(0);
    expr = assertThatExpression("['aa': 55]").isValidExpression();
    assertThat(((LegacyObjectMapLiteralNode) expr).numChildren()).isEqualTo(2);
    expr = assertThatExpression("['aa': 55,]").isValidExpression();
    assertThat(((LegacyObjectMapLiteralNode) expr).numChildren()).isEqualTo(2);
    expr = assertThatExpression("['aaa': 'blah', 'bbb': 123, $foo.bar: $boo]").isValidExpression();
    assertThat(((LegacyObjectMapLiteralNode) expr).numChildren()).isEqualTo(6);
    expr = assertThatExpression("['aaa': 'blah', 'bbb': 123, $foo.bar: $boo,]").isValidExpression();
    assertThat(((LegacyObjectMapLiteralNode) expr).numChildren()).isEqualTo(6);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) ListLiteralNode(com.google.template.soy.exprtree.ListLiteralNode) LegacyObjectMapLiteralNode(com.google.template.soy.exprtree.LegacyObjectMapLiteralNode) Test(org.junit.Test)

Example 3 with ListLiteralNode

use of com.google.template.soy.exprtree.ListLiteralNode 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)

Aggregations

ListLiteralNode (com.google.template.soy.exprtree.ListLiteralNode)3 ExprNode (com.google.template.soy.exprtree.ExprNode)2 SourceLocation (com.google.template.soy.base.SourceLocation)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 IntegerNode (com.google.template.soy.exprtree.IntegerNode)1 LegacyObjectMapLiteralNode (com.google.template.soy.exprtree.LegacyObjectMapLiteralNode)1 StringNode (com.google.template.soy.exprtree.StringNode)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 LoggingFunction (com.google.template.soy.logging.LoggingFunction)1 RangeArgs (com.google.template.soy.shared.RangeArgs)1 HtmlAttributeValueNode (com.google.template.soy.soytree.HtmlAttributeValueNode)1 LetContentNode (com.google.template.soy.soytree.LetContentNode)1 PrintNode (com.google.template.soy.soytree.PrintNode)1 RawTextNode (com.google.template.soy.soytree.RawTextNode)1 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)1 Test (org.junit.Test)1