Search in sources :

Example 1 with SuggestedFix

use of com.google.javascript.refactoring.SuggestedFix in project closure-compiler by google.

the class GoogBindToArrow method processMatch.

@Override
public ImmutableList<SuggestedFix> processMatch(Match match) {
    AbstractCompiler compiler = match.getMetadata().getCompiler();
    Node googBindCall = match.getNode();
    Node function = googBindCall.getFirstChild().getNext();
    Node arrowFunction = function.cloneTree();
    arrowFunction.setIsArrowFunction(true);
    // For "function() { return x; }", transform to "() => x" instead of "() => { return x; }".
    Node body = function.getLastChild();
    if (body.hasOneChild()) {
        Node returnNode = body.getFirstChild();
        if (returnNode.isReturn()) {
            arrowFunction.replaceChild(arrowFunction.getLastChild(), returnNode.getFirstChild().detachFromParent());
        }
    }
    SuggestedFix.Builder fix = new SuggestedFix.Builder();
    fix.replace(googBindCall, arrowFunction, compiler);
    return ImmutableList.<SuggestedFix>of(fix.build());
}
Also used : AbstractCompiler(com.google.javascript.jscomp.AbstractCompiler) SuggestedFix(com.google.javascript.refactoring.SuggestedFix) Node(com.google.javascript.rhino.Node)

Example 2 with SuggestedFix

use of com.google.javascript.refactoring.SuggestedFix in project closure-compiler by google.

the class RefasterJsTestUtils method assertFileRefactoring.

/**
 * Performs refactoring using a RefasterJs template and asserts that result is as expected.
 *
 * @param refasterJsTemplate path of the file or resource containing the RefasterJs template to
 *     apply
 * @param testDataPathPrefix path prefix of the directory from which input and expected-output
 *     file will be read
 * @param originalFile file name of the JavaScript source file to apply the refaster template to
 * @param additionalSourceFiles list of additional source files to provide to the compiler (e.g.
 *     dependencies)
 * @param expectedFileChoices the expected result options of applying the specified template to
 *     {@code originalFile}
 * @throws IOException
 */
public static void assertFileRefactoring(String refasterJsTemplate, String testDataPathPrefix, String originalFile, List<String> additionalSourceFiles, String... expectedFileChoices) throws IOException {
    RefasterJsScanner scanner = new RefasterJsScanner();
    scanner.loadRefasterJsTemplate(refasterJsTemplate);
    final String originalFilePath = testDataPathPrefix + File.separator + originalFile;
    ImmutableList.Builder<String> expectedCodeBuilder = ImmutableList.builder();
    for (String expectedFile : expectedFileChoices) {
        expectedCodeBuilder.add(slurpFile(testDataPathPrefix + File.separator + expectedFile));
    }
    final ImmutableList<String> expectedCode = expectedCodeBuilder.build();
    RefactoringDriver.Builder driverBuilder = new RefactoringDriver.Builder().addExterns(CommandLineRunner.getBuiltinExterns(CompilerOptions.Environment.BROWSER));
    for (String additionalSource : additionalSourceFiles) {
        driverBuilder.addInputsFromFile(testDataPathPrefix + File.separator + additionalSource);
    }
    RefactoringDriver driver = driverBuilder.addInputsFromFile(originalFilePath).build();
    List<SuggestedFix> fixes = driver.drive(scanner);
    assertThat(driver.getCompiler().getErrors()).isEmpty();
    assertThat(driver.getCompiler().getWarnings()).isEmpty();
    ImmutableList<String> newCode = ApplySuggestedFixes.applyAllSuggestedFixChoicesToCode(fixes, ImmutableMap.of(originalFilePath, slurpFile(originalFilePath))).stream().map(m -> m.get(originalFilePath)).collect(ImmutableList.toImmutableList());
    assertThat(newCode).comparingElementsUsing(new IgnoringWhitespaceCorrespondence()).containsExactlyElementsIn(expectedCode);
}
Also used : ApplySuggestedFixes(com.google.javascript.refactoring.ApplySuggestedFixes) RefasterJsScanner(com.google.javascript.refactoring.RefasterJsScanner) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) Truth.assertThat(com.google.common.truth.Truth.assertThat) SuggestedFix(com.google.javascript.refactoring.SuggestedFix) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Correspondence(com.google.common.truth.Correspondence) List(java.util.List) CommandLineRunner(com.google.javascript.jscomp.CommandLineRunner) ImmutableList(com.google.common.collect.ImmutableList) Files(com.google.common.io.Files) RefactoringDriver(com.google.javascript.refactoring.RefactoringDriver) RefactoringDriver(com.google.javascript.refactoring.RefactoringDriver) SuggestedFix(com.google.javascript.refactoring.SuggestedFix) ImmutableList(com.google.common.collect.ImmutableList) RefasterJsScanner(com.google.javascript.refactoring.RefasterJsScanner)

Example 3 with SuggestedFix

use of com.google.javascript.refactoring.SuggestedFix in project closure-compiler by google.

the class Linter method fix.

/**
 * @return Whether any fixes were applied.
 */
private static boolean fix(String filename, ImmutableSet<DiagnosticType> unfixableErrors) throws IOException {
    Compiler compiler = new Compiler(System.out);
    FixingErrorManager errorManager = new FixingErrorManager(unfixableErrors);
    compiler.setErrorManager(errorManager);
    errorManager.setCompiler(compiler);
    lint(Paths.get(filename), compiler);
    Collection<SuggestedFix> fixes = errorManager.getAllFixes();
    if (!fixes.isEmpty()) {
        ApplySuggestedFixes.applySuggestedFixesToFiles(fixes);
        return true;
    }
    return false;
}
Also used : SuggestedFix(com.google.javascript.refactoring.SuggestedFix) FixingErrorManager(com.google.javascript.refactoring.FixingErrorManager)

Aggregations

SuggestedFix (com.google.javascript.refactoring.SuggestedFix)3 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Files (com.google.common.io.Files)1 Correspondence (com.google.common.truth.Correspondence)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)1 CommandLineRunner (com.google.javascript.jscomp.CommandLineRunner)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 ApplySuggestedFixes (com.google.javascript.refactoring.ApplySuggestedFixes)1 FixingErrorManager (com.google.javascript.refactoring.FixingErrorManager)1 RefactoringDriver (com.google.javascript.refactoring.RefactoringDriver)1 RefasterJsScanner (com.google.javascript.refactoring.RefasterJsScanner)1 Node (com.google.javascript.rhino.Node)1 File (java.io.File)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 List (java.util.List)1