lintr icon indicating copy to clipboard operation
lintr copied to clipboard

Add a linter to check spaces around colon(s)

Open yu-iskw opened this issue 10 years ago • 8 comments

According to the style guide, :, :: and ::: don’t need spaces around them. It would be nice to have a linter about spaces around colon(s). http://adv-r.had.co.nz/Style.html

yu-iskw avatar Jul 24 '15 04:07 yu-iskw

Have you ever encountered this style in code in the wild? I never have...

jimhester avatar Jul 24 '15 12:07 jimhester

Indeed, there are rare cases to encounter to the style. However, it is important to check it fully.

yu-iskw avatar Jul 24 '15 16:07 yu-iskw

I could imagine this as being part of the infix_spaces_linter -- the linter checks that low-precedence linters do have spaces, and high-precedence linters do not.

Adding 'help wanted' for now -- it should be doable to read the existing infix_spaces_linter code and extend it to this opposite case.

MichaelChirico avatar Apr 05 '22 04:04 MichaelChirico

These operators are high-precedence: ,, ^, @, !, :, ::, :::, {, [, [[, (, ?, $ Of these, some should allow for spaces (or line breaks). I think we should lint !, :, ::, ::: and ? if they have space around them. All others can sometimes be used with whitespace, e.g.

a$very$
  long$
  chain$
  of_list_accesses@
  or_slot_accesses

(1 + 3) ^ 2

AshesITR avatar Jun 19 '22 14:06 AshesITR

Should we just consider "infix-then-linebreak" OK in all cases? Would be easier to implement. Isolating ! and the others feels like a bit of a judgment call -- I agree with the choices at a glance, but it doesn't feel very principled.

MichaelChirico avatar Jul 08 '22 23:07 MichaelChirico

NB :: and ::: produce parse errors when confronted with a line break. From the tidyverse styleguide I'd say linebreaks are not ok for !, since they aren't ok for !! either.

Actually:

::, :::, $, @, [, [[, ^, unary -, unary +, and :

should never be surrounded by spaces according to the style guide. I'd argue that we can make a conscious exception for indexing operators regarding line breaks, because it's sometimes necessary to fulfill line length requirements and implement the rules as written for all others.

AshesITR avatar Jul 11 '22 12:07 AshesITR

I don't think x[\n is a violation of the style guide -- it's not surrounded by spaces, it just has a space on one side.

So I am leaning towards: always allow ${NON_WHITESPACE}${OP}${NEWLINE} for binary operators. In other words <expr> can't start with an operator and then be immediately followed by newline -- a non-operator part of the <expr> has to show up on the first line.

MichaelChirico avatar Jul 13 '22 03:07 MichaelChirico

Well, x[ 1 only has space on one side as well.

I agree with your proposal for all binary operators except : (:: and ::: won't parse with a following newline anyway)

1:
  some_long_code()

is less readable to me than

1:(
  some_long_code()
)

if that line break is really necessary.

AshesITR avatar Jul 15 '22 05:07 AshesITR

Have you ever encountered this style in code in the wild? I never have...

As to whether such code exists... yes it's not too hard to find:

https://github.com/palantir/spark/blob/b605c1b81d2ff75cd917fa067dd72dc3f0d6e6d2/R/pkg/R/utils.R#L660-L665

MichaelChirico avatar Nov 14 '23 23:11 MichaelChirico