]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2018 Improve rule for detection of commented-out code
authorEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 13 Dec 2011 14:10:57 +0000 (18:10 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 13 Dec 2011 15:54:07 +0000 (19:54 +0400)
* Only one violation in one block of comment.
* Proper recognition of Javadocs.

plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java [new file with mode: 0644]
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/CommentedOutCodeLineCheck.java [deleted file]
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java [new file with mode: 0644]
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java [deleted file]
plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java
sonar-squid/src/main/java/org/sonar/squid/api/SourceFile.java
sonar-squid/src/main/java/org/sonar/squid/text/Source.java

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
new file mode 100644 (file)
index 0000000..d4d4278
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ * 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.ast.check;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+
+import org.sonar.check.Priority;
+import org.sonar.check.Rule;
+import org.sonar.java.ast.visitor.AstUtils;
+import org.sonar.java.ast.visitor.JavaAstVisitor;
+import org.sonar.java.recognizer.JavaFootprint;
+import org.sonar.squid.api.CheckMessage;
+import org.sonar.squid.api.SourceFile;
+import org.sonar.squid.recognizer.CodeRecognizer;
+
+import com.google.common.collect.Sets;
+import com.puppycrawl.tools.checkstyle.api.DetailAST;
+import com.puppycrawl.tools.checkstyle.api.TextBlock;
+import com.puppycrawl.tools.checkstyle.api.TokenTypes;
+
+@Rule(key = "CommentedOutCodeLine", priority = Priority.MAJOR)
+public class CommentedOutCodeLineCheck extends JavaAstVisitor {
+
+  private static final double THRESHOLD = 0.9;
+
+  /**
+   * This list was taken from com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck
+   */
+  private static final List<Integer> WANTED_TOKENS = Arrays.asList(
+      TokenTypes.INTERFACE_DEF,
+      TokenTypes.CLASS_DEF,
+      TokenTypes.ANNOTATION_DEF,
+      TokenTypes.ENUM_DEF,
+      TokenTypes.METHOD_DEF,
+      TokenTypes.CTOR_DEF,
+      TokenTypes.VARIABLE_DEF,
+      TokenTypes.ENUM_CONSTANT_DEF,
+      TokenTypes.ANNOTATION_FIELD_DEF,
+      TokenTypes.PACKAGE_DEF);
+
+  private final CodeRecognizer codeRecognizer;
+  private Set<TextBlock> comments;
+
+  public CommentedOutCodeLineCheck() {
+    codeRecognizer = new CodeRecognizer(THRESHOLD, new JavaFootprint());
+  }
+
+  @Override
+  public List<Integer> getWantedTokens() {
+    return WANTED_TOKENS;
+  }
+
+  @Override
+  public void visitFile(DetailAST ast) {
+    comments = Sets.newHashSet();
+
+    for (TextBlock comment : getFileContents().getCppComments().values()) {
+      comments.add(comment);
+    }
+
+    for (List<TextBlock> listOfComments : getFileContents().getCComments().values()) {
+      // This list contains not only comments in C style, but also Javadocs
+      for (TextBlock comment : listOfComments) {
+        comments.add(comment);
+      }
+    }
+  }
+
+  /**
+   * Documentation comments should be recognized only when placed
+   * immediately before class, interface, constructor, method, or field declarations.
+   */
+  @Override
+  public void visitToken(DetailAST ast) {
+    if (shouldHandle(ast)) {
+      TextBlock javadoc = getFileContents().getJavadocBefore(ast.getLineNo());
+      if (javadoc != null) {
+        comments.remove(javadoc);
+      }
+    }
+  }
+
+  private static boolean shouldHandle(DetailAST ast) {
+    if (AstUtils.isType(ast, TokenTypes.VARIABLE_DEF)) {
+      return AstUtils.isClassVariable(ast);
+    }
+    return true;
+  }
+
+  @Override
+  public void leaveFile(DetailAST ast) {
+    SourceFile sourceFile = (SourceFile) peekSourceCode();
+    for (TextBlock comment : comments) {
+      String[] lines = comment.getText();
+      for (int i = 0; i < lines.length; i++) {
+        if (codeRecognizer.isLineOfCode(lines[i])) {
+          CheckMessage message = new CheckMessage(this, "It's better to remove commented-out line of code.");
+          message.setLine(comment.getStartLineNo() + i);
+          sourceFile.log(message);
+          break;
+        }
+      }
+    }
+    comments = null;
+  }
+
+}
index 1be2716847501055cb129e7fc5e66ecf85563cc8..5fd989083c028d398f8e3fee0668f1287c8baf7c 100644 (file)
@@ -51,7 +51,6 @@ public class CommentVisitor extends JavaAstVisitor {
   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
deleted file mode 100644 (file)
index 1e65f01..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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);
-    }
-  }
-
-}
index f2a93a76dbe679d32587bbe29b9b9271a334670a..017ae33a2153063175c02f3940d522c7c8d17adb 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.api.rules.AnnotationRuleParser;
 import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleRepository;
 import org.sonar.java.ast.check.BreakCheck;
+import org.sonar.java.ast.check.CommentedOutCodeLineCheck;
 import org.sonar.java.ast.check.ContinueCheck;
 import org.sonar.java.ast.check.UndocumentedApiCheck;
 import org.sonar.java.bytecode.check.ArchitectureCheck;
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
new file mode 100644 (file)
index 0000000..b231567
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * 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.ast.check;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.java.CheckMessages;
+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() {
+    CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("CommentedCode.java"));
+
+    checkMessages.assertNext().atLine(26);
+    checkMessages.assertNext().atLine(27);
+    checkMessages.assertNext().atLine(28);
+
+    checkMessages.assertNext().atLine(32);
+
+    checkMessages.assertNext().atLine(38);
+    checkMessages.assertNext().atLine(39);
+    checkMessages.assertNext().atLine(40);
+
+    checkMessages.assertNext().atLine(44);
+
+    checkMessages.assertNoMore();
+  }
+
+}
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
deleted file mode 100644 (file)
index a479d3a..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * 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.junit.Before;
-import org.junit.Test;
-import org.sonar.java.CheckMessages;
-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() {
-    CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("CommentedCode.java"));
-    checkMessages.assertNext().atLine(4);
-    checkMessages.assertNext().atLine(17);
-    checkMessages.assertNext().atLine(18);
-    checkMessages.assertNext().atLine(19);
-    checkMessages.assertNoMore();
-  }
-
-}
index 0a44bc77d2a8d71e4ee3dc56e2c85d6527c23add..c7aa40ba06e1b6862124e4492f2785601a499929 100644 (file)
@@ -1,22 +1,60 @@
+/**
+ * No detection of commented-out code in Javadoc for class
+ * for (Visitor visitor : visitors) {
+ *   continue;
+ * }
+ */
 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(){
-   *
+   * No detection of commented-out code in Javadoc for field
+   * for (Visitor visitor : visitors) {
+   *   continue;
    * }
    */
-  public void analyse() {
+  private int field;
+
+  /**
+   * No detection of commented-out code in Javadoc for constructor
+   * for (Visitor visitor : visitors) {
+   *   continue;
+   * }
+   */
+  public CommentedCode(int field) {
+    this.field = field;
+    // This is a comment, but next line is a commented-out code
+    // for (Visitor visitor : visitors) {
+    //   continue;
+    // }
+
     /*
-    for(Visitor visitor : visitors){
-      visitor.scan(line);
+    This is a comment, but next line is a commented-out code
+    for (Visitor visitor : visitors) {
+      continue;
     }
     */
+
+    /* This is a comment, but next line is a commented-out code */
+    /* for (Visitor visitor : visitors) { */
+    /*   continue; */
+    /* } */
+
+    /**
+     * This is not Javadoc, even if it looks like Javadoc and before declaration of variable
+     * for (Visitor visitor : visitors) {
+     *   continue;
+     * }
+     */
+    int a;
+  }
+
+  /**
+   * No detection of commented-out code in Javadoc for method
+   * for (Visitor visitor : visitors) {
+   *   continue;
+   * }
+   */
+  public int getField() {
+    return field;
   }
 }
index 922c5ca26b1f2787115a6d2fbe5264ec7cae0afa..27d06e6f6524349e7237052735761c55a91eeecb 100644 (file)
@@ -26,7 +26,6 @@ import java.util.Set;
 public class SourceFile extends SourceCode {
 
   private Set<Integer> noSonarTagLines = new HashSet<Integer>();
-  private Set<Integer> commentedOutCodeLines = new HashSet<Integer>();
 
   public SourceFile(String key) {
     super(key);
@@ -54,17 +53,4 @@ public class SourceFile extends SourceCode {
     noSonarTagLines.add(line);
   }
 
-  /**
-   * @since 2.13
-   */
-  public Set<Integer> getCommentedOutCodeLines() {
-    return commentedOutCodeLines;
-  }
-
-  /**
-   * @since 2.13
-   */
-  public void addCommentedOutCodeLines(Set<Integer> commentedOutCodeLines) {
-    this.commentedOutCodeLines.addAll(commentedOutCodeLines);
-  }
 }
index 7d4510f8db1b3f0d6f8881da193b3c089b182cc5..8e6b51b47147d82002b3aa9483f6ef3f0e472ce9 100644 (file)
@@ -33,7 +33,6 @@ public class Source {
   private List<Line> lines = new ArrayList<Line>();
   private CodeRecognizer codeRecognizer;
   private Set<Integer> noSonarTagLines = new HashSet<Integer>();
-  private Set<Integer> commentedOutCodeLines = new HashSet<Integer>();
 
   public Source(Reader reader, CodeRecognizer codeRecognizer, String... additionalSingleLineCommentFlag) {
     this.codeRecognizer = codeRecognizer;
@@ -88,7 +87,6 @@ public class Source {
         line.setMeasure(Metric.COMMENT_LINES, 1);
       } else {
         line.setMeasure(Metric.COMMENTED_OUT_CODE_LINES, 1);
-        commentedOutCodeLines.add(line.getLineIndex());
       }
     }
   }
@@ -127,11 +125,4 @@ public class Source {
   public Set<Integer> getNoSonarTagLines() {
     return noSonarTagLines;
   }
-
-  /**
-   * @since 2.13
-   */
-  public Set<Integer> getCommentedOutCodeLines() {
-    return commentedOutCodeLines;
-  }
 }