Skip to content

Commit

Permalink
Issue #7763: Update AbstractChecks to log DetailAST - TrailingComment
Browse files Browse the repository at this point in the history
  • Loading branch information
romani committed Dec 2, 2020
1 parent bdeb913 commit 92be3fb
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 59 deletions.
@@ -0,0 +1,82 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 the original author or authors.
//
// This library 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 2.1 of the License, or (at your option) any later version.
//
// This library 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 this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////

package org.checkstyle.suppressionxpathfilter;

import java.io.File;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.checks.TrailingCommentCheck;

public class XpathRegressionTrailingCommentTest extends AbstractXpathTestSupport {
private final String checkName = TrailingCommentCheck.class.getSimpleName();

@Override
protected String getCheckName() {
return checkName;
}

@Test
public void testOne() throws Exception {
final File fileToProcess = new File(getPath(
"SuppressionXpathRegressionTrailingComment1.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(TrailingCommentCheck.class);

final String[] expectedViolation = {
"4:12: " + getCheckMessage(TrailingCommentCheck.class,
TrailingCommentCheck.MSG_KEY),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionTrailingComment1']]"
+ "/OBJBLOCK/SINGLE_LINE_COMMENT"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

@Test
public void testTwo() throws Exception {
final File fileToProcess = new File(getPath(
"SuppressionXpathRegressionTrailingComment2.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(TrailingCommentCheck.class);

final String[] expectedViolation = {
"4:40: " + getCheckMessage(TrailingCommentCheck.class,
TrailingCommentCheck.MSG_KEY),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionTrailingComment2']]"
+ "/OBJBLOCK/SINGLE_LINE_COMMENT"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}
}
@@ -0,0 +1,5 @@
package org.checkstyle.suppressionxpathfilter.trailingcomment;

public class SuppressionXpathRegressionTrailingComment1 {
int i; // don't use trailing comments :) // warn
}
@@ -0,0 +1,5 @@
package org.checkstyle.suppressionxpathfilter.trailingcomment;

public class SuppressionXpathRegressionTrailingComment2 {
int j; /* bad c-style comment. */ // warn
}
Expand Up @@ -19,6 +19,7 @@

package com.puppycrawl.tools.checkstyle.checks;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -29,6 +30,7 @@
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

/**
Expand Down Expand Up @@ -137,6 +139,12 @@ public class TrailingCommentCheck extends AbstractCheck {
*/
public static final String MSG_KEY = "trailing.comments";

/**
* A Map contains illegal lines, the key is lineNo, the value is
* ColumnNo.
*/
private final Map<Integer, Integer> illegalLines = new HashMap<>();

/**
* Define pattern for text allowed in trailing comments.
* (This pattern will not be applied to multiline comments and the text
Expand Down Expand Up @@ -167,6 +175,11 @@ public final void setFormat(Pattern pattern) {
format = pattern;
}

@Override
public boolean isCommentNodesRequired() {
return true;
}

@Override
public int[] getDefaultTokens() {
return getRequiredTokens();
Expand All @@ -179,12 +192,19 @@ public int[] getAcceptableTokens() {

@Override
public int[] getRequiredTokens() {
return CommonUtil.EMPTY_INT_ARRAY;
return new int[] {
TokenTypes.SINGLE_LINE_COMMENT,
TokenTypes.BLOCK_COMMENT_BEGIN,
};
}

@Override
public void visitToken(DetailAST ast) {
throw new IllegalStateException("visitToken() shouldn't be called.");
final int lineNo = ast.getLineNo();
if (illegalLines.get(lineNo) != null
&& illegalLines.get(lineNo).equals(ast.getColumnNo())) {
log(ast, MSG_KEY);
}
}

@Override
Expand Down Expand Up @@ -219,7 +239,7 @@ public void beginTree(DetailAST rootAST) {
}
if (!format.matcher(lineBefore).find()
&& !isLegalComment(comment)) {
log(lineNo, MSG_KEY);
illegalLines.put(lineNo, comment.getStartColNo());
}
}
}
Expand Down
Expand Up @@ -54,9 +54,6 @@
* RegexpSinglelineJava (reason is at
* <a href="https://github.com/checkstyle/checkstyle/issues/7759#issuecomment-605525287"> #7759</a>)
* </li>
* <li>
* TrailingComment
* </li>
* </ul>
* <p>
* Also, the filter does not support suppressions inside javadoc reported by Javadoc checks:
Expand Down
Expand Up @@ -26,9 +26,6 @@
RegexpSinglelineJava (reason is at
&lt;a href="https://github.com/checkstyle/checkstyle/issues/7759#issuecomment-605525287"&gt; #7759&lt;/a&gt;)
&lt;/li&gt;
&lt;li&gt;
TrailingComment
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Also, the filter does not support suppressions inside javadoc reported by Javadoc checks:
Expand Down
Expand Up @@ -21,15 +21,12 @@

import static com.puppycrawl.tools.checkstyle.checks.TrailingCommentCheck.MSG_KEY;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.DetailAstImpl;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

public class TrailingCommentCheckTest extends AbstractModuleTestSupport {

Expand All @@ -41,29 +38,33 @@ protected String getPackageLocation() {
@Test
public void testGetRequiredTokens() {
final TrailingCommentCheck checkObj = new TrailingCommentCheck();
assertArrayEquals(CommonUtil.EMPTY_INT_ARRAY, checkObj.getRequiredTokens(),
final int[] expected = {TokenTypes.SINGLE_LINE_COMMENT,
TokenTypes.BLOCK_COMMENT_BEGIN, };
assertArrayEquals(expected, checkObj.getRequiredTokens(),
"Required tokens array is not empty");
}

@Test
public void testGetAcceptableTokens() {
final TrailingCommentCheck checkObj = new TrailingCommentCheck();
assertArrayEquals(CommonUtil.EMPTY_INT_ARRAY, checkObj.getAcceptableTokens(),
final int[] expected = {TokenTypes.SINGLE_LINE_COMMENT,
TokenTypes.BLOCK_COMMENT_BEGIN, };
assertArrayEquals(expected, checkObj.getAcceptableTokens(),
"Acceptable tokens array is not empty");
}

@Test
public void testDefaults() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(TrailingCommentCheck.class);
final String[] expected = {
"4: " + getCheckMessage(MSG_KEY),
"7: " + getCheckMessage(MSG_KEY),
"8: " + getCheckMessage(MSG_KEY),
"18: " + getCheckMessage(MSG_KEY),
"19: " + getCheckMessage(MSG_KEY),
"29: " + getCheckMessage(MSG_KEY),
"30: " + getCheckMessage(MSG_KEY),
"31: " + getCheckMessage(MSG_KEY),
"4:12: " + getCheckMessage(MSG_KEY),
"7:12: " + getCheckMessage(MSG_KEY),
"8:22: " + getCheckMessage(MSG_KEY),
"18:19: " + getCheckMessage(MSG_KEY),
"19:21: " + getCheckMessage(MSG_KEY),
"29:50: " + getCheckMessage(MSG_KEY),
"30:51: " + getCheckMessage(MSG_KEY),
"31:31: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputTrailingComment.java"), expected);
}
Expand All @@ -73,12 +74,12 @@ public void testLegalComment() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(TrailingCommentCheck.class);
checkConfig.addAttribute("legalComment", "^NOI18N$");
final String[] expected = {
"4: " + getCheckMessage(MSG_KEY),
"7: " + getCheckMessage(MSG_KEY),
"8: " + getCheckMessage(MSG_KEY),
"18: " + getCheckMessage(MSG_KEY),
"19: " + getCheckMessage(MSG_KEY),
"31: " + getCheckMessage(MSG_KEY),
"4:12: " + getCheckMessage(MSG_KEY),
"7:12: " + getCheckMessage(MSG_KEY),
"8:22: " + getCheckMessage(MSG_KEY),
"18:19: " + getCheckMessage(MSG_KEY),
"19:21: " + getCheckMessage(MSG_KEY),
"31:31: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputTrailingComment.java"), expected);
}
Expand All @@ -88,35 +89,22 @@ public void testFormat() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(TrailingCommentCheck.class);
checkConfig.addAttribute("format", "NOT MATCH");
final String[] expected = {
"4: " + getCheckMessage(MSG_KEY),
"5: " + getCheckMessage(MSG_KEY),
"6: " + getCheckMessage(MSG_KEY),
"7: " + getCheckMessage(MSG_KEY),
"8: " + getCheckMessage(MSG_KEY),
"13: " + getCheckMessage(MSG_KEY),
"14: " + getCheckMessage(MSG_KEY),
"15: " + getCheckMessage(MSG_KEY),
"18: " + getCheckMessage(MSG_KEY),
"19: " + getCheckMessage(MSG_KEY),
"26: " + getCheckMessage(MSG_KEY),
"29: " + getCheckMessage(MSG_KEY),
"30: " + getCheckMessage(MSG_KEY),
"31: " + getCheckMessage(MSG_KEY),
"4:12: " + getCheckMessage(MSG_KEY),
"5:5: " + getCheckMessage(MSG_KEY),
"6:5: " + getCheckMessage(MSG_KEY),
"7:12: " + getCheckMessage(MSG_KEY),
"8:22: " + getCheckMessage(MSG_KEY),
"13:17: " + getCheckMessage(MSG_KEY),
"14:7: " + getCheckMessage(MSG_KEY),
"15:5: " + getCheckMessage(MSG_KEY),
"18:19: " + getCheckMessage(MSG_KEY),
"19:21: " + getCheckMessage(MSG_KEY),
"26:5: " + getCheckMessage(MSG_KEY),
"29:50: " + getCheckMessage(MSG_KEY),
"30:51: " + getCheckMessage(MSG_KEY),
"31:31: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputTrailingComment.java"), expected);
}

@Test
public void testCallVisitToken() {
final TrailingCommentCheck check = new TrailingCommentCheck();
try {
check.visitToken(new DetailAstImpl());
fail("IllegalStateException is expected");
}
catch (IllegalStateException ex) {
assertEquals("visitToken() shouldn't be called.", ex.getMessage(),
"Error message is unexpected");
}
}

}
Expand Up @@ -59,8 +59,7 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
"NoCodeInFile (reason is that AST is not generated for a file not containing code)",
"Regexp (reason is at #7759)",
"RegexpSinglelineJava (reason is at #7759)",
"TrailingComment"
"RegexpSinglelineJava (reason is at #7759)"
)));

// Javadoc checks are not compatible with SuppressionXpathFilter
Expand Down
1 change: 0 additions & 1 deletion src/xdocs/config_filters.xml
Expand Up @@ -923,7 +923,6 @@ files=&quot;com[\\/]mycompany[\\/]app[\\/].*IT.java&quot;/&gt;
<li>NoCodeInFile (reason is that AST is not generated for a file not containing code)</li>
<li>Regexp (reason is at <a href="https://github.com/checkstyle/checkstyle/issues/7759#issuecomment-605525287"> #7759</a>)</li>
<li>RegexpSinglelineJava (reason is at <a href="https://github.com/checkstyle/checkstyle/issues/7759#issuecomment-605525287"> #7759</a>)</li>
<li>TrailingComment</li>
</ul>
<p>
Also, the filter does not support suppressions inside javadoc reported by Javadoc checks:
Expand Down

0 comments on commit 92be3fb

Please sign in to comment.