Search in sources :

Example 6 with FunctionNode

use of com.google.template.soy.exprtree.FunctionNode in project closure-templates by google.

the class XidPass method run.

@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
    for (FunctionNode fn : SoyTreeUtils.getAllNodesOfType(file, FunctionNode.class)) {
        if (fn.getSoyFunction() == BuiltinFunction.XID) {
            if (fn.numChildren() != 1) {
                // if it isn't == 1, then an error has already been reported, move along.
                continue;
            }
            ExprNode child = fn.getChild(0);
            switch(child.getKind()) {
                case GLOBAL_NODE:
                    GlobalNode global = (GlobalNode) child;
                    if (global.isResolved()) {
                        // This doesn't have to be an error. but it is confusing if it is is since it is
                        // unclear if the user intended to xid the identifier or the value.
                        reporter.report(global.getSourceLocation(), GLOBAL_XID_ARG_IS_RESOLVED, global.getType().toString(), global.getValue().toSourceString());
                    }
                    fn.replaceChild(0, new StringNode(global.getName(), QuoteStyle.SINGLE, global.getSourceLocation()));
                    break;
                case STRING_NODE:
                    break;
                default:
                    reporter.report(child.getSourceLocation(), STRING_OR_GLOBAL_REQUIRED);
            }
        }
    }
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode) GlobalNode(com.google.template.soy.exprtree.GlobalNode)

Example 7 with FunctionNode

use of com.google.template.soy.exprtree.FunctionNode in project closure-templates by google.

the class ResolveExpressionTypesVisitorTest method assertTypes.

/**
 * Traverses the tree and checks all the calls to {@code assertType}
 */
private void assertTypes(SoyNode node) {
    for (FunctionNode fn : SoyTreeUtils.getAllNodesOfType(node, FunctionNode.class)) {
        if (fn.getFunctionName().equals("assertType")) {
            StringNode expected = (StringNode) fn.getChild(0);
            SoyType actualType = fn.getChild(1).getType();
            assertWithMessage("assertion @ " + fn.getSourceLocation()).that(actualType.toString()).isEqualTo(expected.getValue());
        }
    }
}
Also used : SoyType(com.google.template.soy.types.SoyType) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode)

Example 8 with FunctionNode

use of com.google.template.soy.exprtree.FunctionNode in project closure-templates by google.

the class ResolvePackageRelativeCssNamesVisitorTest method testRequireCssOnNamespace.

@Test
public void testRequireCssOnNamespace() {
    TemplateNode template = compileTemplate("{namespace boo requirecss=\"some.test.package,some.other.package\"}\n\n" + "/** Test template. */\n" + "{template .foo}\n" + "  <p class=\"{css('%AAA')}\">\n" + "{/template}\n");
    PrintNode printNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(template, PrintNode.class));
    FunctionNode cssFn = (FunctionNode) printNode.getExpr().getRoot();
    assertThat(((StringNode) cssFn.getChild(0)).getValue()).isEqualTo("someTestPackageAAA");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 9 with FunctionNode

use of com.google.template.soy.exprtree.FunctionNode in project closure-templates by google.

the class EvalVisitorTest method eval.

/**
 * Evaluates the given expression and returns the result.
 *
 * @param expression The expression to evaluate.
 * @return The expression result.
 * @throws Exception If there's an error.
 */
private SoyValue eval(String expression) throws Exception {
    PrintNode code = (PrintNode) SoyFileSetParserBuilder.forTemplateContents(// wrap in a function so we don't run into the 'can't print bools' error message
    untypedTemplateBodyForExpression("fakeFunction(" + expression + ")")).addSoyFunction(new SoyFunction() {

        @Override
        public String getName() {
            return "fakeFunction";
        }

        @Override
        public Set<Integer> getValidArgsSizes() {
            return ImmutableSet.of(1);
        }
    }).parse().fileSet().getChild(0).getChild(0).getChild(0);
    ExprNode expr = ((FunctionNode) code.getExpr().getChild(0)).getChild(0);
    EvalVisitor evalVisitor = new EvalVisitorFactoryImpl().create(TestingEnvironment.createForTest(testData, LOCALS), TEST_IJ_DATA, cssRenamingMap, xidRenamingMap, null, /* debugSoyTemplateInfo= */
    false);
    return evalVisitor.exec(expr);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) SoyFunction(com.google.template.soy.shared.restricted.SoyFunction) FunctionNode(com.google.template.soy.exprtree.FunctionNode) PrintNode(com.google.template.soy.soytree.PrintNode)

Example 10 with FunctionNode

use of com.google.template.soy.exprtree.FunctionNode in project closure-templates by google.

the class SharedTestUtils method createTemplateBodyForExpression.

/**
 * Returns a template body for the given soy expression. With type specializations.
 */
public static String createTemplateBodyForExpression(String soyExpr, final Map<String, SoyType> typeMap) {
    ExprNode expr = SoyFileParser.parseExpression(soyExpr, PluginResolver.nullResolver(Mode.ALLOW_UNDEFINED, ErrorReporter.exploding()), ErrorReporter.exploding());
    final Set<String> loopVarNames = new HashSet<>();
    final Set<String> names = new HashSet<>();
    new AbstractExprNodeVisitor<Void>() {

        @Override
        protected void visitVarRefNode(VarRefNode node) {
            if (!node.isDollarSignIjParameter()) {
                names.add(node.getName());
            }
        }

        @Override
        protected void visitFunctionNode(FunctionNode node) {
            switch(node.getFunctionName()) {
                case "index":
                case "isFirst":
                case "isLast":
                    loopVarNames.add(((VarRefNode) node.getChild(0)).getName());
                    break;
                // fall out
                default:
            }
            visitChildren(node);
        }

        @Override
        protected void visitExprNode(ExprNode node) {
            if (node instanceof ParentExprNode) {
                visitChildren((ParentExprNode) node);
            }
        }
    }.exec(expr);
    final StringBuilder templateBody = new StringBuilder();
    for (String varName : Sets.difference(names, loopVarNames)) {
        SoyType type = typeMap.get(varName);
        if (type == null) {
            type = UnknownType.getInstance();
        }
        templateBody.append("{@param " + varName + ": " + type + "}\n");
    }
    String contents = "{" + soyExpr + "}\n";
    for (String loopVar : loopVarNames) {
        contents = "{for $" + loopVar + " in [null]}\n" + contents + "\n{/for}";
    }
    templateBody.append(contents);
    return templateBody.toString();
}
Also used : ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) SoyType(com.google.template.soy.types.SoyType) FunctionNode(com.google.template.soy.exprtree.FunctionNode) HashSet(java.util.HashSet)

Aggregations

FunctionNode (com.google.template.soy.exprtree.FunctionNode)20 PrintNode (com.google.template.soy.soytree.PrintNode)8 StringNode (com.google.template.soy.exprtree.StringNode)7 Test (org.junit.Test)7 TemplateNode (com.google.template.soy.soytree.TemplateNode)6 ExprNode (com.google.template.soy.exprtree.ExprNode)5 IntegerNode (com.google.template.soy.exprtree.IntegerNode)4 RawTextNode (com.google.template.soy.soytree.RawTextNode)3 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)3 SourceLocation (com.google.template.soy.base.SourceLocation)2 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 LoggingFunction (com.google.template.soy.logging.LoggingFunction)2 CallParamContentNode (com.google.template.soy.soytree.CallParamContentNode)2 CallParamValueNode (com.google.template.soy.soytree.CallParamValueNode)2 SoyType (com.google.template.soy.types.SoyType)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)1 CopyState (com.google.template.soy.basetree.CopyState)1 SyntaxVersionUpperBound (com.google.template.soy.basetree.SyntaxVersionUpperBound)1 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)1