use of com.google.template.soy.jssrc.SoyJsSrcOptions in project closure-templates by google.
the class SoyIncrementalDomSrcOptions method toJsSrcOptions.
/**
* Convert to {@link SoyJsSrcOptions}. This is necessary since {@code incrementaldomsrc} reuses
* lots of {@code jssrc} which needs to interact with this object.
*/
SoyJsSrcOptions toJsSrcOptions() {
SoyJsSrcOptions jsSrcOptions = new SoyJsSrcOptions();
jsSrcOptions.setShouldAllowDeprecatedSyntax(false);
jsSrcOptions.setShouldProvideRequireSoyNamespaces(false);
jsSrcOptions.setShouldProvideRequireJsFunctions(false);
jsSrcOptions.setShouldProvideBothSoyNamespacesAndJsFunctions(false);
// Only goog.module generation supported
jsSrcOptions.setShouldGenerateGoogModules(true);
jsSrcOptions.setShouldGenerateGoogMsgDefs(true);
jsSrcOptions.setGoogMsgsAreExternal(true);
jsSrcOptions.setBidiGlobalDir(0);
jsSrcOptions.setUseGoogIsRtlForBidiGlobalDir(true);
return jsSrcOptions;
}
use of com.google.template.soy.jssrc.SoyJsSrcOptions in project closure-templates by google.
the class JspbTest method testHeaderParamFieldImport.
// Proto import tests
/**
* Test to check that soy code that accesses proto fields, correctly generate JS that includes
* imports for the field types.
*/
@Test
public void testHeaderParamFieldImport() {
Injector injector = Guice.createInjector(new SoyModule());
SoyJsSrcOptions jsSrcOptions = new SoyJsSrcOptions();
jsSrcOptions.setShouldProvideRequireSoyNamespaces(true);
GenJsCodeVisitor genJsCodeVisitor = JsSrcMain.createVisitor(jsSrcOptions, injector.getInstance(SoyTypeRegistry.class));
genJsCodeVisitor.jsCodeBuilder = new JsCodeBuilder();
String testFileContent = "{namespace boo.foo}\n" + "\n" + "/** */\n" + "{template .goo autoescape=\"deprecated-noncontextual\"}\n" + " {@param moo : example.ExampleExtendable}\n" + " {$moo.someExtensionField}\n" + "{/template}\n";
ParseResult parseResult = SoyFileSetParserBuilder.forFileContents(testFileContent).declaredSyntaxVersion(SyntaxVersion.V2_0).typeRegistry(REGISTRY).parse();
// Verify that the import symbol got required.
String expectedJsFileContentStart = "// This file was automatically generated from no-path.\n" + "// Please don't edit this file by hand.\n" + "\n" + "/**\n" + " * @fileoverview Templates in namespace boo.foo.\n" + " * @public\n" + " */\n" + "\n" + "goog.provide('boo.foo');\n" + "\n" + "goog.require('proto.example.ExampleExtendable');\n" + "goog.require('proto.example.SomeExtension');\n" + "goog.require('soy.asserts');\n" + "\n" + "\n" + "/**\n" + " * @param {boo.foo.goo.Params} opt_data\n" + " * @param {Object<string, *>=} opt_ijData\n" + " * @param {Object<string, *>=} opt_ijData_deprecated\n" + " * @return {string}\n" + " * @suppress {checkTypes|uselessCode}\n" + " */\n" + "boo.foo.goo = function(opt_data, opt_ijData, opt_ijData_deprecated) {\n" + " opt_ijData = opt_ijData_deprecated || opt_ijData;\n" + " var $tmp = opt_data.moo.$jspbMessageInstance || opt_data.moo;\n" + " /** @type {proto.example.ExampleExtendable} */\n" + " var moo = soy.asserts.assertType(" + "$tmp instanceof proto.example.ExampleExtendable, " + "'moo', $tmp, 'proto.example.ExampleExtendable');\n" + " return '' + moo.getExtension(proto.example.SomeExtension.someExtensionField);\n" + "};\n" + "/**\n" + " * @typedef {{\n" + " * moo: proto.example.ExampleExtendable,\n" + " * }}\n" + " */\n" + "boo.foo.goo.Params;\n" + "if (goog.DEBUG) {\n" + " boo.foo.goo.soyTemplateName = 'boo.foo.goo';\n" + "}\n" + "";
List<String> jsFilesContents = genJsCodeVisitor.gen(parseResult.fileSet(), parseResult.registry(), ErrorReporter.exploding());
assertThat(jsFilesContents.get(0)).isEqualTo(expectedJsFileContentStart);
}
use of com.google.template.soy.jssrc.SoyJsSrcOptions in project closure-templates by google.
the class TranslateExprNodeVisitorTest method testMapLiteral.
@Test
public void testMapLiteral() {
// ------ Unquoted keys. ------
assertThatSoyExpr("[:]").generatesCode("{};");
assertThatSoyExpr("['aaa': 123, 'bbb': 'blah']").generatesCode("{aaa: 123, bbb: 'blah'};");
assertThatSoyExpr("['aaa': $foo, 'bbb': 'blah']").generatesCode("{aaa: opt_data.foo, bbb: 'blah'};");
assertThatSoyExpr("['aaa': ['bbb': 'blah']]").generatesCode("{aaa: {bbb: 'blah'}};");
// ------ Quoted keys. ------
assertThatSoyExpr("quoteKeysIfJs([:])").generatesCode("{};");
assertThatSoyExpr("quoteKeysIfJs( ['aaa': $foo, 'bbb': 'blah'] )").generatesCode("{'aaa': opt_data.foo, 'bbb': 'blah'};");
assertThatSoyExpr("quoteKeysIfJs(['aaa': 123, $boo: $foo])").generatesCode("var $tmp = {'aaa': 123};", "$tmp[soy.$$checkLegacyObjectMapLiteralKey(opt_data.boo)] = opt_data.foo;");
assertThatSoyExpr("quoteKeysIfJs([$boo: $foo, $goo[0]: 123])").withInitialLocalVarTranslations(LOCAL_VAR_TRANSLATIONS).generatesCode("var $tmp = {};", "$tmp[soy.$$checkLegacyObjectMapLiteralKey(opt_data.boo)] = opt_data.foo;", "$tmp[soy.$$checkLegacyObjectMapLiteralKey(gooData8[0])] = 123;");
assertThatSoyExpr("quoteKeysIfJs(['aaa': ['bbb': 'blah']])").generatesCode("{'aaa': {bbb: 'blah'}};");
// ------ Errors. ------
// Non-string key is error.
assertThatSoyExpr("[0: 123, 1: 'oops']").causesErrors("Keys in map literals cannot be constants (found constant '0').", "Keys in map literals cannot be constants (found constant '1').");
SoyJsSrcOptions noCompiler = new SoyJsSrcOptions();
SoyJsSrcOptions withCompiler = new SoyJsSrcOptions();
withCompiler.setShouldProvideRequireSoyNamespaces(true);
assertThatSoyExpr("quoteKeysIfJs(['0': 123, '1': $foo])").withJsSrcOptions(noCompiler).generatesCode("{'0': 123, '1': opt_data.foo};");
// Non-identifier key without quoteKeysIfJs() is an error
assertThatSoyExpr("['0': 123, '1': '123']").withJsSrcOptions(withCompiler).causesErrors("Map literal with non-identifier key '0' must be wrapped in quoteKeysIfJs().", "Map literal with non-identifier key '1' must be wrapped in quoteKeysIfJs().");
assertThatSoyExpr("quoteKeysIfJs(['aaa': 123, $boo: $foo])").withJsSrcOptions(noCompiler).generatesCode("var $tmp = {'aaa': 123};", "$tmp[soy.$$checkLegacyObjectMapLiteralKey(opt_data.boo)] = opt_data.foo;");
// Expression key without quoteKeysIfJs() is an error.
assertThatSoyExpr("['aaa': 123, $boo: $foo, $moo: $goo]").withJsSrcOptions(withCompiler).causesErrors("Expression key '$boo' in map literal must be wrapped in quoteKeysIfJs().", "Expression key '$moo' in map literal must be wrapped in quoteKeysIfJs().");
}
use of com.google.template.soy.jssrc.SoyJsSrcOptions in project closure-templates by google.
the class GenJsCodeVisitorTest method setUp.
@Before
public void setUp() {
jsSrcOptions = new SoyJsSrcOptions();
genJsCodeVisitor = JsSrcMain.createVisitor(jsSrcOptions, INJECTOR.getInstance(SoyTypeRegistry.class));
genJsCodeVisitor.templateAliases = TEMPLATE_ALIASES;
}
use of com.google.template.soy.jssrc.SoyJsSrcOptions in project closure-templates by google.
the class IncrementalDomSrcMain method genJsSrc.
/**
* Generates Incremental DOM JS source code given a Soy parse tree, an options object, and an
* optional bundle of translated messages.
*
* @param soyTree The Soy parse tree to generate JS source code for.
* @param registry The template registry that contains all the template information.
* @param options The compilation options relevant to this backend.
* @param errorReporter The Soy error reporter that collects errors during code generation.
* @return A list of strings where each string represents the JS source code that belongs in one
* JS file. The generated JS files correspond one-to-one to the original Soy source files.
*/
public List<String> genJsSrc(SoyFileSetNode soyTree, TemplateRegistry registry, SoyIncrementalDomSrcOptions options, ErrorReporter errorReporter) {
SoyJsSrcOptions incrementalJSSrcOptions = options.toJsSrcOptions();
try (GuiceSimpleScope.InScope inScope = apiCallScope.enter()) {
// Seed the scoped parameters.
BidiGlobalDir bidiGlobalDir = SoyBidiUtils.decodeBidiGlobalDirFromJsOptions(incrementalJSSrcOptions.getBidiGlobalDir(), incrementalJSSrcOptions.getUseGoogIsRtlForBidiGlobalDir());
ApiCallScopeUtils.seedSharedParams(inScope, null, /* msgBundle */
bidiGlobalDir);
// Do the code generation.
new HtmlContextVisitor(errorReporter).exec(soyTree);
// Return an empty list here, {@code SoyFileSet} will throw an exception.
if (errorReporter.hasErrors()) {
return Collections.emptyList();
}
new UnescapingVisitor().exec(soyTree);
new RemoveUnnecessaryEscapingDirectives().run(soyTree);
// some of the above passes may slice up raw text nodes, recombine them.
new CombineConsecutiveRawTextNodesPass().run(soyTree);
return createVisitor(incrementalJSSrcOptions, typeRegistry).gen(soyTree, registry, errorReporter);
}
}
Aggregations