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);
}
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;
}
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();
}
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));
}
}
}
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);
}
Aggregations