Skip to content

Commit

Permalink
Introduce SuggestedFixRules Refaster rule collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Apr 1, 2023
1 parent b273502 commit c627149
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState

SuggestedFix serializationFix = SuggestedFixes.renameMethodInvocation(tree, "concatMap", state);
SuggestedFix concurrencyCapFix =
SuggestedFix.builder()
.postfixWith(
Iterables.getOnlyElement(tree.getArguments()), ", " + MAX_CONCURRENCY_ARG_NAME)
.build();
SuggestedFix.postfixWith(
Iterables.getOnlyElement(tree.getArguments()), ", " + MAX_CONCURRENCY_ARG_NAME);

Description.Builder description = buildDescription(tree);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package tech.picnic.errorprone.refasterrules;

import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.sun.source.tree.Tree;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

/** Refaster rules related to expressions dealing with {@link SuggestedFix}es. */
@OnlineDocumentation
final class SuggestedFixRules {
private SuggestedFixRules() {}

/** Prefer {@link SuggestedFix#replace(Tree, String)}} over more contrived alternatives. */
static final class SuggestedFixReplaceTree {
@BeforeTemplate
SuggestedFix before(Tree tree, String replaceWith) {
return SuggestedFix.builder().replace(tree, replaceWith).build();
}

@AfterTemplate
SuggestedFix after(Tree tree, String replaceWith) {
return SuggestedFix.replace(tree, replaceWith);
}
}

/** Prefer {@link SuggestedFix#replace(int, int, String)}} over more contrived alternatives. */
static final class SuggestedFixReplaceStartEnd {
@BeforeTemplate
SuggestedFix before(int start, int end, String replaceWith) {
return SuggestedFix.builder().replace(start, end, replaceWith).build();
}

@AfterTemplate
SuggestedFix after(int start, int end, String replaceWith) {
return SuggestedFix.replace(start, end, replaceWith);
}
}

/**
* Prefer {@link SuggestedFix#replace(Tree, String, int, int)}} over more contrived alternatives.
*/
static final class SuggestedFixReplaceTreeStartEnd {
@BeforeTemplate
SuggestedFix before(Tree tree, String replaceWith, int start, int end) {
return SuggestedFix.builder().replace(tree, replaceWith, start, end).build();
}

@AfterTemplate
SuggestedFix after(Tree tree, String replaceWith, int start, int end) {
return SuggestedFix.replace(tree, replaceWith, start, end);
}
}

/** Prefer {@link SuggestedFix#swap(Tree, Tree)} over more contrived alternatives. */
static final class SuggestedFixSwap {
@BeforeTemplate
SuggestedFix before(Tree tree1, Tree tree2) {
return SuggestedFix.builder().swap(tree1, tree2).build();
}

@AfterTemplate
SuggestedFix after(Tree tree1, Tree tree2) {
return SuggestedFix.swap(tree1, tree2);
}
}

/** Prefer {@link SuggestedFix#prefixWith(Tree, String)} over more contrived alternatives. */
static final class SuggestedFixPrefixWith {
@BeforeTemplate
SuggestedFix before(Tree tree, String prefix) {
return SuggestedFix.builder().prefixWith(tree, prefix).build();
}

@AfterTemplate
SuggestedFix after(Tree tree, String prefix) {
return SuggestedFix.prefixWith(tree, prefix);
}
}

/** Prefer {@link SuggestedFix#postfixWith(Tree, String)}} over more contrived alternatives. */
static final class SuggestedFixPostfixWith {
@BeforeTemplate
SuggestedFix before(Tree tree, String postfix) {
return SuggestedFix.builder().postfixWith(tree, postfix).build();
}

@AfterTemplate
SuggestedFix after(Tree tree, String postfix) {
return SuggestedFix.postfixWith(tree, postfix);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tech.picnic.errorprone.refasterrules;

import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.Tree;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class SuggestFixRulesTest implements RefasterRuleCollectionTestCase {
SuggestedFix testSuggestedFixReplaceTree() {
return SuggestedFix.builder().replace(null, "foo").build();
}

SuggestedFix testSuggestedFixReplaceStartEnd() {
return SuggestedFix.builder().replace(1, 2, "foo").build();
}

SuggestedFix testSuggestedFixReplaceTreeStartEnd() {
return SuggestedFix.builder().replace(null, "foo", 1, 2).build();
}

SuggestedFix testSuggestedFixSwap() {
return SuggestedFix.builder().swap((Tree) null, (ExpressionTree) null).build();
}

SuggestedFix testSuggestedFixPrefixWith() {
return SuggestedFix.builder().prefixWith(null, "foo").build();
}

SuggestedFix testSuggestedFixPostfixWith() {
return SuggestedFix.builder().postfixWith(null, "foo").build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tech.picnic.errorprone.refasterrules;

import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.Tree;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class SuggestFixRulesTest implements RefasterRuleCollectionTestCase {
SuggestedFix testSuggestedFixReplaceTree() {
return Suggestions.replace(null, "foo");
}

SuggestedFix testSuggestedFixReplaceStartEnd() {
return Suggestions.replace(1, 2, "foo");
}

SuggestedFix testSuggestedFixReplaceTreeStartEnd() {
return Suggestions.replace(null, "foo", 1, 2);
}

SuggestedFix testSuggestedFixSwap() {
return Suggestions.swap((Tree) null, (ExpressionTree) null);
}

SuggestedFix testSuggestedFixPrefixWith() {
return Suggestions.prefixWith(null, "foo");
}

SuggestedFix testSuggestedFixPostfixWith() {
return Suggestions.postfixWith(null, "foo");
}
}

0 comments on commit c627149

Please sign in to comment.