use of com.google.template.soy.base.SourceLocation in project closure-templates by google.
the class ContextualAutoescaper method reportError.
/**
* Reports an autoescape exception.
*/
private void reportError(ErrorReporter errorReporter, Set<SourceLocation> errorLocations, SoyAutoescapeException e) {
// First, get to the root cause of the exception, and assemble an error message indicating
// the full call stack that led to the failure.
String message = "- " + e.getOriginalMessage();
while (e.getCause() instanceof SoyAutoescapeException) {
e = (SoyAutoescapeException) e.getCause();
message += "\n- " + e.getMessage();
}
// Now that we've gotten to the leaf, let's use its source location as the canonical one for
// reporting and de-duping. (We might otherwise end up reporting a single error multiple times
// because a single template was called by multiple other contextual templates.)
// TODO(gboyer): Delete this logic once deprecated-contextual is removed.
SourceLocation location = Preconditions.checkNotNull(e.getSourceLocation());
if (!errorLocations.add(location)) {
return;
}
errorReporter.report(location, AUTOESCAPE_ERROR, message);
}
use of com.google.template.soy.base.SourceLocation in project closure-templates by google.
the class AddHtmlCommentsForDebugPass method rewriteTemplates.
/**
* Actual rewrite the template nodes for adding HTML comments.
*/
private void rewriteTemplates(Set<TemplateNode> templatesToRewrite, IdGenerator nodeIdGen) {
for (TemplateNode node : templatesToRewrite) {
String templateName = getTemplateName(node);
SourceLocation insertLocation = node.getSourceLocation();
node.addChild(0, createSoyDebug(insertLocation, nodeIdGen, String.format(HTML_COMMENTS_PREFIX, templateName, insertLocation.getFilePath(), insertLocation.getBeginLine())));
node.addChild(createSoyDebug(insertLocation, nodeIdGen, String.format(HTML_COMMENTS_SUFFIX, templateName)));
}
}
use of com.google.template.soy.base.SourceLocation in project closure-templates by google.
the class CheckTemplateParamsVisitor method visitTemplateNode.
// -----------------------------------------------------------------------------------------------
// Implementations for specific nodes.
@Override
protected void visitTemplateNode(TemplateNode node) {
if (!node.couldHaveSyntaxVersionAtLeast(SyntaxVersion.V2_0)) {
return;
}
ListMultimap<String, SourceLocation> dataKeys = ArrayListMultimap.create();
for (VarRefNode varRefNode : SoyTreeUtils.getAllNodesOfType(node, VarRefNode.class)) {
if (varRefNode.isPossibleParam()) {
dataKeys.put(varRefNode.getName(), varRefNode.getSourceLocation());
}
}
IndirectParamsInfo ipi = new FindIndirectParamsVisitor(templateRegistry).exec(node);
Set<String> allParamNames = new HashSet<>();
List<TemplateParam> unusedParams = new ArrayList<>();
for (TemplateParam param : node.getAllParams()) {
allParamNames.add(param.name());
if (dataKeys.containsKey(param.name())) {
// Good: Declared and referenced in template. We remove these from dataKeys so
// that at the end of the for-loop, dataKeys will only contain the keys that are referenced
// but not declared in SoyDoc.
dataKeys.removeAll(param.name());
} else if (ipi.paramKeyToCalleesMultimap.containsKey(param.name()) || ipi.mayHaveIndirectParamsInExternalCalls || ipi.mayHaveIndirectParamsInExternalDelCalls) {
// Good: Declared in SoyDoc and either (a) used in a call that passes all data or (b) used
// in an external call or delcall that passes all data, which may need the param (we can't
// verify).
} else {
// Bad: Declared in SoyDoc but not referenced in template.
unusedParams.add(param);
}
}
// At this point, the only keys left in dataKeys are undeclared.
for (Entry<String, SourceLocation> undeclared : dataKeys.entries()) {
String extraErrorMessage = SoyErrors.getDidYouMeanMessage(allParamNames, undeclared.getKey());
errorReporter.report(undeclared.getValue(), UNDECLARED_DATA_KEY, undeclared.getKey(), extraErrorMessage);
}
// of the same delegate may need to use those params.
if (node instanceof TemplateBasicNode) {
for (TemplateParam unusedParam : unusedParams) {
errorReporter.report(unusedParam.nameLocation(), UNUSED_PARAM, unusedParam.name());
}
}
}
use of com.google.template.soy.base.SourceLocation in project closure-templates by google.
the class VeLogInstrumentationVisitor method visitVeLogNode.
/**
* Adds data-soylog attribute to the top-level DOM node in this {velog} block.
*/
@Override
protected void visitVeLogNode(VeLogNode node) {
// VeLogValidationPass enforces that the first child is a open tag. We can safely cast it here.
HtmlOpenTagNode tag = (HtmlOpenTagNode) node.getChild(0);
SourceLocation insertionLocation = tag.getSourceLocation().getEndPoint().offset(0, tag.isSelfClosing() ? -2 : -1).asLocation(tag.getSourceLocation().getFilePath());
FunctionNode funcNode = new FunctionNode(VeLogFunction.INSTANCE, insertionLocation);
funcNode.addChild(new IntegerNode(node.getLoggingId(), insertionLocation));
funcNode.addChild(node.getConfigExpression() == null ? new NullNode(insertionLocation) : node.getConfigExpression().copy(new CopyState()));
if (node.getLogonlyExpression() != null) {
funcNode.addChild(node.getLogonlyExpression().copy(new CopyState()));
}
PrintNode attributeNode = new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
true, /* expr= */
funcNode, /* attributes= */
ImmutableList.of(), ErrorReporter.exploding());
tag.addChild(attributeNode);
visitChildren(node);
}
use of com.google.template.soy.base.SourceLocation in project closure-templates by google.
the class TemplateNodeBuilder method setCommonCommandValues.
protected void setCommonCommandValues(List<CommandTagAttribute> attrs) {
AutoescapeMode autoescapeMode = soyFileHeaderInfo.defaultAutoescapeMode;
SanitizedContentKind kind = null;
SourceLocation kindLocation = null;
for (CommandTagAttribute attribute : attrs) {
Identifier name = attribute.getName();
switch(name.identifier()) {
case "autoescape":
autoescapeMode = attribute.valueAsAutoescapeMode(errorReporter);
break;
case "kind":
kind = attribute.valueAsContentKind(errorReporter);
kindLocation = attribute.getValueLocation();
if (kind == SanitizedContentKind.HTML) {
errorReporter.report(kindLocation, CommandTagAttribute.EXPLICIT_DEFAULT_ATTRIBUTE, "kind", "html");
}
break;
case "requirecss":
setRequiredCssNamespaces(attribute.valueAsRequireCss(errorReporter));
break;
case "cssbase":
setCssBaseNamespace(attribute.valueAsCssBase(errorReporter));
break;
case "deprecatedV1":
markDeprecatedV1(attribute.valueAsEnabled(errorReporter));
break;
case "stricthtml":
strictHtmlDisabled = attribute.valueAsDisabled(errorReporter);
break;
default:
break;
}
}
setAutoescapeInfo(autoescapeMode, kind, kindLocation);
}
Aggregations