Skip to content

allow , and %>% on preceding line before { #1116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* Rename `semicolon_terminator_linter` to `semicolon_linter` for better consistency. `semicolon_terminator_linter` survives but is marked for deprecation. The new linter also has a new signature, taking arguments `allow_compound` and `allow_trailing` to replace the old single argument `semicolon=`, again for signature consistency with other linters.
* Combined several curly brace related linters into a new `brace_linter` (#1041, @AshesITR):
+ `closed_curly_linter()`, also allowing `}]` in addition to `})` and `},` as exceptions.
+ `open_curly_linter()`, no longer linting unnecessary trailing whitespace
+ `open_curly_linter()`, no longer linting unnecessary trailing whitespace and also allowing `,` and `%>%` on
preceding lines as exceptions. (#487, #1028)
+ `paren_brace_linter()`, also linting `if`/`else` and `repeat` with missing whitespace
+ Require `else` to come on the same line as the preceding `}`, if present (#884, @michaelchirico)
+ Require functions spanning multiple lines to use curly braces (@michaelchirico)
Expand Down
4 changes: 4 additions & 0 deletions R/brace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ brace_linter <- function(allow_single_line = FALSE) {
"not(
(@line1 = parent::expr/preceding-sibling::OP-LEFT-BRACE/@line1) or
(@line1 = following-sibling::expr/OP-LEFT-BRACE/@line1)
)",
# allow , and %>% on preceding line
"not(
@line1 = parent::expr/preceding-sibling::*[1][self::OP-COMMA or (self::SPECIAL and text() = '%>%')]/@line2 + 1
)"
))

Expand Down
47 changes: 47 additions & 0 deletions tests/testthat/test-brace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,53 @@ test_that("brace_linter lints braces correctly", {
linter
)

# ,<\n>{ is allowed
expect_lint(
trim_some("
switch(
x,
'a' = do_something(x),
'b' = do_another(x),
{
do_first(x)
do_second(x)
}
)
"),
NULL,
linter
)

expect_lint(
trim_some("
fun(
'This is very very very long text.',
{
message('This is the code.')
message('It\\'s stupid, but proves my point.')
}
)
"),
NULL,
linter
)

# %>%\n{ is allowed
expect_lint(
trim_some("
letters %>%
{
tibble(
lo = .,
hi = toupper(.)
)
} %>%
mutate(row_id = row_number())
"),
NULL,
linter
)

# {{ }} is allowed
expect_lint("{{ x }}", NULL, linter)

Expand Down