Skip to content

Commit ca5d41b

Browse files
carlosu7nahsra
andauthored
Sonar codemod for removing block of commented-out lines of code (#269)
Reference: https://rules.sonarsource.com/java/RSPEC-125/ Docs PR pixee/docs#120 --------- Co-authored-by: Arshan Dabirsiaghi <[email protected]>
1 parent f6dcfea commit ca5d41b

File tree

9 files changed

+623
-4
lines changed

9 files changed

+623
-4
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.codemodder.codemods;
2+
3+
import com.github.javaparser.ast.CompilationUnit;
4+
import com.github.javaparser.ast.comments.Comment;
5+
import io.codemodder.*;
6+
import io.codemodder.providers.sonar.ProvidedSonarScan;
7+
import io.codemodder.providers.sonar.RuleIssues;
8+
import io.codemodder.providers.sonar.SonarPluginJavaParserChanger;
9+
import io.codemodder.providers.sonar.api.Issue;
10+
import javax.inject.Inject;
11+
12+
/** A codemod for removing commented-out lines of code. */
13+
@Codemod(
14+
id = "sonar:java/remove-commented-code-s125",
15+
reviewGuidance = ReviewGuidance.MERGE_WITHOUT_REVIEW,
16+
executionPriority = CodemodExecutionPriority.HIGH)
17+
public final class RemoveCommentedCodeCodemod extends SonarPluginJavaParserChanger<Comment> {
18+
19+
/**
20+
* The reason behind this specific region node matcher is that in the reported column by
21+
* sonarcloud (range.begin.colum) differs by minus one point that the column reported by the java
22+
* parser (region.start().column())
23+
*/
24+
private static final RegionNodeMatcher regionNodeMatcher =
25+
(region, range) ->
26+
region.start().line() == range.begin.line
27+
&& region.start().column() >= range.begin.column;
28+
29+
@Inject
30+
public RemoveCommentedCodeCodemod(
31+
@ProvidedSonarScan(ruleId = "java:S125") final RuleIssues issues) {
32+
33+
super(issues, Comment.class, regionNodeMatcher, NodeCollector.ALL_COMMENTS);
34+
}
35+
36+
@Override
37+
public boolean onIssueFound(
38+
final CodemodInvocationContext context,
39+
final CompilationUnit cu,
40+
final Comment comment,
41+
final Issue issue) {
42+
43+
comment.removeForced();
44+
45+
return true;
46+
}
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This change eliminates commented-out code that may impede readability and distract focus. Any deleted code can still be accessed through the source control history if needed.
2+
3+
Our changes look something like this:
4+
5+
```diff
6+
catch (IOException e) {
7+
- // LOG.error("Unexpected problem ", ex);
8+
return handleError(ex);
9+
}
10+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"summary" : "Removed block of commented-out lines of code (Sonar)",
3+
"change" : "Removed block of commented-out lines of code",
4+
"references" : [
5+
"https://rules.sonarsource.com/java/RSPEC-125/"
6+
]
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.codemodder.codemods;
2+
3+
import io.codemodder.testutils.CodemodTestMixin;
4+
import io.codemodder.testutils.Metadata;
5+
6+
@Metadata(
7+
codemodType = RemoveCommentedCodeCodemod.class,
8+
testResourceDir = "remove-commented-code-s125",
9+
renameTestFile = "src/main/java/org/owasp/webgoat/container/assignments/AttackResult.java",
10+
dependencies = {})
11+
final class RemoveCommentedCodeCodemodTest implements CodemodTestMixin {}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* This file is part of WebGoat, an Open Web Application Security Project utility. For details,
3+
* please see http://www.owasp.org/
4+
* <p>
5+
* Copyright (c) 2002 - 2017 Bruce Mayhew
6+
* <p>
7+
* This program is free software; you can redistribute it and/or modify it under the terms of the
8+
* GNU General Public License as published by the Free Software Foundation; either version 2 of the
9+
* License, or (at your option) any later version.
10+
* <p>
11+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
12+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* General Public License for more details.
14+
* <p>
15+
* You should have received a copy of the GNU General Public License along with this program; if
16+
* not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17+
* 02111-1307, USA.
18+
* <p>
19+
* Getting Source ==============
20+
* <p>
21+
* Source for this application is maintained at https://github.com/WebGoat/WebGoat, a repository for free software
22+
* projects.
23+
* <p>
24+
*/
25+
26+
package org.owasp.webgoat.container.assignments;
27+
28+
import static org.apache.commons.text.StringEscapeUtils.escapeJson;
29+
30+
import lombok.Getter;
31+
import org.owasp.webgoat.container.i18n.PluginMessages;
32+
33+
public class AttackResult {
34+
35+
public static class AttackResultBuilder {
36+
37+
private boolean lessonCompleted;
38+
private PluginMessages messages;
39+
private Object[] feedbackArgs;
40+
private String feedbackResourceBundleKey;
41+
private String output;
42+
private Object[] outputArgs;
43+
private AssignmentEndpoint assignment;
44+
private boolean attemptWasMade = false;
45+
46+
public AttackResultBuilder(PluginMessages messages) {
47+
this.messages = messages;
48+
}
49+
50+
public AttackResultBuilder lessonCompleted(boolean lessonCompleted) {
51+
this.lessonCompleted = lessonCompleted;
52+
this.feedbackResourceBundleKey = "lesson.completed";
53+
// return 0;
54+
return this;
55+
}
56+
57+
public AttackResultBuilder lessonCompleted(boolean lessonCompleted, String resourceBundleKey) {
58+
return this;
59+
}
60+
61+
public AttackResultBuilder feedbackArgs(Object... args) {
62+
this.feedbackArgs = args;
63+
return this;
64+
}
65+
66+
public AttackResultBuilder feedback(String resourceBundleKey) {
67+
this.feedbackResourceBundleKey = resourceBundleKey;
68+
return this;
69+
}
70+
71+
72+
public AttackResultBuilder output(String output) {
73+
this.output = output;
74+
return this;
75+
}
76+
77+
public AttackResultBuilder outputArgs(Object... args) {
78+
this.outputArgs = args;
79+
return this;
80+
}
81+
82+
public AttackResultBuilder attemptWasMade() {
83+
this.attemptWasMade = true;
84+
return this;
85+
}
86+
87+
public AttackResult build() {
88+
return new AttackResult(
89+
lessonCompleted,
90+
messages.getMessage(feedbackResourceBundleKey, feedbackArgs),
91+
messages.getMessage(output, output, outputArgs),
92+
assignment.getClass().getSimpleName(),
93+
attemptWasMade);
94+
}
95+
96+
public AttackResultBuilder assignment(AssignmentEndpoint assignment) {
97+
this.assignment = assignment;
98+
return this;
99+
}
100+
}
101+
102+
@Getter private boolean lessonCompleted;
103+
@Getter private String feedback;
104+
@Getter private String output;
105+
@Getter private final String assignment;
106+
@Getter private boolean attemptWasMade;
107+
108+
public AttackResult(
109+
boolean lessonCompleted,
110+
String feedback,
111+
String output,
112+
String assignment,
113+
boolean attemptWasMade) {
114+
this.lessonCompleted = lessonCompleted;
115+
this.feedback = escapeJson(feedback);
116+
this.output = escapeJson(output);
117+
this.assignment = assignment;
118+
this.attemptWasMade = attemptWasMade;
119+
}
120+
121+
public static AttackResultBuilder builder(PluginMessages messages) {
122+
return new AttackResultBuilder(messages);
123+
}
124+
125+
public boolean assignmentSolved() {
126+
return lessonCompleted;
127+
}
128+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* This file is part of WebGoat, an Open Web Application Security Project utility. For details,
3+
* please see http://www.owasp.org/
4+
* <p>
5+
* Copyright (c) 2002 - 2017 Bruce Mayhew
6+
* <p>
7+
* This program is free software; you can redistribute it and/or modify it under the terms of the
8+
* GNU General Public License as published by the Free Software Foundation; either version 2 of the
9+
* License, or (at your option) any later version.
10+
* <p>
11+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
12+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* General Public License for more details.
14+
* <p>
15+
* You should have received a copy of the GNU General Public License along with this program; if
16+
* not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17+
* 02111-1307, USA.
18+
* <p>
19+
* Getting Source ==============
20+
* <p>
21+
* Source for this application is maintained at https://github.com/WebGoat/WebGoat, a repository for free software
22+
* projects.
23+
* <p>
24+
*/
25+
26+
package org.owasp.webgoat.container.assignments;
27+
28+
import static org.apache.commons.text.StringEscapeUtils.escapeJson;
29+
30+
import lombok.Getter;
31+
import org.owasp.webgoat.container.i18n.PluginMessages;
32+
33+
public class AttackResult {
34+
35+
public static class AttackResultBuilder {
36+
37+
private boolean lessonCompleted;
38+
private PluginMessages messages;
39+
private Object[] feedbackArgs;
40+
private String feedbackResourceBundleKey;
41+
private String output;
42+
private Object[] outputArgs;
43+
private AssignmentEndpoint assignment;
44+
private boolean attemptWasMade = false;
45+
46+
public AttackResultBuilder(PluginMessages messages) {
47+
this.messages = messages;
48+
}
49+
50+
public AttackResultBuilder lessonCompleted(boolean lessonCompleted) {
51+
this.lessonCompleted = lessonCompleted;
52+
this.feedbackResourceBundleKey = "lesson.completed";
53+
//System.out.println("hola");
54+
// return 0;
55+
return this;
56+
}
57+
58+
public AttackResultBuilder lessonCompleted(boolean lessonCompleted, String resourceBundleKey) {
59+
/*this.lessonCompleted = lessonCompleted;
60+
this.feedbackResourceBundleKey = resourceBundleKey;*/
61+
return this;
62+
}
63+
64+
public AttackResultBuilder feedbackArgs(Object... args) {
65+
this.feedbackArgs = args;
66+
return this;
67+
}
68+
69+
public AttackResultBuilder feedback(String resourceBundleKey) {
70+
this.feedbackResourceBundleKey = resourceBundleKey;
71+
return this;
72+
}
73+
74+
/*public AttackResultBuilder feedback(String resourceBundleKey) {
75+
this.feedbackResourceBundleKey = resourceBundleKey;
76+
return this;
77+
}*/
78+
79+
public AttackResultBuilder output(String output) {
80+
this.output = output;
81+
return this;
82+
}
83+
84+
public AttackResultBuilder outputArgs(Object... args) {
85+
this.outputArgs = args;
86+
return this;
87+
}
88+
89+
public AttackResultBuilder attemptWasMade() {
90+
this.attemptWasMade = true;
91+
return this;
92+
}
93+
94+
public AttackResult build() {
95+
return new AttackResult(
96+
lessonCompleted,
97+
messages.getMessage(feedbackResourceBundleKey, feedbackArgs),
98+
messages.getMessage(output, output, outputArgs),
99+
assignment.getClass().getSimpleName(),
100+
attemptWasMade);
101+
}
102+
103+
public AttackResultBuilder assignment(AssignmentEndpoint assignment) {
104+
this.assignment = assignment;
105+
return this;
106+
}
107+
}
108+
109+
@Getter private boolean lessonCompleted;
110+
@Getter private String feedback;
111+
@Getter private String output;
112+
@Getter private final String assignment;
113+
@Getter private boolean attemptWasMade;
114+
//@Getter private boolean comment;
115+
116+
public AttackResult(
117+
boolean lessonCompleted,
118+
String feedback,
119+
String output,
120+
String assignment,
121+
boolean attemptWasMade) {
122+
this.lessonCompleted = lessonCompleted;
123+
this.feedback = escapeJson(feedback);
124+
this.output = escapeJson(output);
125+
this.assignment = assignment;
126+
this.attemptWasMade = attemptWasMade;
127+
}
128+
129+
public static AttackResultBuilder builder(PluginMessages messages) {
130+
return new AttackResultBuilder(messages);
131+
}
132+
133+
public boolean assignmentSolved() {
134+
return lessonCompleted;
135+
}
136+
}

0 commit comments

Comments
 (0)