Search in sources :

Example 31 with JsExpr

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

Example 32 with JsExpr

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

Example 33 with JsExpr

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

Example 34 with JsExpr

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

Example 35 with JsExpr

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);
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Matcher(java.util.regex.Matcher) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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