Skip to content

Commit

Permalink
Merge pull request alibaba#151 from kerie/master
Browse files Browse the repository at this point in the history
清理不再使用的代码段——跳过带<pre>标签的注释, fixes alibaba#150
  • Loading branch information
SeanCai authored Oct 31, 2017
2 parents d9792ef + 6fada8e commit 40bcf54
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class RemoveCommentedCodeRule extends AbstractAliCommentRule {

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*;.*",
Pattern.DOTALL);

Expand Down Expand Up @@ -125,6 +127,11 @@ protected void checkCommentsBetweenDeclarations(ASTCompilationUnit cUnit, Object
protected CommentPatternEnum scanCommentedCode(String content) {
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()) {
pattern = CommentPatternEnum.IMPORT;
} else if (FIELD_PATTERN.matcher(content).matches()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,37 @@ public class Test {
<code-ref id="example-in-javadoc" />
</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>

0 comments on commit 40bcf54

Please sign in to comment.