Checkstyle: New Check: EmptyLineWrappingInBlockCheck

Created on 4 Dec 2017  路  7Comments  路  Source: checkstyle/checkstyle

from #3923 and https://groups.google.com/forum/#!topic/checkstyle/KZn_d8nC4c0

New Check
EmptyLineWrappingInBlockCheck
tokens= [CLASS, METHOD, IF, ...........] (by default only class)
topSeparator = [empty-line | empty-line-allowed | no-empty-line]
bottomSeparator = [empty-line | empty-line-allowed | no-empty-line]

empty-line-allowed == mean no validation.

If you consider all my examples above, we could make config for methods to allow user's empty line at the top and completely forbid it at the bottom.
BlockEmptyLineWrappingCheck
tokens= "METHOD_DEF"
topSeparator = "empty-line-allowed"
bottomSeparator = "no-empty-line"

approved new feature new module

Most helpful comment

I'm on it.

All 7 comments

Hello,

I seen than some changes for AfterLeftCurly and BeforeRightCurly is merged. Could that be used?
Because I did not see it in the documentation.
I want to achieve a check to leave empty space after class def and before last curly of the class.
So this will be valid:

public class MyClass {

// my fields and methods

}

Same for enums and interfaces.

Regards,
Attila

Please be welcome to implement Check by design described in description

Thank you.
I'll write here my quick solution, maybe it will help others.

<module name="RegexpMultiline">
    <property name="format" value="^([^\n ]+ )*(class|interface|enum) [^{]*\{\n[^\n}]"/>
    <property name="message" value="Leave empty row after class/interface/enum definition!"/>
    <property name="severity" value="error"/>
    <property name="fileExtensions" value="groovy,java"/>
</module>

<module name="RegexpMultiline">
    <property name="format" value="[^\n{]\n\}\n"/>
    <property name="message" value="Leave empty row before end of class/interface/enum!"/>
    <property name="severity" value="error"/>
    <property name="fileExtensions" value="groovy,java"/>
</module>

I'm on it.

@romani What is the difference between empty-line and empty-line-allowed option?

@romani What is the difference between empty-line and empty-line-allowed option?

Both properties top/bottom separator has three options:

  • do nothing (allow-empty-line)
  • requre (empty-line)
  • forbid (no-empty-line)

The default config should process only classes/interfaces/annotations/enums and require that there is one empty line after opening { and one empty line before closind }.

The following config should enforce that methods/constructors has NO empty lines at the beginning/end of the method body:

<module name="EmptyLineWrappingInBlock">
  <property name="tokens" value="CTOR_DEF,METHOD_DEF"/>
  <property name="topSeparator" value="no-empty-line"/>
  <property name="bottomSeparator" value="no-empty-line"/>
</module>

Also note that this check should ignore empty blocks at all, since the empty block will be always violated when topSeparator is set to no-empty-line but bottomSeparator is set to its opposite empty-line (or vice versa). Also, some coding styles (Google for example) allows them to be inline: {}.

Okay, understood.
Thanks 馃憤

Was this page helpful?
0 / 5 - 0 ratings