Search in sources :

Example 61 with TemplateNode

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

the class FindCalleesNotInFileVisitor method visitSoyFileNode.

// -----------------------------------------------------------------------------------------------
// Implementations for specific nodes.
@Override
protected void visitSoyFileNode(SoyFileNode node) {
    templatesInFile = new LinkedHashSet<>();
    for (TemplateNode template : node.getChildren()) {
        templatesInFile.add(template.getTemplateName());
    }
    calleesNotInFile = new LinkedHashSet<>();
    visitChildren(node);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode)

Example 62 with TemplateNode

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

the class GenJsCodeVisitorTest method testPrivateTemplateHasPrivateJsDocAnnotationInGencode.

// -----------------------------------------------------------------------------------------------
// Header params.
@Test
public void testPrivateTemplateHasPrivateJsDocAnnotationInGencode() {
    String testFileContent = "{namespace boo.foo}\n" + "\n" + "/** Test template. */\n" + "{template .goo autoescape=\"deprecated-noncontextual\" visibility=\"private\"}\n" + "  Blah\n" + "{/template}\n";
    String expectedJsCode = "" + "/**\n" + " * @param {Object<string, *>=} opt_data\n" + " * @param {Object<string, *>=} opt_ijData\n" + " * @param {Object<string, *>=} opt_ijData_deprecated\n" + " * @return {string}\n" + " * @suppress {checkTypes|uselessCode}\n" + " * @private\n" + " */\n" + "boo.foo.goo = function(opt_data, opt_ijData, opt_ijData_deprecated) {\n" + "  opt_ijData = opt_ijData_deprecated || opt_ijData;\n" + "  return 'Blah';\n" + "};\n" + "if (goog.DEBUG) {\n" + "  boo.foo.goo.soyTemplateName = 'boo.foo.goo';\n" + "}\n";
    // Setup the GenJsCodeVisitor's state before the template is visited.
    genJsCodeVisitor.jsCodeBuilder = new JsCodeBuilder();
    ParseResult parseResult = SoyFileSetParserBuilder.forFileContents(testFileContent).parse();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(parseResult.fileSet());
    genJsCodeVisitor.visitForTesting(template, parseResult.registry(), ErrorReporter.exploding());
    assertThat(genJsCodeVisitor.jsCodeBuilder.getCode()).isEqualTo(expectedJsCode);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) Test(org.junit.Test)

Example 63 with TemplateNode

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

the class SourceLocationTest method testIsJustBefore.

@Test
public void testIsJustBefore() throws Exception {
    String template = JOINER.join("{namespace ns}", "{template .t}", "{@param foo : ?}", "{@param bar : ?}", // pair 1
    "  {$foo}{$bar}", // pair 2
    "  {$foo} {$bar}", // pair 3
    "  {$foo}", "  {$bar}", // pair 4
    "{$foo}", "{$bar}", "{/template}");
    TemplateNode templateNode = SoyFileSetParserBuilder.forFileContents(template).parse().fileSet().getChild(0).getChild(0);
    List<PrintNode> nodes = SoyTreeUtils.getAllNodesOfType(templateNode, PrintNode.class);
    assertThat(nodes).hasSize(8);
    PrintNode foo1 = nodes.get(0);
    PrintNode bar1 = nodes.get(1);
    assertTrue(foo1.getSourceLocation().isJustBefore(bar1.getSourceLocation()));
    PrintNode foo2 = nodes.get(2);
    PrintNode bar2 = nodes.get(3);
    assertFalse(foo2.getSourceLocation().isJustBefore(bar2.getSourceLocation()));
    PrintNode foo3 = nodes.get(4);
    PrintNode bar3 = nodes.get(5);
    assertFalse(foo3.getSourceLocation().isJustBefore(bar3.getSourceLocation()));
    PrintNode foo4 = nodes.get(6);
    PrintNode bar4 = nodes.get(7);
    assertFalse(foo4.getSourceLocation().isJustBefore(bar4.getSourceLocation()));
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 64 with TemplateNode

use of com.google.template.soy.soytree.TemplateNode 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)

Example 65 with TemplateNode

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

the class TemplateCompiler method generateRenderMethod.

private Statement generateRenderMethod() {
    final Label start = new Label();
    final Label end = new Label();
    final LocalVariable thisVar = createThisVar(template.typeInfo(), start, end);
    final LocalVariable appendableVar = createLocal("appendable", 1, LOGGING_ADVISING_APPENDABLE_TYPE, start, end).asNonNullable();
    final LocalVariable contextVar = createLocal("context", 2, RENDER_CONTEXT_TYPE, start, end).asNonNullable();
    final TemplateVariableManager variableSet = new TemplateVariableManager(fieldNames, template.typeInfo(), thisVar, template.renderMethod().method());
    TemplateNode node = template.node();
    TemplateVariables variables = new TemplateVariables(variableSet, thisVar, new RenderContextExpression(contextVar));
    final CompiledMethodBody methodBody = SoyNodeCompiler.create(registry, innerClasses, stateField, thisVar, AppendableExpression.forLocal(appendableVar), variableSet, variables).compile(node);
    final Statement returnDone = Statement.returnExpression(MethodRef.RENDER_RESULT_DONE.invoke());
    new Statement() {

        @Override
        protected void doGen(CodeBuilder adapter) {
            adapter.mark(start);
            methodBody.body().gen(adapter);
            adapter.mark(end);
            returnDone.gen(adapter);
            thisVar.tableEntry(adapter);
            appendableVar.tableEntry(adapter);
            contextVar.tableEntry(adapter);
            variableSet.generateTableEntries(adapter);
        }
    }.writeIOExceptionMethod(Opcodes.ACC_PUBLIC, template.renderMethod().method(), writer);
    writer.setNumDetachStates(methodBody.numberOfDetachStates());
    variableSet.defineStaticFields(writer);
    return variableSet.defineFields(writer);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) Statement(com.google.template.soy.jbcsrc.restricted.Statement) Label(org.objectweb.asm.Label) LocalVariable(com.google.template.soy.jbcsrc.restricted.LocalVariable) CompiledMethodBody(com.google.template.soy.jbcsrc.SoyNodeCompiler.CompiledMethodBody) CodeBuilder(com.google.template.soy.jbcsrc.restricted.CodeBuilder)

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