Skip to content
Closed
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
Expand Down
13 changes: 11 additions & 2 deletions src/test/java/org/checkstyle/autofix/RemoveViolationComments.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,19 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
else {
result = space.withComments(filteredComments);
if (!suffixAccumulator.isEmpty()) {
result = result.withWhitespace(suffixAccumulator.toString());
if (filteredComments.isEmpty()) {
// All comments were violation comments - add to whitespace
result = result.withWhitespace(suffixAccumulator.toString());
} else {
// Add suffix to last remaining comment
Comment lastComment = filteredComments.get(filteredComments.size() - 1);
filteredComments.set(filteredComments.size() - 1,
lastComment.withSuffix(lastComment.getSuffix() + suffixAccumulator.toString()));
result = space.withComments(filteredComments);
}
}
}
return super.visitSpace(result, loc, ctx);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.checkstyle.autofix;

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class RemoveViolationCommentsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RemoveViolationComments());
}

@Test
void removesViolationComments() {
rewriteRun(
java(
"""
package org.checkstyle.autofix;
public class Test {
int a; //hloo
//violation
int b;
int c; //violation
}
""",
"""
package org.checkstyle.autofix;
public class Test {
int a; //hloo

int b;
int c;
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ protected void verify(String testCaseName) throws Exception {
final Configuration config = getCheckConfigurations(inputPath);
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);

String[] expectedMessages = convertToExpectedMessages(violations);
verifyWithInlineConfigParser(getPath(inputPath), expectedMessages);

final String beforeCode = readFile(getPath(inputPath));
final String expectedAfterCode = readFile(getPath(outputPath));

final Recipe mainRecipe = createRecipe(violations);

testRecipe(beforeCode, expectedAfterCode,
getPath(inputPath), new InputClassRenamer(),
new RemoveViolationComments(), mainRecipe);
getPath(inputPath), new InputClassRenamer(), mainRecipe, new RemoveViolationComments());
}

private List<CheckstyleViolation> runCheckstyle(String inputPath,
Expand Down Expand Up @@ -117,6 +119,18 @@ private void verifyOutputFile(String outputPath) throws Exception {
}
}

private String[] convertToExpectedMessages(List<CheckstyleViolation> violations) {
return violations.stream()
.map(v -> {
if (v.getColumn() > 0) {
return v.getLine() + ":" + v.getColumn() + ": " + v.getMessage();
} else {
return v.getLine() + ": " + v.getMessage();
}
})
.toArray(String[]::new);
}

private Configuration getCheckConfigurations(String inputPath) throws Exception {
final String configFilePath = getPath(inputPath);
final TestInputConfiguration testInputConfiguration =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
package org.checkstyle.autofix.recipe.upperell.hexoctalliteral;

public class InputHexOctalLiteral {
private long hexLower = 0x1ABCl;
private long hexUpper = 0X2DEFl;
private long octal = 0777l;
private long binary = 0b1010l;
private long decimal = 12345l;
private long hexLower = 0x1ABCl; //violation
private long hexUpper = 0X2DEFl; //violation
private long octal = 0777l; //violation
private long binary = 0b1010l; //violation
private long decimal = 12345l; //violation

public void calculateValues() {
//violation below
long hexResult = 0xDEADBEEFl + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl
//violation below
long octalResult = 01234l + 0xDEADBEEFl; //suppressed violation for 01234l
long binaryResult = 0b11110000l;
long binaryResult = 0b11110000l; //violation
}
}
Loading