Search in sources :

Example 6 with TemplateParam

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

the class Inferences method cloneTemplates.

/**
 * Clones a template, changing the name.
 *
 * @return A copy of tn, differing semantically only in name and auto-generated IDs. The new
 *     templates will be available via {@link #lookupTemplates} with the given name.
 */
public List<TemplateNode> cloneTemplates(String baseName, String derivedName, CallNode callNode) {
    if (!lookupTemplates(derivedName).isEmpty()) {
        throw new AssertionError(derivedName + " already has templates: " + lookupTemplates(derivedName));
    }
    ImmutableList.Builder<TemplateNode> b = ImmutableList.builder();
    for (TemplateNode tn : lookupTemplates(baseName)) {
        if (SoyTreeUtils.hasHtmlNodes(tn)) {
            throw SoyAutoescapeException.createWithNode("Non-strict template '" + baseName + "' contains HTML nodes but does not specify the kind. " + "This is no longer allowed, please migrate the template to strict and " + "specify a content kind by adding a kind attribute", callNode);
        }
        SoyFileHeaderInfo soyFileHeaderInfo = tn.getSoyFileHeaderInfo();
        // We trivially clone the template with new ids, this ensures that all the varrefs have proper
        // vardefns assigned.  Then we manually recopy to a new template in order to modify the name.
        // TODO(lukes): we should add direct support for swapping template names to TemplateNode
        // or just eliminate usecases for this method.
        TemplateNode trivialClonedTemplate = SoyTreeUtils.cloneWithNewIds(tn, idGen);
        int cloneId = trivialClonedTemplate.getId();
        // We need to use the unnamespaced name in the command text since we'll be inserting this
        // template into a file node that already has a namespace declaration.
        TemplateNode clone;
        if (tn instanceof TemplateBasicNode) {
            String derivedPartialName = (tn.getPartialTemplateName() != null) ? derivedName.substring(soyFileHeaderInfo.namespace.length()) : null;
            clone = new TemplateBasicNodeBuilder(soyFileHeaderInfo, ErrorReporter.exploding()).setId(cloneId).setSourceLocation(tn.getSourceLocation()).setCmdTextInfo(derivedName, derivedPartialName, tn.getVisibility(), tn.getAutoescapeMode(), tn.getContentKind(), tn.getRequiredCssNamespaces()).addParams(trivialClonedTemplate.getAllParams()).build();
            if (!(derivedName.equals(clone.getTemplateName()) && Objects.equals(derivedPartialName, clone.getPartialTemplateName()))) {
                throw new AssertionError();
            }
        } else if (tn instanceof TemplateDelegateNode) {
            TemplateDelegateNode tdn = (TemplateDelegateNode) tn;
            clone = new TemplateDelegateNodeBuilder(soyFileHeaderInfo, ErrorReporter.exploding()).setId(cloneId).setSourceLocation(tn.getSourceLocation()).setCmdTextInfo(derivedName, tdn.getDelTemplateVariant(), tdn.getDelPriority(), tn.getAutoescapeMode(), tn.getContentKind(), tn.getRequiredCssNamespaces()).addParams(trivialClonedTemplate.getAllParams()).build();
            if (!(derivedName.equals(((TemplateDelegateNode) clone).getDelTemplateName()))) {
                throw new AssertionError();
            }
        } else {
            throw new AssertionError("Unknown template node type: " + tn.getClass());
        }
        clone.addChildren(trivialClonedTemplate.getChildren());
        // Reassign all the local variable data which isn't maintained by the cloning process above.
        clone.setMaxLocalVariableTableSize(tn.getMaxLocalVariableTableSize());
        Iterator<TemplateParam> tnIterator = tn.getAllParams().iterator();
        Iterator<TemplateParam> cloneIterator = clone.getAllParams().iterator();
        while (tnIterator.hasNext()) {
            cloneIterator.next().setLocalVariableIndex(tnIterator.next().localVariableIndex());
        }
        b.add(clone);
    }
    ImmutableList<TemplateNode> clones = b.build();
    templatesByName.putAll(derivedName, clones);
    return clones;
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) TemplateBasicNode(com.google.template.soy.soytree.TemplateBasicNode) TemplateDelegateNodeBuilder(com.google.template.soy.soytree.TemplateDelegateNodeBuilder) ImmutableList(com.google.common.collect.ImmutableList) TemplateBasicNodeBuilder(com.google.template.soy.soytree.TemplateBasicNodeBuilder) SoyFileHeaderInfo(com.google.template.soy.soytree.TemplateNode.SoyFileHeaderInfo) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam)

Example 7 with TemplateParam

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

the class FindIjParamsVisitor method exec.

/**
 * Computes injected params info for a template.
 *
 * <p>Note: This method is not thread-safe. If you need to get injected params info in a
 * thread-safe manner, then please use {@link #execOnAllTemplates}() in a thread-safe manner.
 */
public IjParamsInfo exec(TemplateNode rootTemplate) {
    TransitiveDepTemplatesInfo depsInfo = findTransitiveDepTemplatesVisitor.exec(rootTemplate);
    if (!depsInfoToIjParamsInfoMap.containsKey(depsInfo)) {
        ImmutableMultimap.Builder<String, TemplateNode> ijParamToCalleesMultimapBuilder = ImmutableMultimap.builder();
        for (TemplateNode template : depsInfo.depTemplateSet) {
            if (!templateToLocalIjParamsMap.containsKey(template)) {
                templateToLocalIjParamsMap.put(template, getAllIjs(template));
            }
            for (String localIjParam : templateToLocalIjParamsMap.get(template)) {
                ijParamToCalleesMultimapBuilder.put(localIjParam, template);
            }
            for (TemplateParam injectedParam : template.getInjectedParams()) {
                ijParamToCalleesMultimapBuilder.put(injectedParam.name(), template);
            }
        }
        IjParamsInfo ijParamsInfo = new IjParamsInfo(ijParamToCalleesMultimapBuilder.build());
        depsInfoToIjParamsInfoMap.put(depsInfo, ijParamsInfo);
    }
    return depsInfoToIjParamsInfoMap.get(depsInfo);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TransitiveDepTemplatesInfo(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam)

Example 8 with TemplateParam

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

the class FindIndirectParamsVisitor method visitTemplateNode.

// -----------------------------------------------------------------------------------------------
// Implementations for specific nodes.
@Override
protected void visitTemplateNode(TemplateNode node) {
    if (isStartOfPass) {
        isStartOfPass = false;
    } else {
        // Add the params listed by this template.
        List<TemplateParam> params = node.getParams();
        for (TemplateParam param : params) {
            if (callerStack.peek().allCallParamKeys.contains(param.name())) {
                // param is actually not being passed by data="all"
                continue;
            }
            if (!indirectParams.containsKey(param.name())) {
                indirectParams.put(param.name(), param);
            }
            paramKeyToCalleesMultimap.put(param.name(), node);
            Preconditions.checkNotNull(param.type());
            indirectParamTypes.put(param.name(), param.type());
        }
        if (!node.couldHaveSyntaxVersionAtLeast(SyntaxVersion.V2_0)) {
            // V1 templates don't need to declare all params.
            mayHaveIndirectParamsInExternalCalls = true;
        }
    }
    // Visit children to recurse on callees.
    currTemplate = node;
    // built the first time it's needed
    currNewAllCallers = null;
    visitChildren(node);
}
Also used : TemplateParam(com.google.template.soy.soytree.defn.TemplateParam)

Example 9 with TemplateParam

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

the class TemplateNodeBuilder method addParams.

/**
 * Helper for {@code setSoyDoc()} and {@code setHeaderDecls()}. This method is intended to be
 * called at most once for SoyDoc params and at most once for header params.
 *
 * @param params The params to add.
 */
public TemplateNodeBuilder addParams(Iterable<? extends TemplateParam> newParams) {
    Set<String> seenParamKeys = new HashSet<>();
    boolean hasTemplateHeaderParams = false;
    if (this.params == null) {
        this.params = ImmutableList.copyOf(newParams);
    } else {
        for (TemplateParam oldParam : this.params) {
            seenParamKeys.add(oldParam.name());
            hasTemplateHeaderParams |= oldParam.declLoc() == TemplateParam.DeclLoc.HEADER;
        }
        this.params = ImmutableList.<TemplateParam>builder().addAll(this.params).addAll(newParams).build();
    }
    // Check new params.
    for (TemplateParam param : newParams) {
        hasTemplateHeaderParams |= param.declLoc() == TemplateParam.DeclLoc.HEADER;
        if (param.name().equals("ij")) {
            errorReporter.report(param.nameLocation(), INVALID_PARAM_NAMED_IJ);
        }
        if (!seenParamKeys.add(param.name())) {
            errorReporter.report(param.nameLocation(), PARAM_ALREADY_DECLARED, param.name());
        }
    }
    // if the template has any header params, report an error on each soydoc param
    if (hasTemplateHeaderParams) {
        for (TemplateParam param : this.params) {
            if (param.declLoc() == TemplateParam.DeclLoc.SOY_DOC) {
                errorReporter.report(param.nameLocation(), MIXED_PARAM_STYLES);
            }
        }
    }
    return this;
}
Also used : TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) HashSet(java.util.HashSet)

Example 10 with TemplateParam

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

the class TemplateNode method clearSoyDocStrings.

/**
 * Clears the SoyDoc text, description, and param descriptions.
 */
public void clearSoyDocStrings() {
    soyDoc = null;
    soyDocDesc = null;
    List<TemplateParam> newParams = Lists.newArrayListWithCapacity(params.size());
    for (TemplateParam origParam : params) {
        newParams.add(origParam.copyEssential());
    }
    params = ImmutableList.copyOf(newParams);
}
Also used : TemplateParam(com.google.template.soy.soytree.defn.TemplateParam)

Aggregations

TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)18 TemplateNode (com.google.template.soy.soytree.TemplateNode)7 IndirectParamsInfo (com.google.template.soy.passes.FindIndirectParamsVisitor.IndirectParamsInfo)4 Test (org.junit.Test)4 TemplateBasicNode (com.google.template.soy.soytree.TemplateBasicNode)3 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)3 SoyType (com.google.template.soy.types.SoyType)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 SourceLocation (com.google.template.soy.base.SourceLocation)2 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 FindIndirectParamsVisitor (com.google.template.soy.passes.FindIndirectParamsVisitor)2 HeaderParam (com.google.template.soy.soytree.defn.HeaderParam)2 SoyDocParam (com.google.template.soy.soytree.defn.SoyDocParam)2 LinkedHashMap (java.util.LinkedHashMap)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 IndentedLinesBuilder (com.google.template.soy.base.internal.IndentedLinesBuilder)1