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;
}
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));
}
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);
}
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;
}
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;
}
Aggregations