private int run()

in java/com/google/javascript/jscomp/JsCompiler.java [88:221]


  private int run(Iterable<String> args) throws IOException {
    // Our flags, which we won't pass along to the compiler.
    List<ClosureJsLibrary> infos = new ArrayList<>();
    List<String> roots = new ArrayList<>();
    Set<DiagnosticType> globalSuppressions = new HashSet<>();
    Path outputErrors = null;
    boolean expectFailure = false;
    boolean expectWarnings = false;
    boolean exportTestFunctions = false;
    boolean checksOnly = false;

    // Compiler flags we want to read.
    Path jsOutputFile = null;
    Path createSourceMap = null;

    // Parse flags in an ad-hoc manner.
    List<String> passThroughArgs = new ArrayList<>(1024);
    PeekingIterator<String> iargs = Iterators.peekingIterator(args.iterator());
    while (iargs.hasNext()) {
      String arg = iargs.next();
      switch (arg) {
        case "--info":
          infos.add(JsCheckerHelper.loadClosureJsLibraryInfo(Paths.get(iargs.next())));
          continue;
        case "--output_errors":
          outputErrors = Paths.get(iargs.next());
          continue;
        case "--suppress":
          globalSuppressions.addAll(Diagnostics.getDiagnosticTypesForSuppressCode(iargs.next()));
          continue;
        case "--expect_failure":
          expectFailure = true;
          continue;
        case "--expect_warnings":
          expectWarnings = true;
          continue;
        case "--export_test_functions":
          // TODO(jart): Remove this when it's added to open source Closure Compiler.
          exportTestFunctions = true;
          continue;
        case "--js_module_root":
          roots.add(iargs.peek());
          break;
        case "--js_output_file":
          jsOutputFile = Paths.get(iargs.peek());
          break;
        case "--checks_only":
          checksOnly = true;
          break;
        case "--create_source_map":
          createSourceMap = Paths.get(iargs.peek());
          break;
        default:
          break;
      }
      passThroughArgs.add(arg);
    }

    // Keep track of modules defined *only* in rules that don't have the suppress attribute,
    // e.g. js_library(). We'll make the compiler much more lax for these sources.
    Set<String> legacyModules = new HashSet<>();
    for (ClosureJsLibrary info : infos) {
      if (info.getLegacy()) {
        legacyModules.addAll(info.getModuleList());
      }
    }

    // We want to be able to turn module names back into labels.
    Map<String, String> labels = new HashMap<>();
    // We also need the set of all (module, suppress) combinations propagated from library rules.
    Multimap<String, DiagnosticType> suppressions = HashMultimap.create();
    for (ClosureJsLibrary info : infos) {
      if (info.getLegacy()) {
        continue;
      }
      legacyModules.removeAll(info.getModuleList());
      for (String module : info.getModuleList()) {
        labels.put(module, info.getLabel());
        for (String suppress : info.getSuppressList()) {
          suppressions.put(module,
              verifyNotNull(Diagnostics.DIAGNOSTIC_TYPES.get(suppress),
                  "Bad DiagnosticType from closure_js_library: %s", suppress));
        }
      }
    }

    // Run the compiler, capturing error messages.
    boolean failed = false;
    Compiler compiler = new Compiler();
    JsCheckerErrorFormatter errorFormatter = new JsCheckerErrorFormatter(compiler, roots, labels);
    errorFormatter.setColorize(true);
    JsCheckerErrorManager errorManager = new JsCheckerErrorManager(errorFormatter);
    compiler.setErrorManager(errorManager);
    JsCompilerWarnings warnings =
        new JsCompilerWarnings(roots, legacyModules, suppressions, globalSuppressions);
    JsCompilerRunner runner =
        new JsCompilerRunner(
            passThroughArgs,
            compiler,
            exportTestFunctions,
            warnings);
    if (runner.shouldRunCompiler()) {
      failed |= runner.go() != 0;
    }
    failed |= runner.hasErrors();

    // Output error messages based on diagnostic settings.
    if (!expectFailure && !expectWarnings) {
      for (String line : errorManager.stderr) {
        System.err.println(line);
      }
      System.err.flush();
    }
    if (outputErrors != null) {
      Files.write(outputErrors, errorManager.stderr, UTF_8);
    }
    if ((failed && expectFailure) || checksOnly) {
      // If we don't return nonzero, Bazel expects us to create every output file.
      if (jsOutputFile != null) {
        Files.write(jsOutputFile, EMPTY_BYTE_ARRAY);
      }
    }

    // Make sure a source map is always created since Bazel expect that but JsCompiler
    // may not emit sometimes (e.g compiler_level=BUNLDE)
    if (createSourceMap != null && !Files.exists(createSourceMap)) {
      Files.write(createSourceMap, EMPTY_BYTE_ARRAY);
    }

    if (!failed && expectFailure) {
      System.err.println("ERROR: Expected failure but didn't fail.");
    }
    return failed == expectFailure ? 0 : 1;
  }