Search in sources :

Example 6 with TemplateBasicNode

use of com.google.template.soy.soytree.TemplateBasicNode in project closure-templates by google.

the class ContextualAutoescaper method findTemplates.

/**
 * Fills in the {@link Inferences} template name to node map.
 *
 * @param files Modified in place.
 */
private static ImmutableListMultimap<String, TemplateNode> findTemplates(Iterable<? extends SoyFileNode> files) {
    ImmutableListMultimap.Builder<String, TemplateNode> builder = ImmutableListMultimap.builder();
    for (SoyFileNode file : files) {
        for (TemplateNode template : file.getChildren()) {
            String templateName;
            if (template instanceof TemplateBasicNode) {
                templateName = template.getTemplateName();
            } else {
                templateName = ((TemplateDelegateNode) template).getDelTemplateName();
            }
            builder.put(templateName, template);
        }
    }
    return builder.build();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateBasicNode(com.google.template.soy.soytree.TemplateBasicNode) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 7 with TemplateBasicNode

use of com.google.template.soy.soytree.TemplateBasicNode in project closure-templates by google.

the class AliasUtils method createTemplateAliases.

/**
 * Creates a mapping for assigning and looking up the variable alias for templates both within a
 * file and referenced from external files. For local files, the alias is just the local
 * template's name. For external templates, the alias is an identifier that is unique for that
 * template.
 *
 * <p>For V1 templates, no alias is generated and the template should be called by the fully
 * qualified name.
 *
 * @param fileNode The {@link SoyFileNode} to create an alias mapping for.
 * @return A {@link TemplateAliases} to look up aliases with.
 */
static TemplateAliases createTemplateAliases(SoyFileNode fileNode) {
    Map<String, String> aliasMap = new HashMap<>();
    Set<String> localTemplates = new HashSet<>();
    int counter = 0;
    // Go through templates first and just alias them to their local name.
    for (TemplateBasicNode templateBasicNode : SoyTreeUtils.getAllNodesOfType(fileNode, TemplateBasicNode.class)) {
        String partialName = templateBasicNode.getPartialTemplateName();
        String fullyQualifiedName = templateBasicNode.getTemplateName();
        localTemplates.add(fullyQualifiedName);
        Preconditions.checkState(partialName != null, "Aliasing not supported for V1 templates");
        // Need to start the alias with something that cannot be a part of a reserved
        // JavaScript identifier like 'function' or 'catch'.
        String alias = "$" + partialName.substring(1);
        aliasMap.put(fullyQualifiedName, alias);
    }
    // Go through all call sites looking for foreign template calls and create an alias for them.
    for (CallBasicNode callBasicNode : SoyTreeUtils.getAllNodesOfType(fileNode, CallBasicNode.class)) {
        String fullyQualifiedName = callBasicNode.getCalleeName();
        // to a local template.
        if (localTemplates.contains(fullyQualifiedName) || aliasMap.containsKey(fullyQualifiedName)) {
            continue;
        }
        String alias = TEMPLATE_ALIAS_PREFIX + (++counter);
        aliasMap.put(fullyQualifiedName, alias);
    }
    return new Aliases(aliasMap);
}
Also used : TemplateBasicNode(com.google.template.soy.soytree.TemplateBasicNode) HashMap(java.util.HashMap) CallBasicNode(com.google.template.soy.soytree.CallBasicNode) HashSet(java.util.HashSet)

Example 8 with TemplateBasicNode

use of com.google.template.soy.soytree.TemplateBasicNode in project closure-templates by google.

the class FindTransitiveDepTemplatesVisitor method visitCallBasicNode.

@Override
protected void visitCallBasicNode(CallBasicNode node) {
    // Don't forget to visit content within CallParamContentNodes.
    visitChildren(node);
    TemplateBasicNode callee = templateRegistry.getBasicTemplate(node.getCalleeName());
    // If the callee is null (i.e. not within the Soy file set), then this is an external call.
    if (callee == null) {
        return;
    }
    // Visit the callee template.
    processCalleeHelper(callee);
}
Also used : TemplateBasicNode(com.google.template.soy.soytree.TemplateBasicNode)

Example 9 with TemplateBasicNode

use of com.google.template.soy.soytree.TemplateBasicNode in project closure-templates by google.

the class FindIndirectParamsVisitor method visitCallBasicNode.

@Override
protected void visitCallBasicNode(CallBasicNode node) {
    // Don't forget to visit content within CallParamContentNodes.
    visitChildren(node);
    // We only want to recurse on calls that pass all data.
    if (!node.isPassingAllData()) {
        return;
    }
    TemplateBasicNode callee = templateRegistry.getBasicTemplate(node.getCalleeName());
    // Soy file set.
    if (callee == null) {
        mayHaveIndirectParamsInExternalCalls = true;
        return;
    }
    // Visit the callee template.
    visitCalleeHelper(node, callee);
}
Also used : TemplateBasicNode(com.google.template.soy.soytree.TemplateBasicNode)

Aggregations

TemplateBasicNode (com.google.template.soy.soytree.TemplateBasicNode)9 TemplateNode (com.google.template.soy.soytree.TemplateNode)3 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)3 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 IndentedLinesBuilder (com.google.template.soy.base.internal.IndentedLinesBuilder)1 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)1 GlobalNode (com.google.template.soy.exprtree.GlobalNode)1 ProtoInitNode (com.google.template.soy.exprtree.ProtoInitNode)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 CssTagsPrefixPresence (com.google.template.soy.parseinfo.SoyFileInfo.CssTagsPrefixPresence)1 IndirectParamsInfo (com.google.template.soy.passes.FindIndirectParamsVisitor.IndirectParamsInfo)1 CallBasicNode (com.google.template.soy.soytree.CallBasicNode)1 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)1