public class GenJsCodeVisitor extends AbstractSoyNodeVisitor<List<String>>
Precondition: MsgNode should not exist in the tree.
gen(com.google.template.soy.soytree.SoyFileSetNode, com.google.template.soy.soytree.TemplateRegistry, com.google.template.soy.error.ErrorReporter) should be called on a full parse tree. JS source code will be generated for all
the Soy files. The return value is a list of strings, each string being the content of one
generated JS file (corresponding to one Soy file).
| Modifier and Type | Field and Description |
|---|---|
protected ErrorReporter |
errorReporter |
protected GenCallCodeUtils |
genCallCodeUtils
Instance of GenCallCodeUtils to use.
|
protected GenJsExprsVisitor |
genJsExprsVisitor
The GenJsExprsVisitor used for the current template.
|
protected IsComputableAsJsExprsVisitor |
isComputableAsJsExprsVisitor
The IsComputableAsJsExprsVisitor used by this instance.
|
protected SoyJsSrcOptions |
jsSrcOptions
The options for generating JS source code.
|
protected TemplateAliases |
templateAliases
Used for looking up the local name for a given template call to a fully qualified template
name.
|
protected TemplateRegistry |
templateRegistry |
protected TranslationContext |
templateTranslationContext |
| Modifier | Constructor and Description |
|---|---|
protected |
GenJsCodeVisitor(SoyJsSrcOptions jsSrcOptions,
DelTemplateNamer delTemplateNamer,
GenCallCodeUtils genCallCodeUtils,
IsComputableAsJsExprsVisitor isComputableAsJsExprsVisitor,
CanInitOutputVarVisitor canInitOutputVarVisitor,
GenJsExprsVisitor.GenJsExprsVisitorFactory genJsExprsVisitorFactory,
SoyTypeRegistry typeRegistry) |
| Modifier and Type | Method and Description |
|---|---|
protected JsCodeBuilder |
createChildJsCodeBuilder() |
protected JsCodeBuilder |
createCodeBuilder() |
List<String> |
exec(SoyNode node)
|
List<String> |
gen(SoyFileSetNode node,
TemplateRegistry registry,
ErrorReporter errorReporter) |
protected CodeChunk |
generateFunctionBody(TemplateNode node)
Generates the function body.
|
protected void |
generateNonExpressionIfNode(IfNode node)
Generates the JavaScript code for an {if} block that cannot be done as an expression.
|
protected CodeChunk |
genParamTypeChecks(TemplateNode node)
Generate code to verify the runtime types of the input params.
|
protected GenJsCodeVisitorAssistantForMsgs |
getAssistantForMsgs() |
protected String |
getGoogModuleNamespace(String soyNamespace) |
protected JsCodeBuilder |
getJsCodeBuilder() |
protected String |
getTemplateReturnType(TemplateNode node) |
protected CodeChunk.WithValue |
sanitize(CodeChunk.WithValue templateBody,
SanitizedContentKind contentKind) |
protected void |
visitCallNode(CallNode node)
Example:
|
protected void |
visitCallParamContentNode(CallParamContentNode node) |
protected void |
visitChildren(SoyNode.ParentSoyNode<?> node)
Helper to visit all the children of a node, in order.
|
protected CodeChunk |
visitChildrenReturningCodeChunk(SoyNode.ParentSoyNode<?> node)
Visits the children of the given node, returning a
CodeChunk encapsulating its
JavaScript code. |
protected void |
visitDebuggerNode(DebuggerNode node)
Example:
|
protected void |
visitForNode(ForNode node)
Example:
|
protected void |
visitForNonemptyNode(ForNonemptyNode node) |
void |
visitForUseByAssistants(SoyNode node)
This method must only be called by assistant visitors, in particular
GenJsCodeVisitorAssistantForMsgs.
|
CodeChunk |
visitForUseByAssistantsAsCodeChunk(SoyNode node)
This method must only be called by assistant visitors, in particular
GenJsCodeVisitorAssistantForMsgs.
|
protected void |
visitIfNode(IfNode node)
Example:
|
protected void |
visitLetContentNode(LetContentNode node)
Example:
|
protected void |
visitLetValueNode(LetValueNode node)
Example:
|
protected void |
visitLogNode(LogNode node)
Example:
|
protected void |
visitMsgFallbackGroupNode(MsgFallbackGroupNode node) |
protected void |
visitMsgHtmlTagNode(MsgHtmlTagNode node) |
protected void |
visitPrintNode(PrintNode node) |
protected void |
visitSoyFileNode(SoyFileNode node)
Example:
|
protected void |
visitSoyFileSetNode(SoyFileSetNode node) |
protected void |
visitSoyNode(SoyNode node) |
protected void |
visitSwitchNode(SwitchNode node)
Example:
|
protected void |
visitTemplateNode(TemplateNode node)
Outputs a
TemplateNode, generating the function open and close, along with a a debug
template name. |
visit, visitCallBasicNode, visitCallDelegateNode, visitCallParamNode, visitCallParamValueNode, visitChildrenAllowingConcurrentModification, visitForIfemptyNode, visitHtmlAttributeNode, visitHtmlAttributeValueNode, visitHtmlCloseTagNode, visitHtmlCommentNode, visitHtmlOpenTagNode, visitIfCondNode, visitIfElseNode, visitLetNode, visitMsgNode, visitMsgPlaceholderNode, visitMsgPluralCaseNode, visitMsgPluralDefaultNode, visitMsgPluralNode, visitMsgSelectCaseNode, visitMsgSelectDefaultNode, visitMsgSelectNode, visitMsgSubstUnitNode, visitPrintDirectiveNode, visitRawTextNode, visitSwitchCaseNode, visitSwitchDefaultNode, visitTemplateBasicNode, visitTemplateDelegateNode, visitVeLogNodevisitChildren, visitChildrenAllowingConcurrentModificationprotected final SoyJsSrcOptions jsSrcOptions
protected final GenCallCodeUtils genCallCodeUtils
protected final IsComputableAsJsExprsVisitor isComputableAsJsExprsVisitor
protected GenJsExprsVisitor genJsExprsVisitor
protected TemplateRegistry templateRegistry
protected ErrorReporter errorReporter
protected TranslationContext templateTranslationContext
protected TemplateAliases templateAliases
SoyFileNode basis.protected GenJsCodeVisitor(SoyJsSrcOptions jsSrcOptions, DelTemplateNamer delTemplateNamer, GenCallCodeUtils genCallCodeUtils, IsComputableAsJsExprsVisitor isComputableAsJsExprsVisitor, CanInitOutputVarVisitor canInitOutputVarVisitor, GenJsExprsVisitor.GenJsExprsVisitorFactory genJsExprsVisitorFactory, SoyTypeRegistry typeRegistry)
public List<String> gen(SoyFileSetNode node, TemplateRegistry registry, ErrorReporter errorReporter)
@Deprecated public final List<String> exec(SoyNode node)
gen(com.google.template.soy.soytree.SoyFileSetNode, com.google.template.soy.soytree.TemplateRegistry, com.google.template.soy.error.ErrorReporter) instead.NodeVisitorexec in interface NodeVisitor<SoyNode,List<String>>exec in class AbstractNodeVisitor<SoyNode,List<String>>node - The node to execute the function on.public void visitForUseByAssistants(SoyNode node)
public CodeChunk visitForUseByAssistantsAsCodeChunk(SoyNode node)
protected void visitChildren(SoyNode.ParentSoyNode<?> node)
AbstractSoyNodeVisitorvisitChildren in class AbstractSoyNodeVisitor<List<String>>node - The parent node whose children to visit.AbstractSoyNodeVisitor.visitChildrenAllowingConcurrentModification(com.google.template.soy.soytree.SoyNode.ParentSoyNode<?>)protected void visitSoyFileSetNode(SoyFileSetNode node)
visitSoyFileSetNode in class AbstractSoyNodeVisitor<List<String>>protected JsCodeBuilder createCodeBuilder()
protected JsCodeBuilder createChildJsCodeBuilder()
protected JsCodeBuilder getJsCodeBuilder()
protected CodeChunk visitChildrenReturningCodeChunk(SoyNode.ParentSoyNode<?> node)
CodeChunk encapsulating its
JavaScript code. The chunk is indented one level from the current indent level.
This is needed to prevent infinite recursion when a visit() method needs to visit its children and return a CodeChunk.
Unlike TranslateExprNodeVisitor, GenJsCodeVisitor does not return anything as the
result of visiting a subtree. To get recursive chunk-building, we use a hack, swapping out the
JsCodeBuilder and using the unsound CodeChunk.treatRawStringAsStatementLegacyOnly(java.lang.String, java.lang.Iterable<com.google.template.soy.jssrc.dsl.GoogRequire>) API.
protected void visitSoyFileNode(SoyFileNode node)
// This file was automatically generated from my-templates.soy.
// Please don't edit this file by hand.
if (typeof boo == 'undefined') { var boo = {}; }
if (typeof boo.foo == 'undefined') { boo.foo = {}; }
...
visitSoyFileNode in class AbstractSoyNodeVisitor<List<String>>protected String getGoogModuleNamespace(String soyNamespace)
soyNamespace - The namespace as declared by the user.protected String getTemplateReturnType(TemplateNode node)
node - The template node that is being generatedprotected void visitTemplateNode(TemplateNode node)
TemplateNode, generating the function open and close, along with a a debug
template name.
If aliasing is not performed (which is always the case for V1 templates), this looks like:
my.namespace.func = function(opt_data, opt_sb) {
...
};
if (goog.DEBUG) {
my.namespace.func.soyTemplateName = 'my.namespace.func';
}
If aliasing is performed, this looks like:
function $func(opt_data, opt_sb) {
...
}
exports.func = $func;
if (goog.DEBUG) {
$func.soyTemplateName = 'my.namespace.func';
}
Note that the alias is not exactly the function name as in may conflict with a reserved
JavaScript identifier.
visitTemplateNode in class AbstractSoyNodeVisitor<List<String>>@CheckReturnValue protected CodeChunk generateFunctionBody(TemplateNode node)
protected final CodeChunk.WithValue sanitize(CodeChunk.WithValue templateBody, @Nullable SanitizedContentKind contentKind)
protected GenJsCodeVisitorAssistantForMsgs getAssistantForMsgs()
protected void visitMsgFallbackGroupNode(MsgFallbackGroupNode node)
visitMsgFallbackGroupNode in class AbstractSoyNodeVisitor<List<String>>protected void visitMsgHtmlTagNode(MsgHtmlTagNode node)
visitMsgHtmlTagNode in class AbstractSoyNodeVisitor<List<String>>protected void visitPrintNode(PrintNode node)
visitPrintNode in class AbstractSoyNodeVisitor<List<String>>protected void visitLetValueNode(LetValueNode node)
{let $boo: $foo.goo[$moo] /}
might generate
var boo35 = opt_data.foo.goo[opt_data.moo];
visitLetValueNode in class AbstractSoyNodeVisitor<List<String>>protected void visitLetContentNode(LetContentNode node)
{let $boo}
Hello {$name}
{/let}
might generate
var boo35 = 'Hello ' + opt_data.name;
visitLetContentNode in class AbstractSoyNodeVisitor<List<String>>protected void visitIfNode(IfNode node)
{if $boo.foo > 0}
...
{/if}
might generate
if (opt_data.boo.foo > 0) {
...
}
visitIfNode in class AbstractSoyNodeVisitor<List<String>>protected void generateNonExpressionIfNode(IfNode node)
TODO(user): Instead of interleaving JsCodeBuilders like this, consider refactoring GenJsCodeVisitor to return CodeChunks for each sub-Template level SoyNode.
protected void visitSwitchNode(SwitchNode node)
{switch $boo}
{case 0}
...
{case 1, 2}
...
{default}
...
{/switch}
might generate
switch (opt_data.boo) {
case 0:
...
break;
case 1:
case 2:
...
break;
default:
...
}
visitSwitchNode in class AbstractSoyNodeVisitor<List<String>>protected void visitForNode(ForNode node)
{for $foo in $boo.foos}
...
{ifempty}
...
{/for}
might generate
var foo2List = opt_data.boo.foos;
var foo2ListLen = foo2List.length;
if (foo2ListLen > 0) {
...
} else {
...
}
visitForNode in class AbstractSoyNodeVisitor<List<String>>protected void visitForNonemptyNode(ForNonemptyNode node)
visitForNonemptyNode in class AbstractSoyNodeVisitor<List<String>>protected void visitCallNode(CallNode node)
{call some.func data="all" /}
{call some.func data="$boo.foo" /}
{call some.func}
{param goo: 88 /}
{/call}
{call some.func data="$boo"}
{param goo}
Hello {$name}
{/param}
{/call}
might generate
output += some.func(opt_data);
output += some.func(opt_data.boo.foo);
output += some.func({goo: 88});
output += some.func(soy.$$assignDefaults({goo: 'Hello ' + opt_data.name}, opt_data.boo);
visitCallNode in class AbstractSoyNodeVisitor<List<String>>protected void visitCallParamContentNode(CallParamContentNode node)
visitCallParamContentNode in class AbstractSoyNodeVisitor<List<String>>protected void visitLogNode(LogNode node)
{log}Blah {$boo}.{/log}
might generate
window.console.log('Blah ' + opt_data.boo + '.');
If the log msg is not computable as JS exprs, then it will be built in a local var logMsg_s##, e.g.
var logMsg_s14 = ... window.console.log(logMsg_s14);
visitLogNode in class AbstractSoyNodeVisitor<List<String>>protected void visitDebuggerNode(DebuggerNode node)
{debugger}
generates
debugger;
visitDebuggerNode in class AbstractSoyNodeVisitor<List<String>>protected void visitSoyNode(SoyNode node)
visitSoyNode in class AbstractSoyNodeVisitor<List<String>>node - the visited node.@CheckReturnValue protected CodeChunk genParamTypeChecks(TemplateNode node)
node - the template node.