Search in sources :

Example 1 with NullCoalescingOpNode

use of com.google.template.soy.exprtree.OperatorNodes.NullCoalescingOpNode 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 2 with NullCoalescingOpNode

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

the class ParseExpressionTest method testParseOperators.

@Test
public void testParseOperators() throws Exception {
    ExprNode expr = assertThatExpression("-11").isValidExpression();
    IntegerNode negInt = (IntegerNode) expr;
    assertThat(negInt.getValue()).isEqualTo(-11);
    expr = assertThatExpression("not false").isValidExpression();
    NotOpNode notOp = (NotOpNode) expr;
    assertThat(((BooleanNode) notOp.getChild(0)).getValue()).isFalse();
    expr = assertThatExpression("90 -14.75").isValidExpression();
    MinusOpNode minusOp = (MinusOpNode) expr;
    assertThat(((IntegerNode) minusOp.getChild(0)).getValue()).isEqualTo(90);
    assertThat(((FloatNode) minusOp.getChild(1)).getValue()).isEqualTo(14.75);
    expr = assertThatExpression("$a or true").isValidExpression();
    OrOpNode orOp = (OrOpNode) expr;
    assertThat(orOp.getChild(0).toSourceString()).isEqualTo("$a");
    assertThat(((BooleanNode) orOp.getChild(1)).getValue()).isTrue();
    expr = assertThatExpression("$a ?: $b ?: $c").isValidExpression();
    NullCoalescingOpNode nullCoalOp0 = (NullCoalescingOpNode) expr;
    assertThat(nullCoalOp0.getChild(0).toSourceString()).isEqualTo("$a");
    NullCoalescingOpNode nullCoalOp1 = (NullCoalescingOpNode) nullCoalOp0.getChild(1);
    assertThat(nullCoalOp1.getChild(0).toSourceString()).isEqualTo("$b");
    assertThat(nullCoalOp1.getChild(1).toSourceString()).isEqualTo("$c");
    expr = assertThatExpression("$a?:$b==null?0*1:0x1").isValidExpression();
    NullCoalescingOpNode nullCoalOp = (NullCoalescingOpNode) expr;
    assertThat(nullCoalOp.getChild(0).toSourceString()).isEqualTo("$a");
    ConditionalOpNode condOp = (ConditionalOpNode) nullCoalOp.getChild(1);
    assertThat(condOp.getChild(0)).isInstanceOf(EqualOpNode.class);
    assertThat(condOp.getChild(1)).isInstanceOf(TimesOpNode.class);
    assertThat(condOp.getChild(2)).isInstanceOf(IntegerNode.class);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) ConditionalOpNode(com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode) NullCoalescingOpNode(com.google.template.soy.exprtree.OperatorNodes.NullCoalescingOpNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) MinusOpNode(com.google.template.soy.exprtree.OperatorNodes.MinusOpNode) OrOpNode(com.google.template.soy.exprtree.OperatorNodes.OrOpNode) FloatNode(com.google.template.soy.exprtree.FloatNode) NotOpNode(com.google.template.soy.exprtree.OperatorNodes.NotOpNode) BooleanNode(com.google.template.soy.exprtree.BooleanNode) Test(org.junit.Test)

Aggregations

NullCoalescingOpNode (com.google.template.soy.exprtree.OperatorNodes.NullCoalescingOpNode)2 BooleanNode (com.google.template.soy.exprtree.BooleanNode)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 FloatNode (com.google.template.soy.exprtree.FloatNode)1 IntegerNode (com.google.template.soy.exprtree.IntegerNode)1 ConditionalOpNode (com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode)1 MinusOpNode (com.google.template.soy.exprtree.OperatorNodes.MinusOpNode)1 NotOpNode (com.google.template.soy.exprtree.OperatorNodes.NotOpNode)1 OrOpNode (com.google.template.soy.exprtree.OperatorNodes.OrOpNode)1 StringNode (com.google.template.soy.exprtree.StringNode)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)1 Test (org.junit.Test)1