Search in sources :

Example 1 with SoyFunction

use of com.google.template.soy.shared.restricted.SoyFunction in project closure-templates by google.

the class JsSrcSubject method parse.

private ParseResult parse() {
    SoyFileSetParserBuilder builder = SoyFileSetParserBuilder.forFileContents(actual()).allowUnboundGlobals(true).declaredSyntaxVersion(syntaxVersion).typeRegistry(typeRegistry).options(generalOptions);
    for (SoyFunction soyFunction : soyFunctions) {
        builder.addSoyFunction(soyFunction);
    }
    ParseResult parse = builder.parse();
    // genjscodevisitor depends on this having been run
    return parse;
}
Also used : SoyFileSetParserBuilder(com.google.template.soy.SoyFileSetParserBuilder) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyFunction(com.google.template.soy.shared.restricted.SoyFunction)

Example 2 with SoyFunction

use of com.google.template.soy.shared.restricted.SoyFunction in project closure-templates by google.

the class ExpressionCompilerTest method compileExpression.

private SoyExpression compileExpression(String soyExpr) {
    // The fake function allows us to work around the 'can't print bool' restrictions
    String createTemplateBody = createTemplateBody("fakeFunction(" + soyExpr + ")");
    PrintNode code = (PrintNode) SoyFileSetParserBuilder.forTemplateContents(createTemplateBody).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);
    return testExpressionCompiler.compile(((FunctionNode) code.getExpr().getChild(0)).getChild(0));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) SoyFunction(com.google.template.soy.shared.restricted.SoyFunction) PrintNode(com.google.template.soy.soytree.PrintNode)

Example 3 with SoyFunction

use of com.google.template.soy.shared.restricted.SoyFunction in project closure-templates by google.

the class FunctionNodeTest method testResolveFunctionsVisitor.

/**
 * Tests that {@link com.google.template.soy.passes.ResolveFunctionsVisitor} recurses into {@link
 * FunctionNode}s that are descendants of other FunctionNodes. (This omission caused cl/101255365
 * to be rolled back.)
 */
@Test
public void testResolveFunctionsVisitor() {
    SoyFunction foo = new SoyFunction() {

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

        @Override
        public Set<Integer> getValidArgsSizes() {
            return ImmutableSet.of(1);
        }
    };
    SoyFunction bar = new SoyFunction() {

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

        @Override
        public Set<Integer> getValidArgsSizes() {
            return ImmutableSet.of(1);
        }
    };
    SoyFileSetNode root = SoyFileSetParserBuilder.forTemplateContents("<a class=\"{foo(bar(1))}\">").addSoyFunction(foo).addSoyFunction(bar).parse().fileSet();
    List<FunctionNode> functionNodes = SoyTreeUtils.getAllNodesOfType(root, FunctionNode.class);
    assertThat(functionNodes).hasSize(2);
    assertThat(functionNodes.get(0).getSoyFunction()).isSameAs(foo);
    assertThat(functionNodes.get(1).getSoyFunction()).isSameAs(bar);
}
Also used : SoyFunction(com.google.template.soy.shared.restricted.SoyFunction) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 4 with SoyFunction

use of com.google.template.soy.shared.restricted.SoyFunction in project closure-templates by google.

the class PluginResolver method lookupSoyFunction.

/**
 * Returns a function with the given name and arity.
 *
 * <p>An error will be reported according to the current {@link Mode} and a placeholder function
 * will be returned if it cannot be found.
 */
public SoyFunction lookupSoyFunction(String name, int numArgs, SourceLocation location) {
    SoyFunction soyFunction = functions.get(name);
    if (soyFunction == null) {
        if (mode == Mode.REQUIRE_DEFINITIONS) {
            reporter.report(location, UNKNOWN_PLUGIN, "function", name, SoyErrors.getDidYouMeanMessage(functions.keySet(), name));
        }
        soyFunction = createPlaceholderSoyFunction(name, numArgs);
    }
    checkNumArgs("function", soyFunction.getValidArgsSizes(), numArgs, location);
    warnIfDeprecated(name, soyFunction, location);
    return soyFunction;
}
Also used : SoyFunction(com.google.template.soy.shared.restricted.SoyFunction)

Example 5 with SoyFunction

use of com.google.template.soy.shared.restricted.SoyFunction in project closure-templates by google.

the class SoyFileSetParserBuilder method addSoyFunctions.

public SoyFileSetParserBuilder addSoyFunctions(Iterable<? extends SoyFunction> soyFunctions) {
    Map<String, SoyFunction> functions = new LinkedHashMap<>();
    functions.putAll(soyFunctionMap);
    for (SoyFunction function : soyFunctions) {
        functions.put(function.getName(), function);
    }
    this.soyFunctionMap = ImmutableMap.copyOf(functions);
    return this;
}
Also used : SoyFunction(com.google.template.soy.shared.restricted.SoyFunction) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SoyFunction (com.google.template.soy.shared.restricted.SoyFunction)7 ImmutableSet (com.google.common.collect.ImmutableSet)3 Set (java.util.Set)3 PrintNode (com.google.template.soy.soytree.PrintNode)2 Test (org.junit.Test)2 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 TypedSoyFunction (com.google.template.soy.shared.restricted.TypedSoyFunction)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1