Search in sources :

Example 1 with SourceLocation

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);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation)

Example 2 with SourceLocation

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)));
    }
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SourceLocation(com.google.template.soy.base.SourceLocation)

Example 3 with SourceLocation

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());
        }
    }
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) TemplateBasicNode(com.google.template.soy.soytree.TemplateBasicNode) ArrayList(java.util.ArrayList) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IndirectParamsInfo(com.google.template.soy.passes.FindIndirectParamsVisitor.IndirectParamsInfo) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) HashSet(java.util.HashSet)

Example 4 with SourceLocation

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);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) CopyState(com.google.template.soy.basetree.CopyState) PrintNode(com.google.template.soy.soytree.PrintNode) NullNode(com.google.template.soy.exprtree.NullNode) HtmlOpenTagNode(com.google.template.soy.soytree.HtmlOpenTagNode)

Example 5 with SourceLocation

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);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) Identifier(com.google.template.soy.base.internal.Identifier) SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind)

Aggregations

SourceLocation (com.google.template.soy.base.SourceLocation)23 Test (org.junit.Test)8 VarRefNode (com.google.template.soy.exprtree.VarRefNode)5 TemplateNode (com.google.template.soy.soytree.TemplateNode)4 IntegerNode (com.google.template.soy.exprtree.IntegerNode)3 Matcher (java.util.regex.Matcher)3 CharMatcher (com.google.common.base.CharMatcher)2 ExprNode (com.google.template.soy.exprtree.ExprNode)2 FunctionNode (com.google.template.soy.exprtree.FunctionNode)2 NullNode (com.google.template.soy.exprtree.NullNode)2 HtmlOpenTagNode (com.google.template.soy.soytree.HtmlOpenTagNode)2 PrintNode (com.google.template.soy.soytree.PrintNode)2 RawTextNode (com.google.template.soy.soytree.RawTextNode)2 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Point (com.google.template.soy.base.SourceLocation.Point)1 Identifier (com.google.template.soy.base.internal.Identifier)1 SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)1