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());
}
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);
}
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;
}
Aggregations