use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class V1JsExprTranslator method getLocalVarTranslation.
/**
* Gets the translated expression for an in-scope local variable (or special "variable" derived
* from a foreach-loop var), or null if not found.
*
* @param ident The Soy local variable to translate.
* @param mappings The replacement JS expressions for the local variables
* (and foreach-loop special functions) current in scope.
* @return The translated string for the given variable, or null if not found.
*
* TODO(user): change the return type to CodeChunk.WithValue.
*/
@Nullable
private static String getLocalVarTranslation(String ident, SoyToJsVariableMappings mappings) {
CodeChunk.WithValue translation = mappings.maybeGet(ident);
if (translation == null) {
return null;
}
JsExpr asExpr = translation.assertExpr();
return asExpr.getPrecedence() != Integer.MAX_VALUE ? "(" + asExpr.getText() + ")" : asExpr.getText();
}
use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class IsNullFunctionTest method testComputeForJsSrc.
@Test
public void testComputeForJsSrc() {
JsExpr expr = new JsExpr("JS_CODE", Integer.MAX_VALUE);
assertThat(IS_NULL.computeForJsSrc(ImmutableList.of(expr))).isEqualTo(new JsExpr("JS_CODE == null", Operator.EQUAL.getPrecedence()));
}
use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class MapToLegacyObjectMapFunctionTest method computeForJsSrc.
@Test
public void computeForJsSrc() {
JsExpr map = new JsExpr("map", Integer.MAX_VALUE);
JsExpr legacyObjectMap = MAP_TO_LEGACY_OBJECT_MAP.computeForJsSrc(ImmutableList.of(map));
assertThat(legacyObjectMap.getText()).isEqualTo("soy.map.$$mapToLegacyObjectMap(map)");
}
use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class RoundFunctionTest method testComputeForJsSrc.
@Test
public void testComputeForJsSrc() {
RoundFunction roundFunction = new RoundFunction();
JsExpr floatExpr = new JsExpr("FLOAT_JS_CODE", Integer.MAX_VALUE);
assertThat(roundFunction.computeForJsSrc(ImmutableList.of(floatExpr))).isEqualTo(new JsExpr("Math.round(FLOAT_JS_CODE)", Integer.MAX_VALUE));
JsExpr numDigitsAfterPtExpr = new JsExpr("0", Integer.MAX_VALUE);
assertThat(roundFunction.computeForJsSrc(ImmutableList.of(floatExpr, numDigitsAfterPtExpr))).isEqualTo(new JsExpr("Math.round(FLOAT_JS_CODE)", Integer.MAX_VALUE));
numDigitsAfterPtExpr = new JsExpr("4", Integer.MAX_VALUE);
assertThat(roundFunction.computeForJsSrc(ImmutableList.of(floatExpr, numDigitsAfterPtExpr))).isEqualTo(new JsExpr("Math.round(FLOAT_JS_CODE * 10000) / 10000", Operator.DIVIDE_BY.getPrecedence()));
numDigitsAfterPtExpr = new JsExpr("-2", Operator.NEGATIVE.getPrecedence());
assertThat(roundFunction.computeForJsSrc(ImmutableList.of(floatExpr, numDigitsAfterPtExpr))).isEqualTo(new JsExpr("Math.round(FLOAT_JS_CODE / 100) * 100", Operator.TIMES.getPrecedence()));
numDigitsAfterPtExpr = new JsExpr("NUM_DIGITS_JS_CODE", Integer.MAX_VALUE);
assertThat(roundFunction.computeForJsSrc(ImmutableList.of(floatExpr, numDigitsAfterPtExpr))).isEqualTo(new JsExpr("Math.round(FLOAT_JS_CODE * Math.pow(10, NUM_DIGITS_JS_CODE)) /" + " Math.pow(10, NUM_DIGITS_JS_CODE)", Operator.DIVIDE_BY.getPrecedence()));
}
use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.
the class StrIndexOfFunctionTest method testComputeForJsSrc_lowPrecedenceArg.
@Test
public void testComputeForJsSrc_lowPrecedenceArg() {
StrIndexOfFunction strIndexOf = new StrIndexOfFunction();
JsExpr arg0 = new JsExpr("'foo' + 'bar'", Operator.PLUS.getPrecedence());
JsExpr arg1 = new JsExpr("'ba' + 'r'", Operator.PLUS.getPrecedence());
assertThat(strIndexOf.computeForJsSrc(ImmutableList.of(arg0, arg1))).isEqualTo(new JsExpr("('' + ('foo' + 'bar')).indexOf('' + ('ba' + 'r'))", Integer.MAX_VALUE));
}
Aggregations