TrailingComma - false positive for map
darxriggs opened this issue · 3 comments
For this map a violation is generated:
def x = [foo: 1,
bar: 2]
For this list no violation is generated:
def x = [1,
2]
I would expect no violation for both maps and lists when the closing square bracket is on the same line as the last entry.
To be clear , I don't believe the rule documentation (or accompanying tests) makes it clear whether there should be a violation if the last element is followed by the closing square bracket. I would probably assume there should be a violation in that case, since that seems to be more in line with the stated rationale for the rule: "Putting this comma in make is easier to change the order of the elements or add new elements on the end." (ignoring the grammatical errors).
In any case, the inconsistency between lists and maps needs to be addressed.
If a trailing comma should always be enforced, the rule then should also check that the closing square bracket is not on the same line as the last entry. Maybe also that the opening square bracket is not on the same line as the first entry.
I have seen code like this, just to prevent the CodeNarc violation.
def x = [1,
2,]
Here the "trailing" comma does not serve the intended purpose.
Yeah, I don't like that style, either. I think I am inclined to NOT cause a violation for either map or list in that situation, and just make sure it is documented behavior for the rule.