Skip to content

Commit 9812d1c

Browse files
Anmol202005rdiachenko
authored andcommitted
Issue #41: Added RemoveViolationComments recipe for preprocessing
1 parent a2d69a9 commit 9812d1c

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 java.util.List;
21+
import java.util.Objects;
22+
import java.util.stream.Collectors;
23+
24+
import org.openrewrite.ExecutionContext;
25+
import org.openrewrite.Recipe;
26+
import org.openrewrite.TreeVisitor;
27+
import org.openrewrite.java.JavaIsoVisitor;
28+
import org.openrewrite.java.tree.Comment;
29+
import org.openrewrite.java.tree.Space;
30+
import org.openrewrite.java.tree.TextComment;
31+
32+
public class RemoveViolationComments extends Recipe {
33+
34+
@Override
35+
public String getDisplayName() {
36+
return "Remove violation comments";
37+
}
38+
39+
@Override
40+
public String getDescription() {
41+
return "Removes comments that match the pattern '//violation *'.";
42+
}
43+
44+
@Override
45+
public TreeVisitor<?, ExecutionContext> getVisitor() {
46+
return new ViolationCommentRemover();
47+
}
48+
49+
private static final class ViolationCommentRemover extends JavaIsoVisitor<ExecutionContext> {
50+
@Override
51+
public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
52+
final StringBuilder suffixAccumulator = new StringBuilder();
53+
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+
}
63+
}
64+
return result; })
65+
.filter(Objects::nonNull)
66+
.collect(Collectors.toList());
67+
68+
Space result;
69+
70+
if (filteredComments.size() == space.getComments().size()) {
71+
result = space;
72+
}
73+
else {
74+
result = space.withComments(filteredComments);
75+
if (!suffixAccumulator.isEmpty()) {
76+
result = result.withWhitespace(suffixAccumulator.toString());
77+
}
78+
}
79+
return super.visitSpace(result, loc, ctx);
80+
}
81+
}
82+
}

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;
@@ -52,10 +53,6 @@ protected String getPackageLocation() {
5253
return "org/checkstyle/autofix/recipe/" + getSubpackage();
5354
}
5455

55-
private Recipe createPreprocessingRecipe() {
56-
return new InputClassRenamer();
57-
}
58-
5956
protected void verify(String testCaseName) throws Exception {
6057
final String inputFileName = "Input" + testCaseName + ".java";
6158
final String outputFileName = "Output" + testCaseName + ".java";
@@ -71,7 +68,8 @@ protected void verify(String testCaseName) throws Exception {
7168
final Recipe mainRecipe = createRecipe(violations);
7269

7370
testRecipe(beforeCode, expectedAfterCode,
74-
getPath(inputPath), createPreprocessingRecipe(), mainRecipe);
71+
getPath(inputPath), new InputClassRenamer(),
72+
new RemoveViolationComments(), mainRecipe);
7573
}
7674

7775
private List<CheckstyleViolation> runCheckstyle(String inputPath,

0 commit comments

Comments
 (0)