Skip to content

Commit 06454b5

Browse files
committed
Issue #69: resolved removeViolationComments Bug
1 parent 9279400 commit 06454b5

File tree

4 files changed

+137
-26
lines changed

4 files changed

+137
-26
lines changed

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717

1818
package org.checkstyle.autofix;
1919

20+
import java.util.ArrayList;
2021
import java.util.List;
21-
import java.util.Objects;
22-
import java.util.stream.Collectors;
2322

2423
import org.openrewrite.ExecutionContext;
2524
import org.openrewrite.Recipe;
@@ -49,21 +48,32 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4948
private static final class ViolationCommentRemover extends JavaIsoVisitor<ExecutionContext> {
5049
@Override
5150
public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
52-
final StringBuilder suffixAccumulator = new StringBuilder();
51+
String suffixAccumulator = null;
52+
final List<Comment> filteredComments = new ArrayList<>();
5353

54-
final List<Comment> filteredComments = space.getComments().stream()
55-
.map(comment -> {
56-
Comment result = comment;
57-
if (!comment.isMultiline() && comment instanceof TextComment) {
58-
final TextComment textComment = (TextComment) comment;
59-
if (textComment.getText().startsWith("violation")) {
60-
suffixAccumulator.append(textComment.getSuffix());
61-
result = null;
62-
}
54+
for (Comment comment : space.getComments()) {
55+
if (!comment.isMultiline() && comment instanceof TextComment) {
56+
final TextComment textComment = (TextComment) comment;
57+
if (textComment.getText().matches("\\s*(\\d+\\s*)?violation.*")) {
58+
if (filteredComments.isEmpty()) {
59+
suffixAccumulator = textComment.getSuffix();
6360
}
64-
return result; })
65-
.filter(Objects::nonNull)
66-
.collect(Collectors.toList());
61+
else {
62+
final int lastIndex = filteredComments.size() - 1;
63+
final Comment previousComment = filteredComments.get(lastIndex);
64+
filteredComments.set(lastIndex,
65+
previousComment.withSuffix(textComment.getSuffix()));
66+
}
67+
}
68+
else {
69+
filteredComments.add(comment);
70+
}
71+
72+
}
73+
else {
74+
filteredComments.add(comment);
75+
}
76+
}
6777

6878
Space result;
6979

@@ -72,17 +82,8 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
7282
}
7383
else {
7484
result = space.withComments(filteredComments);
75-
if (!suffixAccumulator.isEmpty()) {
76-
if (filteredComments.isEmpty()) {
77-
result = result.withWhitespace(suffixAccumulator.toString());
78-
}
79-
else {
80-
final Comment lastComment = filteredComments
81-
.get(filteredComments.size() - 1);
82-
filteredComments.set(filteredComments.size() - 1,
83-
lastComment.withSuffix(suffixAccumulator.toString()));
84-
result = space.withComments(filteredComments);
85-
}
85+
if (suffixAccumulator != null) {
86+
result = result.withWhitespace(suffixAccumulator);
8687
}
8788
}
8889
return super.visitSpace(result, loc, ctx);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite.
3+
// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
///////////////////////////////////////////////////////////////////////////////////////////////
17+
18+
package org.checkstyle.autofix.recipe;
19+
20+
import static org.openrewrite.java.Assertions.java;
21+
22+
import java.io.IOException;
23+
24+
import org.checkstyle.autofix.InputClassRenamer;
25+
import org.checkstyle.autofix.RemoveViolationComments;
26+
import org.junit.jupiter.api.Test;
27+
import org.openrewrite.test.RewriteTest;
28+
29+
import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
30+
31+
public class RemoveViolationCommentsTest extends AbstractPathTestSupport implements RewriteTest {
32+
33+
@Override
34+
protected String getPackageLocation() {
35+
return "org/checkstyle/autofix/recipe/removeviolationcomments/allcommentvariations";
36+
}
37+
38+
@Test
39+
void removeAllViolationCommentPatterns() throws IOException {
40+
final String beforeCode = readFile(getPath("InputAllCommentVariations.java"));
41+
final String afterCode = readFile(getPath("OutputAllCommentVariations.java"));
42+
43+
rewriteRun(
44+
spec -> {
45+
spec.recipes(new InputClassRenamer(), new RemoveViolationComments());
46+
}, java(beforeCode, afterCode)
47+
);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.checkstyle.autofix.recipe.removeviolationcomments.allcommentvariations;
2+
3+
public class InputAllCommentVariations {
4+
5+
private int field1; // violation
6+
private int field2; // 2 violation message
7+
private int field3; // violation
8+
private int field4; // 3 violation text
9+
private int field5; // violation - missing javadoc
10+
private int field6; // 10 violation some descriptive text
11+
12+
/* This violation should not be removed - multiline */
13+
private int field7; // this is not a violation
14+
private int field8; // some regular comment
15+
16+
// Another regular comment
17+
private int field9; // violation
18+
// violation below
19+
private int field10; // random comment
20+
private int field11; // 25 violation
21+
private int field12;
22+
// violation above
23+
24+
/**
25+
* Javadoc with violation word should stay
26+
*/
27+
public void method() {
28+
int local; // violation
29+
// violation in standalone comment
30+
int other; // normal comment
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.checkstyle.autofix.recipe.removeviolationcomments.allcommentvariations;
2+
3+
public class OutputAllCommentVariations {
4+
5+
private int field1;
6+
private int field2;
7+
private int field3;
8+
private int field4;
9+
private int field5;
10+
private int field6;
11+
12+
/* This violation should not be removed - multiline */
13+
private int field7; // this is not a violation
14+
private int field8; // some regular comment
15+
16+
// Another regular comment
17+
private int field9;
18+
private int field10; // random comment
19+
private int field11;
20+
private int field12;
21+
22+
/**
23+
* Javadoc with violation word should stay
24+
*/
25+
public void method() {
26+
int local;
27+
int other; // normal comment
28+
}
29+
}

0 commit comments

Comments
 (0)