Skip to content

Commit

Permalink
Fix MethodEmptyLinesCheck multi-line string issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tfij committed Jul 10, 2023
1 parent c7cfd82 commit 38c07e9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ you have to add the library as a maven dependency to the plugin.
<dependency>
<groupId>pl.tfij</groupId>
<artifactId>check-tfij-style</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
</dependency>
</dependencies>
</plugin>
Expand All @@ -446,7 +446,7 @@ plugins {
}
dependencies {
checkstyle("pl.tfij:check-tfij-style:1.5.0")
checkstyle("pl.tfij:check-tfij-style:1.5.1")
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static pl.tfij.checktfijstyle.checks.DetailASTUtil.getFirstChild;
import static pl.tfij.checktfijstyle.checks.DetailASTUtil.streamRecursively;

public class MethodEmptyLinesCheck extends AbstractCheck {
Expand Down Expand Up @@ -49,15 +50,17 @@ private static Set<Integer> getNotEmptyLines(DetailAST ast) {
.collect(Collectors.toSet());
}

private static List<Integer> getLineNumbers(DetailAST it) {
if (it.getType() == TokenTypes.COMMENT_CONTENT) {
int commentFirstLine = it.getLineNo();
int commentLength = (int) it.getText().lines().count();
return IntStream.range(commentFirstLine, commentFirstLine + commentLength)
.boxed()
.collect(Collectors.toList());
private static List<Integer> getLineNumbers(DetailAST ast) {
if (ast.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) {
int commentFirstLine = ast.getLineNo();
int commentLastLine = getFirstChild(ast, TokenTypes.BLOCK_COMMENT_END).getLineNo();
return intRange(commentFirstLine, commentLastLine);
} else if (ast.getType() == TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) {
int stringFirstLine = ast.getLineNo();
int stringLastLine = getFirstChild(ast, TokenTypes.TEXT_BLOCK_LITERAL_END).getLineNo();
return intRange(stringFirstLine, stringLastLine);
} else {
return List.of(it.getLineNo());
return List.of(ast.getLineNo());
}
}

Expand All @@ -76,4 +79,10 @@ private static List<Integer> findEmptyLines(Set<Integer> notEmptyLines, int firs
.boxed()
.collect(Collectors.toList());
}

private static List<Integer> intRange(int stringFirstLine, int stringLastLine) {
return IntStream.range(stringFirstLine, stringLastLine)
.boxed()
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ void methodsWithEmptyLines() {
checkstyle.assertViolation(19, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(21, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(25, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(32, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(37, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(41, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(42, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(55, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(64, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(73, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(80, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(82, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(83, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(38, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(43, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(47, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(48, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(61, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(70, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(79, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(86, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(88, 1, "Empty lines in methods are not allowed.");
checkstyle.assertViolation(89, 1, "Empty lines in methods are not allowed.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
static class SampleTestClass {

SampleTestClass(String f1, String f2) {
// sample comment
// sample \n \n\r comment
this.f1 = f1;
this.f2 = f2;
}
Expand All @@ -25,6 +25,12 @@ void metthod1() {
.out
.pringline("3");
System.out.pringline("4");
String aString = "a\nb";
String aMultiLineString = """
a
b
""";
System.out.pringline("5");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ void metthod1() {
.out
.pringline("3");
System.out.pringline("4");
String aString = "a\nb";
String aMultiLineString = """
a
b
""";

System.out.pringline("5");
}
Expand Down

0 comments on commit 38c07e9

Please sign in to comment.