use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.
the class ExpressionSubject method isValidVarNamed.
void isValidVarNamed(String name) {
VarRefNode varNode = (VarRefNode) parseExpression();
assertThat(errorReporter.hasErrors()).isFalse();
String actualName = varNode.getName();
if (!actualName.equals(name)) {
failWithBadResults("is var named", name, "is named", actualName);
}
if (!varNode.toSourceString().equals("$" + name)) {
failWithBadResults("has sourceString", "$" + name, "is named", varNode.toSourceString());
}
}
use of com.google.template.soy.exprtree.VarRefNode 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();
}
use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.
the class VeLogInstrumentationVisitor method visitHtmlAttributeNode.
/**
* For HtmlAttributeNode that has a logging function as its value, replace the logging function
* with its place holder, and append a new data attribute that contains all the desired
* information that are used later by the runtime library.
*/
@Override
protected void visitHtmlAttributeNode(HtmlAttributeNode node) {
// Skip attributes that do not have a value.
if (!node.hasValue()) {
return;
}
SourceLocation insertionLocation = node.getSourceLocation();
for (FunctionNode function : SoyTreeUtils.getAllNodesOfType(node, FunctionNode.class)) {
if (!(function.getSoyFunction() instanceof LoggingFunction)) {
continue;
}
FunctionNode funcNode = new FunctionNode(VeLogJsSrcLoggingFunction.INSTANCE, insertionLocation);
funcNode.addChild(new StringNode(function.getFunctionName(), QuoteStyle.SINGLE, insertionLocation));
funcNode.addChild(new ListLiteralNode(function.getChildren(), insertionLocation));
StandaloneNode attributeName = node.getChild(0);
if (attributeName instanceof RawTextNode) {
// If attribute name is a plain text, directly pass it as a function argument.
funcNode.addChild(new StringNode(((RawTextNode) attributeName).getRawText(), QuoteStyle.SINGLE, insertionLocation));
} else {
// Otherwise wrap the print node or call node into a let block, and use the let variable
// as a function argument.
String varName = "soy_logging_function_attribute_" + counter;
LetContentNode letNode = LetContentNode.forVariable(nodeIdGen.genId(), attributeName.getSourceLocation(), varName, null);
// Adds a let var which references to the original attribute name, and move the name to
// the let block.
node.replaceChild(attributeName, new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
true, /* expr= */
new VarRefNode(varName, insertionLocation, false, letNode.getVar()), /* attributes= */
ImmutableList.of(), ErrorReporter.exploding()));
letNode.addChild(attributeName);
node.getParent().addChild(node.getParent().getChildIndex(node), letNode);
funcNode.addChild(new VarRefNode(varName, insertionLocation, false, letNode.getVar()));
}
funcNode.addChild(new IntegerNode(counter++, insertionLocation));
PrintNode loggingFunctionAttribute = new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
true, /* expr= */
funcNode, /* attributes= */
ImmutableList.of(), ErrorReporter.exploding());
// Append the logging function attribute to its parent
int appendIndex = node.getParent().getChildIndex(node) + 1;
node.getParent().addChild(appendIndex, loggingFunctionAttribute);
// Replace the original attribute value to the placeholder.
HtmlAttributeValueNode placeHolder = new HtmlAttributeValueNode(nodeIdGen.genId(), insertionLocation, Quotes.DOUBLE);
placeHolder.addChild(new RawTextNode(nodeIdGen.genId(), ((LoggingFunction) function.getSoyFunction()).getPlaceholder(), insertionLocation));
node.replaceChild(node.getChild(1), placeHolder);
// logging function in a html attribute value.
break;
}
visitChildren(node);
}
use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.
the class EvalVisitor method visitIsFirstFunction.
private SoyValue visitIsFirstFunction(FunctionNode node) {
int localVarIndex;
try {
VarRefNode dataRef = (VarRefNode) node.getChild(0);
localVarIndex = env.getIndex((LoopVar) dataRef.getDefnDecl());
} catch (Exception e) {
throw RenderException.create("Failed to evaluate function call " + node.toSourceString() + ".", e);
}
return convertResult(localVarIndex == 0);
}
use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.
the class EvalVisitor method visitIsLastFunction.
private SoyValue visitIsLastFunction(FunctionNode node) {
boolean isLast;
try {
VarRefNode dataRef = (VarRefNode) node.getChild(0);
isLast = env.isLast((LoopVar) dataRef.getDefnDecl());
} catch (Exception e) {
throw RenderException.create("Failed to evaluate function call " + node.toSourceString() + ".", e);
}
return convertResult(isLast);
}
Aggregations