<commit_before>/*
 * Copyright 2016 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.javascript.jscomp;

import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES8;
import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES8_MODULES;
import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES_NEXT;

import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.jscomp.PassFactory.HotSwapPassFactory;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.Node;
import java.util.List;

/**
 * Provides a single place to manage transpilation passes.
 */
public class TranspilationPasses {
  private TranspilationPasses() {}

  public static void addEs6ModulePass(
      List<PassFactory> passes,
      final PreprocessorSymbolTable.CachedInstanceFactory preprocessorTableFactory) {
    passes.add(
        new HotSwapPassFactory("es6RewriteModule") {
          @Override
          protected HotSwapCompilerPass create(AbstractCompiler compiler) {
            preprocessorTableFactory.maybeInitialize(compiler);
            return new Es6RewriteModules(
                compiler,
                preprocessorTableFactory.getInstanceOrNull(),
                compiler.getOptions().processCommonJSModules,
                compiler.getOptions().moduleResolutionMode);
          }

          @Override
          protected FeatureSet featureSet() {
            return ES_NEXT;
          }
        });
  }

  public static void addEs6ModuleToCjsPass(List<PassFactory> passes) {
    passes.add(es6RewriteModuleToCjs);
  }

  public static void addEs2018Passes(List<PassFactory> passes) {
    passes.add(rewriteObjRestSpread);
  }

  public static void addEs2017Passes(List<PassFactory> passes) {
    // Trailing commas in parameter lists are flagged as present by the parser,
    // but never actually represented in the AST.
    // The only thing we need to do is mark them as not present in the AST.
    passes.add(
        createFeatureRemovalPass(
            "markTrailingCommasInParameterListsRemoved", Feature.TRAILING_COMMA_IN_PARAM_LIST));
    passes.add(rewriteAsyncFunctions);
  }

  public static void addEs2016Passes(List<PassFactory> passes) {
    passes.add(convertEs7ToEs6);
  }

  public static void addEs6PreTypecheckPasses(List<PassFactory> passes, CompilerOptions options) {
    // Binary and octal literals are effectively transpiled by the parser.
    // There's no transpilation we can do for the new regexp flags.
    passes.add(
        createFeatureRemovalPass(
            "markEs6FeaturesNotRequiringTranspilationAsRemoved",
            Feature.BINARY_LITERALS,
            Feature.OCTAL_LITERALS,
            Feature.REGEXP_FLAG_U,
            Feature.REGEXP_FLAG_Y));
    passes.add(es6NormalizeShorthandProperties);
    passes.add(es6ConvertSuper);
    passes.add(es6RenameVariablesInParamLists);
    passes.add(es6SplitVariableDeclarations);
    passes.add(es6RewriteDestructuring);
    passes.add(es6RewriteArrowFunction);
    passes.add(es6ExtractClasses);
    passes.add(es6RewriteClass);
    passes.add(es6InjectRuntimeLibraries);
    if (!options.checksOnly) {
      // Don't run these passes in checksOnly mode since all the typechecking & checks passes
      // support the transpiled features.
      // TODO(b/73387406): Move each pass above here temporarily, then into
      // addEs6PostCheck Passes once the pass supports propagating type information
      passes.add(es6RewriteRestAndSpread);
    }
  }

  /** Deprecated: use addEs6PostCheckPasses instead. */<commit_msg>Automated g4 rollback of changelist 200075148.<commit_after>/*
 * Copyright 2016 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.javascript.jscomp;

import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES8;
import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES8_MODULES;
import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES_NEXT;

import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.jscomp.PassFactory.HotSwapPassFactory;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.Node;
import java.util.List;

/**
 * Provides a single place to manage transpilation passes.
 */
public class TranspilationPasses {
  private TranspilationPasses() {}

  public static void addEs6ModulePass(
      List<PassFactory> passes,
      final PreprocessorSymbolTable.CachedInstanceFactory preprocessorTableFactory) {
    passes.add(
        new HotSwapPassFactory("es6RewriteModule") {
          @Override
          protected HotSwapCompilerPass create(AbstractCompiler compiler) {
            preprocessorTableFactory.maybeInitialize(compiler);
            return new Es6RewriteModules(
                compiler,
                preprocessorTableFactory.getInstanceOrNull(),
                compiler.getOptions().processCommonJSModules,
                compiler.getOptions().moduleResolutionMode);
          }

          @Override
          protected FeatureSet featureSet() {
            return ES_NEXT;
          }
        });
  }

  public static void addEs6ModuleToCjsPass(List<PassFactory> passes) {
    passes.add(es6RewriteModuleToCjs);
  }

  public static void addEs2018Passes(List<PassFactory> passes) {
    passes.add(rewriteObjRestSpread);
  }

  public static void addEs2017Passes(List<PassFactory> passes) {
    // Trailing commas in parameter lists are flagged as present by the parser,
    // but never actually represented in the AST.
    // The only thing we need to do is mark them as not present in the AST.
    passes.add(
        createFeatureRemovalPass(
            "markTrailingCommasInParameterListsRemoved", Feature.TRAILING_COMMA_IN_PARAM_LIST));
    passes.add(rewriteAsyncFunctions);
  }

  public static void addEs2016Passes(List<PassFactory> passes) {
    passes.add(convertEs7ToEs6);
  }

  public static void addEs6PreTypecheckPasses(List<PassFactory> passes, CompilerOptions options) {
    // Binary and octal literals are effectively transpiled by the parser.
    // There's no transpilation we can do for the new regexp flags.
    passes.add(
        createFeatureRemovalPass(
            "markEs6FeaturesNotRequiringTranspilationAsRemoved",
            Feature.BINARY_LITERALS,
            Feature.OCTAL_LITERALS,
            Feature.REGEXP_FLAG_U,
            Feature.REGEXP_FLAG_Y));
    passes.add(es6NormalizeShorthandProperties);
    passes.add(es6ConvertSuper);
    passes.add(es6RenameVariablesInParamLists);
    passes.add(es6SplitVariableDeclarations);
    passes.add(es6RewriteDestructuring);
    passes.add(es6RewriteArrowFunction);
    passes.add(es6ExtractClasses);
    passes.add(es6RewriteClass);
    passes.add(es6InjectRuntimeLibraries);
    passes.add(es6RewriteRestAndSpread);
    if (!options.checksOnly) {
      // Don't run these passes in checksOnly mode since all the typechecking & checks passes
      // support the transpiled features.
      // TODO(b/73387406): Move each pass above here temporarily, then into
      // addEs6PostCheck Passes once the pass supports propagating type information
    }
  }

  /** Deprecated: use addEs6PostCheckPasses instead. */
{'fraction_non_alphanumeric': 0.07701324580940101, 'fraction_numerical': 0.013128589848786778, 'mean_word_length': 3.5407131452900478, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5933462', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2367, 'n_words': 607}