Issue #2067: Fix false-negatives in EmptyLineSeparatorCheck
This commit is contained in:
parent
fb2ec00cc7
commit
3b26b571a8
|
|
@ -161,6 +161,13 @@ public class EmptyLineSeparatorCheck extends Check {
|
|||
*/
|
||||
public static final String MSG_MULTIPLE_LINES = "empty.line.separator.multiple.lines";
|
||||
|
||||
/**
|
||||
* A key is pointing to the warning message empty.line.separator.lines.after
|
||||
* in "messages.properties" file.
|
||||
*/
|
||||
public static final String MSG_MULTIPLE_LINES_AFTER =
|
||||
"empty.line.separator.multiple.lines.after";
|
||||
|
||||
/** Allows no empty line between fields. */
|
||||
private boolean allowNoEmptyLineBetweenFields;
|
||||
|
||||
|
|
@ -212,8 +219,11 @@ public class EmptyLineSeparatorCheck extends Check {
|
|||
|
||||
@Override
|
||||
public void visitToken(DetailAST ast) {
|
||||
final DetailAST nextToken = ast.getNextSibling();
|
||||
if (hasMultipleLinesBefore(ast)) {
|
||||
log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText());
|
||||
}
|
||||
|
||||
final DetailAST nextToken = ast.getNextSibling();
|
||||
if (nextToken != null) {
|
||||
final int astType = ast.getType();
|
||||
switch (astType) {
|
||||
|
|
@ -227,16 +237,34 @@ public class EmptyLineSeparatorCheck extends Check {
|
|||
processPackage(ast, nextToken);
|
||||
break;
|
||||
default:
|
||||
if (nextToken.getType() != TokenTypes.RCURLY && !hasEmptyLineAfter(ast)) {
|
||||
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED, nextToken.getText());
|
||||
if (nextToken.getType() == TokenTypes.RCURLY) {
|
||||
if (hasNotAllowedTwoEmptyLinesBefore(nextToken)) {
|
||||
log(ast.getLineNo(), MSG_MULTIPLE_LINES_AFTER, ast.getText());
|
||||
}
|
||||
}
|
||||
if (hasNotAllowedTwoEmptyLinesBefore(ast)) {
|
||||
log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText());
|
||||
else if (!hasEmptyLineAfter(ast)) {
|
||||
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED,
|
||||
nextToken.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the token has not allowed multiple empty lines before.
|
||||
* @param ast the ast to check.
|
||||
* @return true if the token has not allowed multiple empty lines before.
|
||||
*/
|
||||
private boolean hasMultipleLinesBefore(DetailAST ast) {
|
||||
boolean result = false;
|
||||
if ((ast.getType() != TokenTypes.VARIABLE_DEF
|
||||
|| isTypeField(ast))
|
||||
&& hasNotAllowedTwoEmptyLinesBefore(ast)) {
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process Package.
|
||||
* @param ast token
|
||||
|
|
@ -249,9 +277,6 @@ public class EmptyLineSeparatorCheck extends Check {
|
|||
if (!hasEmptyLineAfter(ast)) {
|
||||
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED, nextToken.getText());
|
||||
}
|
||||
if (hasNotAllowedTwoEmptyLinesBefore(ast)) {
|
||||
log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -264,9 +289,6 @@ public class EmptyLineSeparatorCheck extends Check {
|
|||
if (astType != nextToken.getType() && !hasEmptyLineAfter(ast)) {
|
||||
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED, nextToken.getText());
|
||||
}
|
||||
if (hasNotAllowedTwoEmptyLinesBefore(ast)) {
|
||||
log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -280,9 +302,6 @@ public class EmptyLineSeparatorCheck extends Check {
|
|||
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED,
|
||||
nextToken.getText());
|
||||
}
|
||||
if (isTypeField(ast) && hasNotAllowedTwoEmptyLinesBefore(ast)) {
|
||||
log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
empty.line.separator=''{0}'' should be separated from previous statement.
|
||||
empty.line.separator.multiple.lines=''{0}'' has more than 1 empty lines before.
|
||||
empty.line.separator.multiple.lines.after=''{0}'' has more than 1 empty lines after.
|
||||
|
||||
containsTab=Line contains a tab character.
|
||||
file.containsTab=File contains tab characters (this is the first instance).
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
empty.line.separator=''{0}'' sollte vom vorangehenden Ausdruck getrennt stehen.
|
||||
empty.line.separator.multiple.lines.after=''{0}'' hat mehr als 1 Leerzeilen nach.
|
||||
|
||||
containsTab=Zeile enthält ein TAB-Zeichen
|
||||
file.containsTab=Datei enthält Tabulatorzeichen (diese Stelle ist das erste Vorkommnen).
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ ws.typeCast=''conversión de tipos'' no está seguido de espacio en blanco.
|
|||
|
||||
empty.line.separator = ''{0}'' debe ser separado de la declaración anterior.
|
||||
empty.line.separator.multiple.lines = ''{0}'' cuenta con más de 1 líneas vacías antes.
|
||||
empty.line.separator.multiple.lines.after=''{0}'' cuenta con más de 1 líneas vacías después.
|
||||
file.containsTab = Archivo contiene caracteres de tabulación (este es el primer ejemplo).
|
||||
no.line.wrap = {0} declaración no debe ser la línea envuelto.
|
||||
ws.illegalFollow = ''{0}'' es seguido por un carácter ilegal.
|
||||
|
|
|
|||
|
|
@ -14,5 +14,6 @@ ws.illegalFollow=''{0}'' seuraa laiton merkki.
|
|||
|
||||
empty.line.separator = ''{0}'' olisi erotettava edellisen selonteon.
|
||||
empty.line.separator.multiple.lines = ''{0}'' on yli 1 tyhjää riviä ennen.
|
||||
empty.line.separator.multiple.lines.after=''{0}'' on yli 1 tyhjää riviä jälkeen.
|
||||
file.containsTab = Tiedosto sisältää sarkainmerkeillä (tämä on ensisijaisesti).
|
||||
no.line.wrap = {0} lausunto ei pitäisi olla linja-kääritty.
|
||||
|
|
|
|||
|
|
@ -14,5 +14,6 @@ ws.illegalFollow=''{0}'' est suivi par un caractère illégal.
|
|||
|
||||
empty.line.separator = ''{0}'' doit être séparé de la déclaration précédente.
|
||||
empty.line.separator.multiple.lines = ''{0}'' a plus de 1 lignes vides avant.
|
||||
empty.line.separator.multiple.lines.after=''{0}'' compte plus de 1 lignes vides après.
|
||||
file.containsTab = Fichier contient des caractères de tabulation (ce qui est le premier exemple).
|
||||
no.line.wrap = {0} déclaration ne devrait pas être sur des lignes enveloppé.
|
||||
|
|
|
|||
|
|
@ -14,5 +14,6 @@ ws.illegalFollow=が ''{0}'' 不正な文字が続いています。
|
|||
|
||||
empty.line.separator = ''{0}'' 前の文から分離する必要があります。
|
||||
empty.line.separator.multiple.lines = ''{0}'' の前に1以上の空行を持っています。
|
||||
empty.line.separator.multiple.lines.after=''{0}'' 後の1以上の空行を持っています。
|
||||
file.containsTab = ファイルが(これが最初のインスタンスである)タブ文字が含まれています。
|
||||
no.line.wrap = {0} 文は、行ラップされてはなりません。
|
||||
|
|
|
|||
|
|
@ -14,5 +14,6 @@ ws.illegalFollow=''{0}'' é seguido por um carácter ilegal.
|
|||
|
||||
empty.line.separator = ''{0}'' deve ser separada da declaração anterior.
|
||||
empty.line.separator.multiple.lines = ''{0}'' tem mais de 1 linhas vazias antes.
|
||||
empty.line.separator.multiple.lines.after=''{0}'' tem mais de 1 linhas vazias depois.
|
||||
file.containsTab = Arquivo contém caracteres de tabulação (esta é a primeira instância).
|
||||
no.line.wrap = {0} afirmação não deve ser linha-embrulhado.
|
||||
|
|
|
|||
|
|
@ -18,4 +18,5 @@ ws.typeCast=''türü dönüştürme'' ifadesinden sonra boşluk kullanılmamış
|
|||
|
||||
empty.line.separator = {0} 'Bir önceki deyimi ayrılmalıdır.
|
||||
empty.line.separator.multiple.lines = {0} daha önce en fazla 1 boş hatları vardır.
|
||||
empty.line.separator.multiple.lines.after=''{0}'' sonra 1'den fazla boş hatları vardır.
|
||||
no.line.wrap = {0} ifadesi hattı sarılı olmamalıdır.
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@
|
|||
package com.puppycrawl.tools.checkstyle.checks.whitespace;
|
||||
|
||||
import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES;
|
||||
import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES_AFTER;
|
||||
import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
|
|
@ -100,9 +101,11 @@ public class EmptyLineSeparatorCheckTest
|
|||
final String[] expected = {
|
||||
"21: " + getCheckMessage(MSG_MULTIPLE_LINES, "package"),
|
||||
"24: " + getCheckMessage(MSG_MULTIPLE_LINES, "import"),
|
||||
"29: " + getCheckMessage(MSG_MULTIPLE_LINES, "CLASS_DEF"),
|
||||
"33: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
|
||||
"38: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
|
||||
"43: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
|
||||
"45: " + getCheckMessage(MSG_MULTIPLE_LINES_AFTER, "METHOD_DEF"),
|
||||
};
|
||||
verify(checkConfig, getPath("InputEmptyLineSeparatorMultipleEmptyLines.java"), expected);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,5 +43,6 @@ public class InputEmptyLineSeparatorMultipleEmptyLines
|
|||
private static void foo() {}
|
||||
|
||||
private static void foo1() {}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue