/*
 * Copyright 2015 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 com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Minimal binary that just runs the "lint" checks which can be run on a single file at a time.
 * This means some checks in the lintChecks DiagnosticGroup are skipped, since they depend on
 * type information.
 */
public class Linter {
  public static void main(String[] args) throws IOException {
    for (String filename : args) {
      lint(null, filename);
    }
  }

  protected static void lint(SourceFile externs, String filename) throws IOException {
    lint(externs, Paths.get(filename));
  }

  private static void lint(SourceFile externs, Path path) throws IOException {
    if (externs == null) {
      externs = SourceFile.fromCode("<Linter externs>", "");
    }
    SourceFile file = SourceFile.fromFile(path.toString());
    Compiler compiler = new Compiler();
    CompilerOptions options = new CompilerOptions();
    options.setLanguage(LanguageMode.ECMASCRIPT6_STRICT);

    // For a full compile, this would cause a crash, as the method name implies. But the passes
    // in LintPassConfig can all handle untranspiled ES6.
    options.setSkipTranspilationAndCrash(true);

    options.setCodingConvention(new GoogleCodingConvention());
    options.setWarningLevel(DiagnosticGroups.MISSING_REQUIRE, CheckLevel.WARNING);
    options.setWarningLevel(DiagnosticGroups.EXTRA_REQUIRE, CheckLevel.WARNING);
    compiler.setPassConfig(new LintPassConfig(options));
    compiler.disableThreads();
    compiler.compile(ImmutableList.<SourceFile>of(externs), ImmutableList.of(file), options);
  }
}

@@ -758,16 +758,192 @@
 ageMode;
+%0Aimport com.google.javascript.refactoring.ApplySuggestedFixes;%0Aimport com.google.javascript.refactoring.ErrorToFixMapper;%0Aimport com.google.javascript.refactoring.SuggestedFix;
 %0A%0Aimport
@@ -1018,16 +1018,67 @@
 e.Paths;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1405,24 +1405,135 @@
 e : args) %7B%0A
+      // TODO(tbreisacher): Add a command line flag that causes this%0A      // to call fix() instead of lint().%0A
       lint(n
@@ -1563,19 +1563,139 @@
  %7D%0A%0A
+%0A
   
-protected
+static void lint(SourceFile externs, String filename) throws IOException %7B%0A    lint(externs, Paths.get(filename), false);%0A  %7D%0A%0A 
  sta
@@ -1695,36 +1695,35 @@
 %0A%0A  static void 
-lint
+fix
 (SourceFile exte
@@ -1802,16 +1802,22 @@
 ilename)
+, true
 );%0A  %7D%0A%0A
@@ -1872,16 +1872,29 @@
 ath path
+, boolean fix
 ) throws
@@ -2413,24 +2413,53 @@
 ash(true);%0A%0A
+    options.setIdeMode(fix);%0A
     options.
@@ -2855,14 +2855,382 @@
 tions);%0A
+    if (fix) %7B%0A      List%3CSuggestedFix%3E fixes = new ArrayList%3C%3E();%0A      for (JSError warning : compiler.getWarnings()) %7B%0A        SuggestedFix suggestedFix = ErrorToFixMapper.getFixForJsError(warning, compiler);%0A        if (suggestedFix != null) %7B%0A          fixes.add(suggestedFix);%0A        %7D%0A      %7D%0A      ApplySuggestedFixes.applySuggestedFixesToFiles(fixes);%0A    %7D%0A
   %7D%0A%7D%0A

Add a way to auto-apply fixes. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=110494692