lintr
lintr copied to clipboard
New linter for comment spacing
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: #
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 ####
.
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
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?
yep so
foo() # c1
double_foo() #c2
will lint on c2
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?
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...)