public class GenJsCodeVisitor extends AbstractHtmlSoyNodeVisitor<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 JsExprTranslator |
jsExprTranslator
Instance of JsExprTranslator to use.
|
protected SoyJsSrcOptions |
jsSrcOptions
The options for generating JS source code.
|
protected Deque<Map<String,JsExpr>> |
localVarTranslations
The current stack of replacement JS expressions for the local variables (and foreach-loop
special functions) current in scope.
|
protected TemplateAliases |
templateAliases
Used for looking up the local name for a given template call to a fully qualified template
name.
|
protected TemplateRegistry |
templateRegistry |
| Modifier | Constructor and Description |
|---|---|
protected |
GenJsCodeVisitor(SoyJsSrcOptions jsSrcOptions,
JsExprTranslator jsExprTranslator,
DelTemplateNamer delTemplateNamer,
GenCallCodeUtils genCallCodeUtils,
IsComputableAsJsExprsVisitor isComputableAsJsExprsVisitor,
CanInitOutputVarVisitor canInitOutputVarVisitor,
GenJsExprsVisitor.GenJsExprsVisitorFactory genJsExprsVisitorFactory,
GenDirectivePluginRequiresVisitor genDirectivePluginRequiresVisitor,
SoyTypeOps typeOps) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addCodeToRequireGeneralDeps(SoyFileNode soyFile)
Helper for visitSoyFileNode(SoyFileNode) to add code to require general dependencies.
|
protected void |
addCodeToRequireGoogModuleDeps(SoyFileNode soyFile)
Helper for visitSoyFileNode(SoyFileNode) to add code to require goog.module dependencies.
|
protected void |
addGoogRequire(String namespace,
boolean suppressExtra)
Emit goog.require for namespace if namespace has not already been emitted.
|
protected CodeBuilder<JsExpr> |
createCodeBuilder() |
List<String> |
exec(SoyNode node)
|
List<String> |
gen(SoyFileSetNode node,
TemplateRegistry registry,
ErrorReporter errorReporter) |
protected void |
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 void |
genParamTypeChecks(TemplateNode node)
Generate code to verify the runtime types of the input params.
|
protected GenJsCodeVisitorAssistantForMsgs |
getAssistantForMsgs() |
protected String |
getGoogModuleNamespace(String soyNamespace) |
protected CodeBuilder<JsExpr> |
getJsCodeBuilder() |
protected String |
getJsTypeName(SoyType type)
Returns the name of the JS type used to represent the given SoyType at runtime.
|
protected String |
getTemplateReturnType(TemplateNode node) |
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 void |
visitDebuggerNode(DebuggerNode node)
Example:
|
protected void |
visitForeachNode(ForeachNode node)
Example:
|
protected void |
visitForeachNonemptyNode(ForeachNonemptyNode node)
Example:
|
protected void |
visitForNode(ForNode node)
Example:
|
void |
visitForUseByAssistants(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, visitHtmlAttributeNode, visitHtmlCloseTagNode, visitHtmlOpenTagEndNode, visitHtmlOpenTagNode, visitHtmlOpenTagStartNode, visitHtmlVoidTagNodevisitCallBasicNode, visitCallDelegateNode, visitCallParamNode, visitCallParamValueNode, visitChildrenAllowingConcurrentModification, visitCssNode, visitForeachIfemptyNode, visitIfCondNode, visitIfElseNode, visitLetNode, visitLoopNode, visitMsgNode, visitMsgPlaceholderNode, visitMsgPluralCaseNode, visitMsgPluralDefaultNode, visitMsgPluralNode, visitMsgSelectCaseNode, visitMsgSelectDefaultNode, visitMsgSelectNode, visitMsgSubstUnitNode, visitPrintDirectiveNode, visitRawTextNode, visitSwitchCaseNode, visitSwitchDefaultNode, visitTemplateBasicNode, visitTemplateDelegateNode, visitXidNodevisitChildren, visitChildrenAllowingConcurrentModificationprotected final SoyJsSrcOptions jsSrcOptions
protected final JsExprTranslator jsExprTranslator
protected final GenCallCodeUtils genCallCodeUtils
protected final IsComputableAsJsExprsVisitor isComputableAsJsExprsVisitor
protected Deque<Map<String,JsExpr>> localVarTranslations
protected GenJsExprsVisitor genJsExprsVisitor
protected TemplateRegistry templateRegistry
protected ErrorReporter errorReporter
protected TemplateAliases templateAliases
SoyFileNode basis.@Inject protected GenJsCodeVisitor(SoyJsSrcOptions jsSrcOptions, JsExprTranslator jsExprTranslator, DelTemplateNamer delTemplateNamer, GenCallCodeUtils genCallCodeUtils, IsComputableAsJsExprsVisitor isComputableAsJsExprsVisitor, CanInitOutputVarVisitor canInitOutputVarVisitor, GenJsExprsVisitor.GenJsExprsVisitorFactory genJsExprsVisitorFactory, GenDirectivePluginRequiresVisitor genDirectivePluginRequiresVisitor, SoyTypeOps typeOps)
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)
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 CodeBuilder<JsExpr> createCodeBuilder()
protected CodeBuilder<JsExpr> getJsCodeBuilder()
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 void addCodeToRequireGeneralDeps(SoyFileNode soyFile)
soyFile - The node we're visiting.protected void addGoogRequire(String namespace, boolean suppressExtra)
namespace - The JS namespace to require if not already required.suppressExtra - Whether to add a @suppress {extraRequire} annotation for requires
that may be unused.protected void addCodeToRequireGoogModuleDeps(SoyFileNode soyFile)
soyFile - The node we're visiting.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>>protected void generateFunctionBody(TemplateNode node)
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)
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 visitForeachNode(ForeachNode node)
{foreach $foo in $boo.foos}
...
{ifempty}
...
{/foreach}
might generate
var fooList2 = opt_data.boo.foos;
var fooListLen2 = fooList2.length;
if (fooListLen2 > 0) {
...
} else {
...
}
visitForeachNode in class AbstractSoyNodeVisitor<List<String>>protected void visitForeachNonemptyNode(ForeachNonemptyNode node)
{foreach $foo in $boo.foos}
...
{/foreach}
might generate
for (var fooIndex2 = 0; fooIndex2 < fooListLen2; fooIndex2++) {
var fooData2 = fooList2[fooIndex2];
...
}
visitForeachNonemptyNode in class AbstractSoyNodeVisitor<List<String>>protected void visitForNode(ForNode node)
{for $i in range(1, $boo)}
...
{/for}
might generate
var iLimit4 = opt_data.boo;
for (var i4 = 1; i4 < iLimit4; i4++) {
...
}
visitForNode 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.protected String getJsTypeName(SoyType type)
protected void genParamTypeChecks(TemplateNode node)
node - the template node.