Search in sources :

Example 56 with TemplateNode

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

the class StrictDepsVisitor method visitCallBasicNode.

// -----------------------------------------------------------------------------------------------
// Implementations for specific nodes.
// TODO(gboyer): Consider some deltemplate checking, but it's hard to make a coherent case for
// deltemplates since it's legitimate to have zero implementations, or to have the implementation
// in a different part of the dependency graph (if it's late-bound).
@Override
protected void visitCallBasicNode(CallBasicNode node) {
    TemplateNode callee = templateRegistry.getBasicTemplate(node.getCalleeName());
    if (callee == null) {
        String extraErrorMessage = SoyErrors.getDidYouMeanMessage(templateRegistry.getBasicTemplatesMap().keySet(), node.getCalleeName());
        errorReporter.report(node.getSourceLocation(), CALL_TO_UNDEFINED_TEMPLATE, node.getCalleeName(), extraErrorMessage);
    } else {
        SoyFileKind callerKind = node.getNearestAncestor(SoyFileNode.class).getSoyFileKind();
        SoyFileKind calleeKind = callee.getParent().getSoyFileKind();
        String callerFilePath = node.getSourceLocation().getFilePath();
        String calleeFilePath = callee.getSourceLocation().getFilePath();
        if (calleeKind == SoyFileKind.INDIRECT_DEP && callerKind == SoyFileKind.SRC) {
            errorReporter.report(node.getSourceLocation(), CALL_TO_INDIRECT_DEPENDENCY, calleeFilePath);
        }
        // should fail due to unknown template, but it doesn't hurt to add this.
        if (calleeKind == SoyFileKind.SRC && callerKind != SoyFileKind.SRC) {
            errorReporter.report(node.getSourceLocation(), CALL_FROM_DEP_TO_SRC, callee.getTemplateNameForUserMsgs(), calleeFilePath, callerFilePath);
        }
    }
    // Don't forget to visit content within CallParamContentNodes.
    visitChildren(node);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyFileKind(com.google.template.soy.base.internal.SoyFileKind) SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 57 with TemplateNode

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

the class PassManager method doContextualEscaping.

/**
 * Runs the autoescaper and returns whether or not new contextual templates have been added.
 */
private boolean doContextualEscaping(SoyFileSetNode soyTree) {
    List<TemplateNode> extraTemplates = autoescaper.rewrite(soyTree, errorReporter);
    // TODO: Run the redundant template remover here and rename after CL 16642341 is in.
    if (!extraTemplates.isEmpty()) {
        // TODO: pull out somewhere else.  Ideally do the merge as part of the redundant template
        // removal.
        Map<String, SoyFileNode> containingFile = new HashMap<>();
        for (SoyFileNode fileNode : soyTree.getChildren()) {
            for (TemplateNode templateNode : fileNode.getChildren()) {
                String name = templateNode instanceof TemplateDelegateNode ? ((TemplateDelegateNode) templateNode).getDelTemplateName() : templateNode.getTemplateName();
                containingFile.put(DerivedTemplateUtils.getBaseName(name), fileNode);
            }
        }
        for (TemplateNode extraTemplate : extraTemplates) {
            String name = extraTemplate instanceof TemplateDelegateNode ? ((TemplateDelegateNode) extraTemplate).getDelTemplateName() : extraTemplate.getTemplateName();
            containingFile.get(DerivedTemplateUtils.getBaseName(name)).addChild(extraTemplate);
        }
        return true;
    }
    return false;
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) HashMap(java.util.HashMap) SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 58 with TemplateNode

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

the class Rewriter method rewrite.

/**
 * @return Derived templates that should be added to the parse tree.
 */
public List<TemplateNode> rewrite(SoyFileSetNode files) {
    RewriterVisitor mutator = new RewriterVisitor();
    // First walk the input files that the caller already knows about.
    for (SoyFileNode file : files.getChildren()) {
        mutator.exec(file);
    }
    // Now walk over anything not reachable from the input files to make sure we get all the derived
    // templates.
    ImmutableList.Builder<TemplateNode> extraTemplates = ImmutableList.builder();
    for (TemplateNode template : inferences.getAllTemplates()) {
        String name = template.getTemplateName();
        if (!visitedTemplateNames.contains(name)) {
            extraTemplates.add(template);
            mutator.exec(template);
        }
    }
    return extraTemplates.build();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ImmutableList(com.google.common.collect.ImmutableList) SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 59 with TemplateNode

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

the class ContentSecurityPolicyNonceInjectionPass method run.

@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
    // * $ij references
    for (TemplateNode template : file.getChildren()) {
        for (TemplateParam param : template.getAllParams()) {
            if (param.isInjected() && param.name().equals(CSP_NONCE_VARIABLE_NAME)) {
                errorReporter.report(param.nameLocation(), IJ_CSP_NONCE_REFERENCE);
            }
        }
    }
    for (VarRefNode var : SoyTreeUtils.getAllNodesOfType(file, VarRefNode.class)) {
        // of isInjected
        if (var.isDollarSignIjParameter() && var.getName().equals(CSP_NONCE_VARIABLE_NAME)) {
            errorReporter.report(var.getSourceLocation(), IJ_CSP_NONCE_REFERENCE);
        }
    }
    for (HtmlOpenTagNode openTag : SoyTreeUtils.getAllNodesOfType(file, HtmlOpenTagNode.class)) {
        if (isTagNonceable(openTag)) {
            // this should point to the character immediately before the '>' or '/>' at the end of the
            // open tag
            SourceLocation insertionLocation = openTag.getSourceLocation().getEndPoint().offset(0, openTag.isSelfClosing() ? -2 : -1).asLocation(openTag.getSourceLocation().getFilePath());
            openTag.addChild(createCspInjection(insertionLocation, nodeIdGen));
        }
    }
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SourceLocation(com.google.template.soy.base.SourceLocation) VarRefNode(com.google.template.soy.exprtree.VarRefNode) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) HtmlOpenTagNode(com.google.template.soy.soytree.HtmlOpenTagNode)

Example 60 with TemplateNode

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

the class FindTransitiveDepTemplatesVisitor method exec.

/**
 * {@inheritDoc}
 *
 * <p>Note: This method is not thread-safe. If you need to get transitive dep templates info in a
 * thread-safe manner, then please use {@link #execOnAllTemplates}() in a thread-safe manner.
 */
@Override
public TransitiveDepTemplatesInfo exec(SoyNode rootTemplate) {
    Preconditions.checkArgument(rootTemplate instanceof TemplateNode);
    TemplateNode rootTemplateCast = (TemplateNode) rootTemplate;
    // If finished in a previous pass (previous call to exec), just return the finished info.
    if (templateToFinishedInfoMap.containsKey(rootTemplateCast)) {
        return templateToFinishedInfoMap.get(rootTemplateCast);
    }
    // Initialize vars for the pass.
    currTemplateVisitInfo = null;
    activeTemplateVisitInfoStack = new ArrayDeque<>();
    activeTemplateSet = Sets.newHashSet();
    visitedTemplateToInfoMap = Maps.newHashMap();
    visit(rootTemplateCast);
    if (!activeTemplateVisitInfoStack.isEmpty() || !activeTemplateSet.isEmpty()) {
        throw new AssertionError();
    }
    // Convert visit info to finished info for all visited templates.
    for (TemplateVisitInfo templateVisitInfo : visitedTemplateToInfoMap.values()) {
        templateToFinishedInfoMap.put(templateVisitInfo.rootTemplate, templateVisitInfo.toFinishedInfo());
    }
    return templateToFinishedInfoMap.get(rootTemplateCast);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode)

Aggregations

TemplateNode (com.google.template.soy.soytree.TemplateNode)89 Test (org.junit.Test)59 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)35 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)19 ErrorReporter (com.google.template.soy.error.ErrorReporter)11 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)10 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)9 RawTextNode (com.google.template.soy.soytree.RawTextNode)9 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)8 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)7 FunctionNode (com.google.template.soy.exprtree.FunctionNode)6 PrintNode (com.google.template.soy.soytree.PrintNode)5 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 SourceLocation (com.google.template.soy.base.SourceLocation)4 StringNode (com.google.template.soy.exprtree.StringNode)4 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)4 ImmutableList (com.google.common.collect.ImmutableList)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)3 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)3