Search in sources :

Example 36 with JsExpr

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();
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) Nullable(javax.annotation.Nullable)

Example 37 with JsExpr

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()));
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Test(org.junit.Test)

Example 38 with JsExpr

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)");
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Test(org.junit.Test)

Example 39 with JsExpr

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()));
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Test(org.junit.Test)

Example 40 with JsExpr

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));
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Test(org.junit.Test)

Aggregations

JsExpr (com.google.template.soy.jssrc.restricted.JsExpr)64 Test (org.junit.Test)42 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)3 ImmutableList (com.google.common.collect.ImmutableList)2 RequiresCollector (com.google.template.soy.jssrc.dsl.CodeChunk.RequiresCollector)2 SoyLibraryAssistedJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 SanitizedContent (com.google.template.soy.data.SanitizedContent)1 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)1 SoyValue (com.google.template.soy.data.SoyValue)1 UnsafeSanitizedContentOrdainer (com.google.template.soy.data.UnsafeSanitizedContentOrdainer)1 SoyString (com.google.template.soy.data.restricted.SoyString)1 StringData (com.google.template.soy.data.restricted.StringData)1 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)1 StringNode (com.google.template.soy.exprtree.StringNode)1 WithValue (com.google.template.soy.jssrc.dsl.CodeChunk.WithValue)1 SoyJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective)1