Example #1
0
 WithScope enter() {
   // TODO(lukes): this isn't right, re-entering the scope shouldn't retrigger injection of
   // items, we need an explicit detach api.
   WithScope withScope = scope.enter();
   ApiCallScopeUtils.seedSharedParams(scope, dir, localeString);
   return withScope;
 }
  /**
   * 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 jsSrcOptions The compilation options relevant to this backend.
   * @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.
   * @throws SoySyntaxException If a syntax error is found.
   */
  public List<String> genJsSrc(SoyFileSetNode soyTree, SoyJsSrcOptions jsSrcOptions)
      throws SoySyntaxException {

    // Generate code with the opt_ijData param if either (a) the user specified the compiler flag
    // --isUsingIjData or (b) any of the Soy code in the file set references injected data.
    boolean isUsingIjData =
        jsSrcOptions.isUsingIjData() || new IsUsingIjDataVisitor().exec(soyTree);

    // Make sure that we don't try to use goog.i18n.bidi when we aren't supposed to use Closure.
    Preconditions.checkState(
        !jsSrcOptions.getUseGoogIsRtlForBidiGlobalDir()
            || jsSrcOptions.shouldProvideRequireSoyNamespaces()
            || jsSrcOptions.shouldProvideRequireJsFunctions(),
        "Do not specify useGoogIsRtlForBidiGlobalDir without either"
            + " shouldProvideRequireSoyNamespaces or shouldProvideRequireJsFunctions.");

    try (WithScope withScope = apiCallScope.enter()) {
      // Seed the scoped parameters.
      apiCallScope.seed(SoyJsSrcOptions.class, jsSrcOptions);
      apiCallScope.seed(Key.get(Boolean.class, IsUsingIjData.class), isUsingIjData);
      BidiGlobalDir bidiGlobalDir =
          SoyBidiUtils.decodeBidiGlobalDirFromJsOptions(
              jsSrcOptions.getBidiGlobalDir(), jsSrcOptions.getUseGoogIsRtlForBidiGlobalDir());
      ApiCallScopeUtils.seedSharedParams(apiCallScope, null /* msgBundle */, bidiGlobalDir);

      // TODO(sparhami) figure out how to deal with msg nodes - need to support some sort of
      // innerHTML,
      // which means we need autoescaping for just those subtrees.
      //      new ReplaceMsgsWithGoogMsgsVisitor(errorReporter).exec(soyTree);
      //      new MoveGoogMsgDefNodesEarlierVisitor(errorReporter).exec(soyTree);
      //      Preconditions.checkState(
      //          bidiGlobalDir != null,
      //          "If enabling shouldGenerateGoogMsgDefs, must also set bidi global
      // directionality.");

      // Do the code generation.
      optimizeBidiCodeGenVisitorProvider.get().exec(soyTree);
      simplifyVisitor.exec(soyTree);

      new HtmlTransformVisitor(errorReporter).exec(soyTree);
      IncrementalDomOutputOptimizers.collapseOpenTags(soyTree);
      IncrementalDomOutputOptimizers.collapseElements(soyTree);

      return genIncrementalDomCodeVisitorProvider.get().exec(soyTree);
    }
  }