-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
SuggestedFixRules
Refaster rule collection
- Loading branch information
1 parent
b273502
commit c627149
Showing
4 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...r-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/SuggestedFixRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...b/src/test/resources/tech/picnic/errorprone/refasterrules/SuggestedFixRulesTestInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../src/test/resources/tech/picnic/errorprone/refasterrules/SuggestedFixRulesTestOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |