mirror of
https://github.com/alibaba/p3c.git
synced 2025-10-14 15:10:54 +00:00
清理不再使用的代码段——跳过带<pre>标签的注释, fixes #150
This commit is contained in:
@@ -44,6 +44,8 @@ public class RemoveCommentedCodeRule extends AbstractAliCommentRule {
|
|||||||
|
|
||||||
private static final Pattern SUPPRESS_PATTERN = Pattern.compile("\\s*///.*", Pattern.DOTALL);
|
private static final Pattern SUPPRESS_PATTERN = Pattern.compile("\\s*///.*", Pattern.DOTALL);
|
||||||
|
|
||||||
|
private static final Pattern PRE_TAG_PATTERN = Pattern.compile(".*<pre>.*", Pattern.DOTALL);
|
||||||
|
|
||||||
private static final Pattern IMPORT_PATTERN = Pattern.compile(".*import\\s(static\\s)?(\\w*\\.)*\\w*;.*",
|
private static final Pattern IMPORT_PATTERN = Pattern.compile(".*import\\s(static\\s)?(\\w*\\.)*\\w*;.*",
|
||||||
Pattern.DOTALL);
|
Pattern.DOTALL);
|
||||||
|
|
||||||
@@ -125,6 +127,11 @@ public class RemoveCommentedCodeRule extends AbstractAliCommentRule {
|
|||||||
protected CommentPatternEnum scanCommentedCode(String content) {
|
protected CommentPatternEnum scanCommentedCode(String content) {
|
||||||
CommentPatternEnum pattern = CommentPatternEnum.NONE;
|
CommentPatternEnum pattern = CommentPatternEnum.NONE;
|
||||||
|
|
||||||
|
// Skip comment which contains pre tag.
|
||||||
|
if (PRE_TAG_PATTERN.matcher(content).matches()) {
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
|
|
||||||
if (IMPORT_PATTERN.matcher(content).matches()) {
|
if (IMPORT_PATTERN.matcher(content).matches()) {
|
||||||
pattern = CommentPatternEnum.IMPORT;
|
pattern = CommentPatternEnum.IMPORT;
|
||||||
} else if (FIELD_PATTERN.matcher(content).matches()) {
|
} else if (FIELD_PATTERN.matcher(content).matches()) {
|
||||||
|
@@ -103,4 +103,37 @@ public class Test {
|
|||||||
<code-ref id="example-in-javadoc" />
|
<code-ref id="example-in-javadoc" />
|
||||||
</test-code>
|
</test-code>
|
||||||
|
|
||||||
|
<code-fragment id="code-without-pre-tag"><![CDATA[
|
||||||
|
/**
|
||||||
|
* import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||||
|
*/
|
||||||
|
public class Test {
|
||||||
|
}
|
||||||
|
]]>
|
||||||
|
</code-fragment>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>Code without pre tag</description>
|
||||||
|
<expected-problems>1</expected-problems>
|
||||||
|
<expected-linenumbers>1</expected-linenumbers>
|
||||||
|
<code-ref id="code-without-pre-tag" />
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
<code-fragment id="code-in-pre-tag"><![CDATA[
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
public class Test {
|
||||||
|
}
|
||||||
|
]]>
|
||||||
|
</code-fragment>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>Code in pre tag</description>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code-ref id="code-in-pre-tag" />
|
||||||
|
</test-code>
|
||||||
|
|
||||||
</test-data>
|
</test-data>
|
Reference in New Issue
Block a user