Search in sources :

Example 11 with TemplateParam

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

the class FindIndirectParamsVisitorTest method testFindIndirectParams.

@Test
public void testFindIndirectParams() {
    String fileContent1 = "{namespace alpha}\n" + "\n" + "/** @param? a0 @param? b3 */\n" + // 'b3' listed by alpha.zero
    "{template .zero}\n" + "  {call .zero data=\"all\" /}\n" + // recursive call should not cause 'a0' to be added
    "  {call .one data=\"all\" /}\n" + "  {call .two /}\n" + "  {call beta.zero /}\n" + "  {call .five data=\"all\"}\n" + "    {param a5: $a0 /}\n" + "    {param b2: 88 /}\n" + "  {/call}\n" + "{/template}\n" + "\n" + "/** @param? a1 */\n" + "{template .one}\n" + "  {call .three data=\"all\" /}\n" + "  {call .four /}\n" + "  {$a1}\n" + "{/template}\n" + "\n" + "/** @param? a2 */\n" + "{template .two}\n" + "  {$a2}\n" + "{/template}\n" + "\n" + "/** @param? a3 */\n" + "{template .three}\n" + "  {call beta.one data=\"all\" /}\n" + "  {$a3}\n" + "{/template}\n" + "\n" + "/** @param? a4 */\n" + "{template .four}\n" + "  {call external.one /}\n" + "  {$a4}\n" + "{/template}\n" + "\n" + "/** @param? a5 @param? b4 */\n" + // 'b4' listed by alpha.five
    "{template .five}\n" + "  {call beta.two data=\"all\" /}\n" + "  {call beta.three data=\"all\" /}\n" + "  {call beta.four data=\"all\" /}\n" + "  {$b4}\n" + "  {$a5}\n" + "{/template}\n" + "\n" + "/** @param? a6 */\n" + "{template .six}\n" + "  {$a6}\n" + "{/template}\n";
    String fileContent2 = "{namespace beta}\n" + "\n" + "/** @param? b0 */\n" + "{template .zero}\n" + "  {$b0}\n" + "{/template}\n" + "\n" + "/** @param? b1 */\n" + "{template .one}\n" + "  {call alpha.six data=\"all\" /}\n" + "  {$b1}\n" + "{/template}\n" + "\n" + "/** @param? b2 */\n" + "{template .two}\n" + "  {$b2}\n" + "{/template}\n" + "\n" + "/** @param? b3 */\n" + "{template .three}\n" + "  {$b3}\n" + "{/template}\n" + "\n" + "/** @param? b4 */\n" + "{template .four}\n" + "  {$b4}\n" + "{/template}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(fileContent1, fileContent2).errorReporter(boom).parse().fileSet();
    TemplateRegistry registry = new TemplateRegistry(soyTree, boom);
    SoyFileNode a = soyTree.getChild(0);
    TemplateNode a0 = a.getChild(0);
    TemplateNode a1 = a.getChild(1);
    // TemplateNode a2 = a.getChild(2);
    TemplateNode a3 = a.getChild(3);
    // TemplateNode a4 = a.getChild(4);
    TemplateNode a5 = a.getChild(5);
    TemplateNode a6 = a.getChild(6);
    SoyFileNode b = soyTree.getChild(1);
    // TemplateNode b0 = b.getChild(0);
    TemplateNode b1 = b.getChild(1);
    // TemplateNode b2 = b.getChild(2);
    TemplateNode b3 = b.getChild(3);
    TemplateNode b4 = b.getChild(4);
    IndirectParamsInfo ipi = new FindIndirectParamsVisitor(registry).exec(a0);
    assertThat(ipi.mayHaveIndirectParamsInExternalCalls).isFalse();
    assertThat(ipi.mayHaveIndirectParamsInExternalDelCalls).isFalse();
    Map<String, TemplateParam> ipMap = ipi.indirectParams;
    assertThat(ipMap).hasSize(6);
    assertThat(ipMap).doesNotContainKey("a0");
    assertThat(ipMap).containsKey("a1");
    assertThat(ipMap).doesNotContainKey("a2");
    assertThat(ipMap).containsKey("a3");
    assertThat(ipMap).doesNotContainKey("a4");
    assertThat(ipMap).doesNotContainKey("a5");
    assertThat(ipMap).containsKey("a6");
    assertThat(ipMap).doesNotContainKey("b0");
    assertThat(ipMap).containsKey("b1");
    assertThat(ipMap).doesNotContainKey("b2");
    assertThat(ipMap).containsKey("b3");
    assertThat(ipMap).containsKey("b4");
    Multimap<String, TemplateNode> pktcm = ipi.paramKeyToCalleesMultimap;
    assertThat(pktcm).valuesForKey("a1").isEqualTo(ImmutableSet.of(a1));
    assertThat(pktcm).valuesForKey("a3").isEqualTo(ImmutableSet.of(a3));
    assertThat(pktcm).valuesForKey("a6").isEqualTo(ImmutableSet.of(a6));
    assertThat(pktcm).valuesForKey("b1").isEqualTo(ImmutableSet.of(b1));
    assertThat(pktcm).valuesForKey("b3").isEqualTo(ImmutableSet.of(b3));
    // 'b4' listed by alpha.five
    assertThat(pktcm).valuesForKey("b4").isEqualTo(ImmutableSet.of(a5, b4));
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ErrorReporter(com.google.template.soy.error.ErrorReporter) IndirectParamsInfo(com.google.template.soy.passes.FindIndirectParamsVisitor.IndirectParamsInfo) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) Test(org.junit.Test)

Example 12 with TemplateParam

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

the class ResolveNamesVisitor method visitTemplateNode.

@Override
protected void visitTemplateNode(TemplateNode node) {
    // Create a scope for all parameters.
    localVariables = new LocalVariables();
    localVariables.enterScope();
    ijParams = new HashMap<>();
    // Add both injected and regular params to the param scope.
    for (TemplateParam param : node.getAllParams()) {
        localVariables.define(param, node);
    }
    visitSoyNode(node);
    localVariables.exitScope();
    localVariables.verify();
    node.setMaxLocalVariableTableSize(localVariables.nextSlotToClaim);
    localVariables = null;
    ijParams = null;
}
Also used : TemplateParam(com.google.template.soy.soytree.defn.TemplateParam)

Example 13 with TemplateParam

use of com.google.template.soy.soytree.defn.TemplateParam 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 14 with TemplateParam

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

the class TemplateNode method toSourceString.

@Override
public String toSourceString() {
    StringBuilder sb = new StringBuilder();
    // SoyDoc.
    if (soyDoc != null) {
        sb.append(soyDoc).append("\n");
    }
    // Begin tag.
    sb.append(getTagString()).append("\n");
    // Header.
    for (TemplateParam param : params) {
        if (param.declLoc() != DeclLoc.HEADER) {
            continue;
        }
        HeaderParam headerParam = (HeaderParam) param;
        sb.append("  {@param");
        if (!headerParam.isRequired()) {
            sb.append('?');
        }
        sb.append(' ').append(headerParam.name()).append(": ").append(headerParam.type()).append("}");
        if (headerParam.desc() != null) {
            sb.append("  /** ").append(headerParam.desc()).append(" */");
        }
        sb.append("\n");
    }
    // Body.
    // If first or last char of template body is a space, must be turned into '{sp}'.
    StringBuilder bodySb = new StringBuilder();
    appendSourceStringForChildren(bodySb);
    int bodyLen = bodySb.length();
    if (bodyLen != 0) {
        if (bodyLen != 1 && bodySb.charAt(bodyLen - 1) == ' ') {
            bodySb.replace(bodyLen - 1, bodyLen, "{sp}");
        }
        if (bodySb.charAt(0) == ' ') {
            bodySb.replace(0, 1, "{sp}");
        }
    }
    sb.append(bodySb);
    sb.append("\n");
    // End tag.
    sb.append("{/").append(getCommandName()).append("}\n");
    return sb.toString();
}
Also used : HeaderParam(com.google.template.soy.soytree.defn.HeaderParam) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam)

Example 15 with TemplateParam

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

the class TemplateParserTest method testParseHeaderDecls.

@Test
public void testParseHeaderDecls() throws Exception {
    String templateHeaderAndBody = "" + "  {@param boo: string}  // Something scary. (Not doc comment.)\n" + "  {@param foo: list<int>}  /** Something random. */\n" + "  {@param goo: string}/** Something\n" + "      slimy. */\n" + "  /* Something strong. (Not doc comment.) */" + "  // {@param commentedOut: string}\n" + "  {@param moo: string}{@param too: string}\n" + "  {@param? woo: string}  /** Something exciting. */  {@param hoo: string}\n" + // use all the params
    "  {$boo + $goo + $moo + $too + $woo + $hoo}{$foo}\n";
    TemplateNode result = parseTemplateContent(templateHeaderAndBody, FAIL);
    assertEquals(7, Iterables.size(result.getAllParams()));
    assertEquals("{$boo + $goo + $moo + $too + $woo + $hoo}", result.getChildren().get(0).toSourceString());
    List<TemplateParam> declInfos = ImmutableList.copyOf(result.getAllParams());
    assertFalse(declInfos.get(0).isInjected());
    assertEquals("boo", declInfos.get(0).name());
    assertEquals("string", declInfos.get(0).type().toString());
    assertEquals(null, declInfos.get(0).desc());
    assertEquals("foo", declInfos.get(1).name());
    assertEquals("list<int>", declInfos.get(1).type().toString());
    assertEquals(null, declInfos.get(1).desc());
    assertEquals("Something random.", declInfos.get(2).desc());
    assertEquals("Something\n      slimy.", declInfos.get(3).desc());
    assertEquals("too", declInfos.get(4).name());
    assertEquals(null, declInfos.get(4).desc());
    assertEquals("woo", declInfos.get(5).name());
    assertEquals(null, declInfos.get(5).desc());
    assertEquals("Something exciting.", declInfos.get(6).desc());
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) Test(org.junit.Test)

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