Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [PiranhaJava] Support fixing enum fields with trailing semicolon #128

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion java/piranha/src/main/java/com/uber/piranha/XPFlagCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import javax.lang.model.element.ElementKind;

Expand Down Expand Up @@ -808,8 +809,30 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
// trailing "," if present on the parent.
String enumAsStr = state.getSourceForNode(state.getPath().getParentPath().getLeaf());
String varAsStrWithComma = tree.getName().toString() + ",";
if (enumAsStr.contains(varAsStrWithComma)) {
String varAsStrWithSemiColon = tree.getName().toString() + ";";
if (Pattern.compile("\\b" + varAsStrWithComma).matcher(enumAsStr).find()) {
return buildDescription(tree).addFix(SuggestedFix.replace(tree, "", 0, 1)).build();
} else if (Pattern.compile("\\b" + varAsStrWithSemiColon).matcher(enumAsStr).find()) {
// Complicated calculation to remove backwards from this enum field, pushing the ';' back on
// the previous enum field, if any. Otherwise, delete the full statement.
final int varTreeStartIdx = enumAsStr.indexOf(varAsStrWithSemiColon);
int deleteStartIdx = varTreeStartIdx;
char c = enumAsStr.charAt(deleteStartIdx);
while (c != ',' && c != ';' && c != '{') {
deleteStartIdx -= 1;
c = enumAsStr.charAt(deleteStartIdx);
}
// negative offset up to and including the comma, bracket or semicolon
int offset = deleteStartIdx - varTreeStartIdx;
if (c == ';' || c == '{') {
// Don't delete the previous bracket or semicolon, but do delete the trailing semicolon
return buildDescription(tree)
.addFix(SuggestedFix.replace(tree, "", offset + 1, 1))
.build();
} else {
// Previous separator is a comma, erase it and push back the `;`
return buildDescription(tree).addFix(SuggestedFix.replace(tree, "", offset, 0)).build();
}
} else {
// Fallback for single/last enum variable detection
return buildDescription(tree).addFix(SuggestedFix.delete(tree)).build();
Expand Down
32 changes: 32 additions & 0 deletions java/piranha/src/test/java/com/uber/piranha/CorePiranhaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,36 @@ public void testIgnoresPrefixMatchFlag() throws IOException {
"class TestClassPartialMatch { }")
.doTest();
}

@Test
public void testEnumWithTrailingSemicolon() throws IOException {

ErrorProneFlags.Builder b = ErrorProneFlags.builder();
b.putFlag("Piranha:FlagName", "STALE_FLAG");
b.putFlag("Piranha:IsTreated", "false");
b.putFlag("Piranha:Config", "config/properties.json");

BugCheckerRefactoringTestHelper bcr =
BugCheckerRefactoringTestHelper.newInstance(new XPFlagCleaner(b.build()), getClass());

bcr = bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath());

bcr = PiranhaTestingHelpers.addHelperClasses(bcr);
bcr.addInputLines(
"TestExperimentName.java",
"package com.uber.piranha;",
"public enum TestExperimentName {",
" OTHER_FLAG,",
" STALE_FLAG;",
" public void foo() {}",
"}")
.addOutputLines(
"TestExperimentName.java",
"package com.uber.piranha;",
"public enum TestExperimentName {",
" OTHER_FLAG;",
" public void foo() {}",
"}")
.doTest();
}
}