Skip to content

Commit 5f08631

Browse files
committed
Issue #41: Added RemoveViolationComments recipe for preprocessing
1 parent faeab1d commit 5f08631

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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;
19+
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.internal.ListUtils;
24+
import org.openrewrite.java.JavaIsoVisitor;
25+
import org.openrewrite.java.tree.Space;
26+
import org.openrewrite.java.tree.TextComment;
27+
28+
public class RemoveViolationComments extends Recipe {
29+
30+
@Override
31+
public String getDisplayName() {
32+
return "Remove violation comments";
33+
}
34+
35+
@Override
36+
public String getDescription() {
37+
return "Removes comments that match the pattern '//violation *'.";
38+
}
39+
40+
@Override
41+
public TreeVisitor<?, ExecutionContext> getVisitor() {
42+
return new ViolationCommentsVisitor();
43+
}
44+
45+
private static final class ViolationCommentsVisitor extends JavaIsoVisitor<ExecutionContext> {
46+
@Override
47+
public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
48+
final StringBuilder suffixAccumulator = new StringBuilder();
49+
50+
final Space modifiedSpace = space.withComments(
51+
ListUtils.map(space.getComments(), comment -> {
52+
return processComment(comment, suffixAccumulator);
53+
}));
54+
55+
Space result = modifiedSpace;
56+
57+
if (suffixAccumulator.isEmpty()) {
58+
result = modifiedSpace.withWhitespace(suffixAccumulator.toString());
59+
}
60+
return result;
61+
}
62+
63+
private TextComment processComment(org.openrewrite.java.tree.Comment comment,
64+
StringBuilder suffixAccumulator) {
65+
TextComment textComment = (TextComment) comment;
66+
if (!comment.isMultiline() && textComment.getText().startsWith("violation")) {
67+
suffixAccumulator.append(textComment.getSuffix());
68+
textComment = null;
69+
}
70+
return textComment;
71+
}
72+
73+
}
74+
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929

3030
import org.checkstyle.autofix.InputClassRenamer;
31+
import org.checkstyle.autofix.RemoveViolationComments;
3132
import org.checkstyle.autofix.parser.CheckstyleReportParser;
3233
import org.checkstyle.autofix.parser.CheckstyleViolation;
3334
import org.openrewrite.Recipe;
@@ -54,10 +55,6 @@ protected String getPackageLocation() {
5455
return "org/checkstyle/autofix/recipe/" + getSubpackage();
5556
}
5657

57-
private Recipe createPreprocessingRecipe() {
58-
return new InputClassRenamer();
59-
}
60-
6158
protected void verify(String testCaseName) throws Exception {
6259
final String inputFileName = "Input" + testCaseName + ".java";
6360
final String outputFileName = "Output" + testCaseName + ".java";
@@ -73,7 +70,8 @@ protected void verify(String testCaseName) throws Exception {
7370
final Recipe mainRecipe = createRecipe(violations);
7471

7572
testRecipe(beforeCode, expectedAfterCode,
76-
getPath(inputPath), createPreprocessingRecipe(), mainRecipe);
73+
getPath(inputPath), new InputClassRenamer(),
74+
new RemoveViolationComments(), mainRecipe);
7775
}
7876

7977
private List<CheckstyleViolation> runCheckstyle(String inputPath,

0 commit comments

Comments
 (0)