Whitespace checks flexibility
I'd like to enforce anonymous object declarations that look like this:
{ key: value, key: value }
I think this is currently not possible (except for the space after the comma with WhitespaceAfter and the , token).
- Using the
:token inWhitespaceAfterdoesn't work, since there is no way to differentiate between object declarations and type hints (I still wantname:Typefor the latter). - Using the
{token inWhitespaceAfterdoesn't work, since it also triggers on empty blocks{}and reification expressions. - Using the
}token inWhitespaceBeforedoesn't work, since there is noWhitespaceBeforecheck yet ;)
How about writing a generic WhitespaceCheck, that has four modes: NoWhitespace, OnlyBefore, OnlyAfter and WhitespaceAround. It also has a list of tokens like in WhitespaceAround and a scope / context that limits where the mode applies. Possible scopes / contexts are ObjectDecl, FunctionDef, FieldDef, ArrayAccess, etc.
Each check instance has one mode, multiple tokens and multiple scopes / contexts.
Depending on coding style WhitespaceCheck would occur multiple times in checkstyle.json, with different mode, token and context combinations, but I think that could cover all cases and we might even incorporate SpacingCheck (by opening up the range of supported tokens).
I have not started development, so if you would like to work on it, go ahead.
Sounds good to me, I actually had something similar in mind!
For the modes I'd suggest different names: Before, After, Around and None.
One question is whether the old whitespace checks should be removed when adding this (breaking change).
Maybe we can keep them (marked as deprecated), but refactor them to use new whitespace check with matching parameters, so they are like dumbed down versions of the full check.
Seems like a good idea!