lintr icon indicating copy to clipboard operation
lintr copied to clipboard

New linter for comment spacing

Open MichaelChirico opened this issue 2 years ago • 6 comments

styler enforces comment spacing:

styler::style_text("a #b")
# a # b

We could throw a lint calling out this rule as well.

This is part of the style guide, so it should be a default linter:

https://style.tidyverse.org/syntax.html#comments

Each line of a comment should begin with the comment symbol and a single space: #

MichaelChirico avatar May 10 '22 21:05 MichaelChirico

I think we should be a little bit more lenient than requiring exactly one space by default, though.

Using more than 1 space can be useful to emphasize, e.g. an equation.

# The foo can be determined by:
#    (bar + baz) ^ 2 - boo

Also, I prefer #> 1 + 1 to # > 1 + 1 as a workaround to commented code lints. WDYT about requiring at least one space and optionally a, possibly repeated, preceding non-alphanumeric?

This would also allow flexible "heading" comments like #### Heading ####.

AshesITR avatar May 11 '22 04:05 AshesITR

I was thinking to let the allowed chars be configurable. that way it covers roxygen comments, but also any other meta commenting systems that might be out there. by default we'd allow ' # and > to come right after #.

good point about hanging indent comments... that's definitely my own personal style. we could (1) only enforce the one-space rule on the first line of a block of comments (2) have a strict mode (like for assignment_spaces_linter) that toggles whether exactly-one or just non-zero spaces is enforced or (3) always allows a number>0 of spaces

MichaelChirico avatar May 11 '22 04:05 MichaelChirico

I like (1) with an option to activate (3). Good thinking of the roxygen style.

A block would be defined as # starting on the same column in subsequent lines, right?

AshesITR avatar May 11 '22 05:05 AshesITR

yep so

foo() # c1
double_foo() #c2

will lint on c2

MichaelChirico avatar May 11 '22 05:05 MichaelChirico

How do we want to deal with repetitions of allowed symbols? I can generally see them used for heading styles, e.g. #----- a ----- and ##### a #####.

So block comment starts should match ^#[#'>-]*\ [^\ ] and continuation lines should match ^#[#'>-]*\ +[^\ ] by default, right?

AshesITR avatar May 11 '22 05:05 AshesITR

that sounds right. my first reaction was that subsequent lines should match the same repetition but that would preclude decorative comments like

###########
# comment #
###########

(although, it would be fine if the decorative comment starts with a space...)

MichaelChirico avatar May 11 '22 06:05 MichaelChirico