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