Skip to content

Commit

Permalink
Issue checkstyle#10924: Update TrailingCommentCheck to use code points
Browse files Browse the repository at this point in the history
  • Loading branch information
MUzairS15 committed Mar 18, 2022
1 parent b6b5852 commit ab39e3e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
Expand Up @@ -19,6 +19,7 @@

package com.puppycrawl.tools.checkstyle.checks;

import java.util.Arrays;
import java.util.regex.Pattern;

import com.puppycrawl.tools.checkstyle.StatelessCheck;
Expand Down Expand Up @@ -243,8 +244,9 @@ public void visitToken(DetailAST ast) {
private void checkSingleLineComment(DetailAST ast) {
final int lineNo = ast.getLineNo();
final String comment = ast.getFirstChild().getText();
final String line = getLines()[lineNo - 1];
final String lineBefore = line.substring(0, ast.getColumnNo());
final int[] lineBeforeCodePoints =
Arrays.copyOfRange(getLineCodePoints(lineNo - 1), 0, ast.getColumnNo());
final String lineBefore = new String(lineBeforeCodePoints, 0, lineBeforeCodePoints.length);

if (!format.matcher(lineBefore).find() && !isLegalCommentContent(comment)) {
log(ast, MSG_KEY);
Expand All @@ -261,15 +263,19 @@ private void checkBlockComment(DetailAST ast) {
final DetailAST firstChild = ast.getFirstChild();
final DetailAST lastChild = ast.getLastChild();
final String comment = firstChild.getText();
String line = getLines()[lineNo - 1];
int[] lineCodePoints = getLineCodePoints(lineNo - 1);

if (line.length() > lastChild.getColumnNo() + 1) {
line = line.substring(lastChild.getColumnNo() + 2);
if (lineCodePoints.length > lastChild.getColumnNo() + 1) {
lineCodePoints = Arrays.copyOfRange(lineCodePoints,
lastChild.getColumnNo() + 2, lineCodePoints.length);
}

String line = new String(lineCodePoints, 0, lineCodePoints.length);
line = FORMAT_LINE.matcher(line).replaceAll("");

final String lineBefore = getLines()[lineNo - 1].substring(0, ast.getColumnNo());
final int[] lineBeforeCodePoints =
Arrays.copyOfRange(getLineCodePoints(lineNo - 1), 0, ast.getColumnNo());
final String lineBefore = new String(lineBeforeCodePoints, 0, lineBeforeCodePoints.length);
final boolean isCommentAtEndOfLine = ast.getLineNo() != lastChild.getLineNo()
|| CommonUtil.isBlank(line);
final boolean isLegalBlockComment = isLegalCommentContent(comment)
Expand Down
Expand Up @@ -119,4 +119,19 @@ public void testLegalCommentWithNoPrecedingWhitespace() throws Exception {
verifyWithInlineConfigParser(
getPath("InputTrailingCommentWithNoPrecedingWhitespace.java"), expected);
}

@Test
public void testWithEmoji() throws Exception {
final String[] expected = {
"13:24: " + getCheckMessage(MSG_KEY),
"15:27: " + getCheckMessage(MSG_KEY),
"21:33: " + getCheckMessage(MSG_KEY),
"25:13: " + getCheckMessage(MSG_KEY),
"27:16: " + getCheckMessage(MSG_KEY),
"28:24: " + getCheckMessage(MSG_KEY),
"33:37: " + getCheckMessage(MSG_KEY),
};
verifyWithInlineConfigParser(
getPath("InputTrailingCommentWithEmoji.java"), expected);
}
}
@@ -0,0 +1,34 @@
/*
TrailingComment
format = (default)^[\s});]*$
legalComment = ^ this is ok
*/

package com.puppycrawl.tools.checkstyle.checks.trailingcomment;

public class InputTrailingCommentWithEmoji {
// violation below
String a = "🧐🥳😠😨"; /*string with emoji */
// violation below
String b = "👌🏻🤞🏻😂😂🎄"; // another string

/* yet another */String c = "😂😂🎄👍"; /* this is ok */
String d = "🧐🥳"; // this is ok
/*
* 🎄a🎄b🎄c🎄 // violation below
* 🎄 👌🏻 🤘🏻 🎄*/ void test1() { /* some
🎄 adsad 🎄 /*comments */
}
// violation below
/*😂 😂*/ // 🤛🏻🤛🏻

/* 🎃 ☠️ */ // 👿 asd😱 // violation
/* 😱 🎄 */ /*🎄 🥶 */ // violation

/** // comment
* 🤛🏻q🥳w👆🏻e😠r👇🏻t😨y
*/
/* 👆🏻 👇🏻 */ void test2() {} // violation
}

0 comments on commit ab39e3e

Please sign in to comment.