Skip to content

Commit 54edd71

Browse files
committed
Issue #41: add Violation Comment validation
1 parent 8fe1753 commit 54edd71

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

src/test/java/org/checkstyle/autofix/RemoveViolationComments.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,19 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
7373
else {
7474
result = space.withComments(filteredComments);
7575
if (!suffixAccumulator.isEmpty()) {
76-
result = result.withWhitespace(suffixAccumulator.toString());
76+
if (filteredComments.isEmpty()) {
77+
// All comments were violation comments - add to whitespace
78+
result = result.withWhitespace(suffixAccumulator.toString());
79+
} else {
80+
// Add suffix to last remaining comment
81+
Comment lastComment = filteredComments.get(filteredComments.size() - 1);
82+
filteredComments.set(filteredComments.size() - 1,
83+
lastComment.withSuffix(lastComment.getSuffix() + suffixAccumulator.toString()));
84+
result = space.withComments(filteredComments);
85+
}
7786
}
7887
}
7988
return super.visitSpace(result, loc, ctx);
8089
}
8190
}
82-
}
91+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.checkstyle.autofix;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openrewrite.test.RecipeSpec;
5+
import org.openrewrite.test.RewriteTest;
6+
7+
import static org.openrewrite.java.Assertions.java;
8+
9+
class RemoveViolationCommentsTest implements RewriteTest {
10+
11+
@Override
12+
public void defaults(RecipeSpec spec) {
13+
spec.recipe(new RemoveViolationComments());
14+
}
15+
16+
@Test
17+
void removesViolationComments() {
18+
rewriteRun(
19+
java(
20+
"""
21+
package org.checkstyle.autofix;
22+
public class Test {
23+
int a; //hloo
24+
//violation
25+
int b;
26+
int c; //violation
27+
}
28+
""",
29+
"""
30+
package org.checkstyle.autofix;
31+
public class Test {
32+
int a; //hloo
33+
34+
int b;
35+
int c;
36+
}
37+
"""
38+
)
39+
);
40+
}
41+
}

src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ protected void verify(String testCaseName) throws Exception {
7373
final Recipe mainRecipe = createRecipe(violations);
7474

7575
testRecipe(beforeCode, expectedAfterCode,
76-
getPath(inputPath), new InputClassRenamer(),
77-
new RemoveViolationComments(), mainRecipe);
76+
getPath(inputPath), new InputClassRenamer(), mainRecipe, new RemoveViolationComments());
7877
}
7978

8079
private List<CheckstyleViolation> runCheckstyle(String inputPath,

0 commit comments

Comments
 (0)