Search in sources :

Example 1 with WithValue

use of com.google.template.soy.jssrc.dsl.CodeChunk.WithValue in project closure-templates by google.

the class TranslateExprNodeVisitor method visitMapLiteralNode.

@Override
protected WithValue visitMapLiteralNode(MapLiteralNode node) {
    // Map literal nodes are much simpler to translate than legacy object map literal nodes.
    // Because they are implemented by ES6 Maps, there is no possibility that JSCompiler
    // will mistakenly rename (or not rename) its keys, and no need to ever quote a key.
    CodeChunk.WithValue map = codeGenerator.declarationBuilder().setRhs(CodeChunk.new_(id("Map")).call()).build().ref();
    ImmutableList.Builder<CodeChunk> setCalls = ImmutableList.builder();
    for (int i = 0; i < node.numChildren(); i += 2) {
        ExprNode keyNode = node.getChild(i);
        // Constructing a map literal with a null key is a runtime error.
        CodeChunk.WithValue key = SOY_CHECK_NOT_NULL.call(genMapKeyCode(keyNode));
        CodeChunk.WithValue value = visit(node.getChild(i + 1));
        setCalls.add(map.dotAccess("set").call(key, value));
    }
    return map.withInitialStatements(setCalls.build());
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue) CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with WithValue

use of com.google.template.soy.jssrc.dsl.CodeChunk.WithValue in project closure-templates by google.

the class ConditionalBuilder method isRepresentableAsTernaryExpression.

private boolean isRepresentableAsTernaryExpression(ImmutableList<IfThenPair> pairs) {
    if (pairs.size() != 1 || trailingElse == null) {
        return false;
    }
    IfThenPair ifThen = Iterables.getOnlyElement(pairs);
    CodeChunk.WithValue predicate = ifThen.predicate;
    CodeChunk consequent = ifThen.consequent;
    return consequent instanceof CodeChunk.WithValue && trailingElse instanceof CodeChunk.WithValue && predicate.initialStatements().containsAll(((WithValue) consequent).initialStatements()) && predicate.initialStatements().containsAll(((WithValue) trailingElse).initialStatements());
}
Also used : WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue) WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue)

Aggregations

WithValue (com.google.template.soy.jssrc.dsl.CodeChunk.WithValue)2 ImmutableList (com.google.common.collect.ImmutableList)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)1