diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-12-13 20:58:33 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-12-13 21:05:43 +0400 |
commit | 72cecec95b39ff4ece21618d2c6172819033d0d9 (patch) | |
tree | 936f18ad9de2567704fbdf1f8bd22b00d592d003 /plugins/sonar-squid-java-plugin | |
parent | 81cffc0f9e4ba50a873f72de195345216c66559b (diff) | |
download | sonarqube-72cecec95b39ff4ece21618d2c6172819033d0d9.tar.gz sonarqube-72cecec95b39ff4ece21618d2c6172819033d0d9.zip |
SONAR-2018 Improve rule for detection of commented-out code
JSNI methods (GWT) should not be considered as commented-out code.
Diffstat (limited to 'plugins/sonar-squid-java-plugin')
3 files changed, 31 insertions, 3 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java index d4d42781297..da7e1160aa2 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java @@ -23,6 +23,7 @@ import java.util.Arrays; import java.util.List; import java.util.Set; +import org.apache.commons.lang.StringUtils; import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.java.ast.visitor.AstUtils; @@ -78,9 +79,11 @@ public class CommentedOutCodeLineCheck extends JavaAstVisitor { } for (List<TextBlock> listOfComments : getFileContents().getCComments().values()) { - // This list contains not only comments in C style, but also Javadocs + // This list contains not only comments in C style, but also Javadocs and JSNI comments for (TextBlock comment : listOfComments) { - comments.add(comment); + if (!isJSNI(comment)) { + comments.add(comment); + } } } } @@ -123,4 +126,18 @@ public class CommentedOutCodeLineCheck extends JavaAstVisitor { comments = null; } + private static final String START_JSNI = "/*-{"; + private static final String END_JSNI = "}-*/"; + + /** + * From GWT documentation: + * A JSNI comment block begins with the exact token {@link #START_JSNI} and ends with the exact token {@link #END_JSNI}. + */ + private boolean isJSNI(TextBlock comment) { + String[] lines = comment.getText(); + String firstLine = lines[0]; + String lastLine = lines[lines.length - 1]; + return StringUtils.startsWith(firstLine, START_JSNI) && StringUtils.endsWith(lastLine, END_JSNI); + } + } diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java index b231567b3b8..2322b1bbdf5 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java @@ -49,7 +49,7 @@ public class CommentedOutCodeLineCheckTest { public void testDetection() { CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("CommentedCode.java")); - checkMessages.assertNext().atLine(26); + checkMessages.assertNext().atLine(26).withMessage("It's better to remove commented-out line of code."); checkMessages.assertNext().atLine(27); checkMessages.assertNext().atLine(28); diff --git a/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java b/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java index c7aa40ba06e..c2f0aef4955 100644 --- a/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java +++ b/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java @@ -49,6 +49,17 @@ public class CommentedCode { } /** + * From GWT documentation: + * JSNI methods are declared native and contain JavaScript code in a specially formatted comment block + * between the end of the parameter list and the trailing semicolon. + */ + public static native void alert(String msg) /*-{ + for (i=0;i<=5;i++) { + $wnd.alert(msg); + } + }-*/; + + /** * No detection of commented-out code in Javadoc for method * for (Visitor visitor : visitors) { * continue; |