diff options
Diffstat (limited to 'plugins')
7 files changed, 130 insertions, 2 deletions
diff --git a/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava.properties b/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava.properties index f26cabba546..c178b1b078c 100644 --- a/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava.properties +++ b/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava.properties @@ -27,3 +27,5 @@ rule.squid.UndocumentedApi.param.forClasses=Optional. If this property is not de rule.squid.UnusedPrivateMethod.name=Unused private method rule.squid.UnusedProtectedMethod.name=Unused protected method + +rule.squid.CommentedOutCodeLine.name=Avoid commented-out lines of code diff --git a/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava/CommentedOutCodeLine.html b/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava/CommentedOutCodeLine.html new file mode 100644 index 00000000000..f5e5d238f24 --- /dev/null +++ b/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava/CommentedOutCodeLine.html @@ -0,0 +1,8 @@ +<p>Here are the main reasons why commented code is a code smell :</p> +<ul> + <li>It always raises more questions than it gives answers</li> + <li>Everybody will forget very quickly how relevant the commented code is</li> + <li>This is distraction when going down the code as it stops the flow of eyes</li> + <li>It is a bad SCM engine : Subversion, CVS and Git are really more trustworthy !</li> + <li>The simple fact of understanding why code was commented out in the first place can take a lot of time</li> +</ul> diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java index 9d013afd8e5..1be27168475 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java @@ -49,9 +49,9 @@ public class CommentVisitor extends JavaAstVisitor { @Override public void visitFile(DetailAST ast) { - SourceFile file = (SourceFile) peekSourceCode(); file.addNoSonarTagLines(getSource().getNoSonarTagLines()); + file.addCommentedOutCodeLines(getSource().getCommentedOutCodeLines()); file.setMeasure(Metric.HEADER_COMMENT_LINES, getSource().getMeasure(Metric.HEADER_COMMENT_LINES)); file.setMeasure(Metric.COMMENTED_OUT_CODE_LINES, getSource().getMeasure(Metric.COMMENTED_OUT_CODE_LINES)); file.setMeasure(Metric.COMMENT_LINES, getSource().getMeasure(Metric.COMMENT_LINES)); diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/CommentedOutCodeLineCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/CommentedOutCodeLineCheck.java new file mode 100644 index 00000000000..1e65f01c51b --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/CommentedOutCodeLineCheck.java @@ -0,0 +1,39 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.java.squid.check; + +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonar.squid.api.CheckMessage; +import org.sonar.squid.api.SourceFile; + +@Rule(key = "CommentedOutCodeLine", priority = Priority.MAJOR) +public class CommentedOutCodeLineCheck extends SquidCheck { + + @Override + public void visitFile(SourceFile sourceFile) { + for (Integer line : sourceFile.getCommentedOutCodeLines()) { + CheckMessage message = new CheckMessage(this, "It's better to remove commented-out line of code."); + message.setLine(line); + sourceFile.log(message); + } + } + +} diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java index fe31dba88ab..f2a93a76dbe 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java @@ -58,6 +58,7 @@ public final class SquidRuleRepository extends RuleRepository { // AST checks UndocumentedApiCheck.class, ContinueCheck.class, BreakCheck.class, // Squid checks - DITCheck.class, ClassComplexityCheck.class, MethodComplexityCheck.class, NoSonarCheck.class, EmptyFileCheck.class); + DITCheck.class, ClassComplexityCheck.class, MethodComplexityCheck.class, NoSonarCheck.class, EmptyFileCheck.class, + CommentedOutCodeLineCheck.class); } } diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java new file mode 100644 index 00000000000..aee6c74b360 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java @@ -0,0 +1,56 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.java.squid.check; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.java.ast.JavaAstScanner; +import org.sonar.java.ast.SquidTestUtils; +import org.sonar.java.squid.JavaSquidConfiguration; +import org.sonar.java.squid.SquidScanner; +import org.sonar.squid.Squid; +import org.sonar.squid.api.SourceFile; +import org.sonar.squid.measures.Metric; + +public class CommentedOutCodeLineCheckTest { + + private Squid squid; + + @Before + public void setUp() { + squid = new Squid(new JavaSquidConfiguration()); + CommentedOutCodeLineCheck check = new CommentedOutCodeLineCheck(); + squid.registerVisitor(check); + JavaAstScanner scanner = squid.register(JavaAstScanner.class); + scanner.scanFile(SquidTestUtils.getInputFile("/rules/CommentedCode.java")); + squid.decorateSourceCodeTreeWith(Metric.values()); + squid.register(SquidScanner.class).scan(); + } + + @Test + public void testDetection() { + SourceFile file = (SourceFile) squid.search("CommentedCode.java"); + assertThat(file.getCheckMessages().size(), is(4)); + } + +} diff --git a/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java b/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java new file mode 100644 index 00000000000..0a44bc77d2a --- /dev/null +++ b/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java @@ -0,0 +1,22 @@ +public class CommentedCode { + public CommentedCode() { + //This is a comment + //if (true) { + int i = 2; + } + + /** + * No detection of commented code line in Javadoc bloc + * + * public void main(){ + * + * } + */ + public void analyse() { + /* + for(Visitor visitor : visitors){ + visitor.scan(line); + } + */ + } +} |