use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class AugmentMapFunction method computeForJsSrc.
@Override
public JsExpr computeForJsSrc(List<JsExpr> args) {
JsExpr arg0 = args.get(0);
JsExpr arg1 = args.get(1);
String exprText = "soy.$$augmentMap(" + arg0.getText() + ", " + arg1.getText() + ")";
return new JsExpr(exprText, Integer.MAX_VALUE);
}
use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class GenJsExprsVisitorTest method assertGeneratedJsExprs.
/**
* @param indicesToNode Series of indices for walking down to the node we want to test.
*/
private static void assertGeneratedJsExprs(String soyCode, List<JsExpr> expectedJsExprs, int... indicesToNode) {
List<CodeChunk.WithValue> actualChunks = generateChunks(soyCode, indicesToNode);
List<JsExpr> actualJsExprs = new ArrayList<>();
for (CodeChunk.WithValue chunk : actualChunks) {
// TODO(user): Fix tests to work with CodeChunks
actualJsExprs.add(chunk.assertExpr());
}
assertThat(actualJsExprs).hasSize(expectedJsExprs.size());
for (int i = 0; i < expectedJsExprs.size(); i++) {
JsExpr expectedJsExpr = expectedJsExprs.get(i);
JsExpr actualJsExpr = actualJsExprs.get(i);
assertThat(actualJsExpr.getText()).isEqualTo(expectedJsExpr.getText());
assertThat(actualJsExpr.getPrecedence()).isEqualTo(expectedJsExpr.getPrecedence());
}
}
use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class VeLogFunctionTest method testComputeForJsSrcWithData.
@Test
public void testComputeForJsSrcWithData() {
VeLogFunction function = VeLogFunction.INSTANCE;
JsExpr idExpr = new JsExpr("1", Integer.MAX_VALUE);
JsExpr dataExpr = new JsExpr("new proto.soy.compiler.test.Foo()", Integer.MAX_VALUE);
assertThat(function.computeForJsSrc(ImmutableList.of(idExpr, dataExpr))).isEqualTo(new JsExpr("soy.velog.$$getLoggingAttribute('1', new proto.soy.compiler.test.Foo(), false)", Integer.MAX_VALUE));
}
use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class VeLogFunctionTest method testComputeForJsSrcWithLogOnly.
@Test
public void testComputeForJsSrcWithLogOnly() {
VeLogFunction function = VeLogFunction.INSTANCE;
JsExpr idExpr = new JsExpr("1", Integer.MAX_VALUE);
JsExpr dataExpr = new JsExpr("new proto.soy.compiler.test.Foo()", Integer.MAX_VALUE);
JsExpr logonlyExpr = new JsExpr("true", Integer.MAX_VALUE);
assertThat(function.computeForJsSrc(ImmutableList.of(idExpr, dataExpr, logonlyExpr))).isEqualTo(new JsExpr("soy.velog.$$getLoggingAttribute('1', new proto.soy.compiler.test.Foo(), true)", Integer.MAX_VALUE));
}
use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class V1JsExprTranslator method translateToJsExpr.
/**
* Helper function to generate code for a JS expression found in a Soy tag.
* Replaces all variables, data references, and special function calls in
* the given expression text with the appropriate generated code. E.g.
* <pre>
* $boo.foo + ($goo.moo).doIt()
* </pre>
* might become
* <pre>
* opt_data.boo.foo + (gooData0.moo).doIt()
* </pre>
*
* @param soyExpr The expression text to generate code for.
* @param sourceLocation Source location of the expression text.
* @param variableMappings -
* @param errorReporter For reporting syntax errors.
* @return The resulting expression code after the necessary substitutions.
*/
@VisibleForTesting
static JsExpr translateToJsExpr(String soyExpr, SourceLocation sourceLocation, SoyToJsVariableMappings variableMappings, ErrorReporter errorReporter) {
StringBuffer jsExprTextSb = new StringBuffer();
Matcher matcher = VAR_OR_BOOL_OP_OR_SOY_FUNCTION.matcher(soyExpr);
while (matcher.find()) {
String group = matcher.group();
Matcher var = VAR.matcher(group);
if (var.matches()) {
matcher.appendReplacement(jsExprTextSb, Matcher.quoteReplacement(translateVar(variableMappings, var)));
} else if (BOOL_OP_RE.matcher(group).matches()) {
errorReporter.report(matcherLocation(matcher, sourceLocation), UNSUPPORTED_OPERATOR);
} else {
errorReporter.report(matcherLocation(matcher, sourceLocation), UNSUPPORTED_FUNCTION);
}
}
matcher.appendTail(jsExprTextSb);
String jsExprText = jsExprTextSb.toString();
// Note: There is a JavaScript language quirk that requires all Unicode Foramt characters
// (Unicode category "Cf") to be escaped in JS strings. Therefore, we call
// JsSrcUtils.escapeUnicodeFormatChars() on the expression text in case it contains JS strings.
jsExprText = JsSrcUtils.escapeUnicodeFormatChars(jsExprText);
// Use a high precedence to ensure that everything with the v1Expression is grouped together
return new JsExpr('(' + jsExprText + ')', /* precedence= */
Integer.MAX_VALUE);
}
Aggregations