Skip to content

Commit 08960d1

Browse files
brace_linter() always allows {}/{ } (#1685)
* brace_linter() always allows {}/{ } * NEWS Co-authored-by: Indrajeet Patil <[email protected]>
1 parent 964469b commit 08960d1

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
* `spaces_inside_linter()` allows terminal missing keyword arguments (e.g. `alist(arg = )`; #540, @MichaelChirico)
1919

20+
* `brace_linter()` allows empty braced expression on the same line (e.g. `while (updating_condition()) { }`)
21+
regardless of `allow_single_line` to match the corresponding behavior in {styler}. This is an expedient while
22+
the style guide on handling this case awaits clarification: https://github.com/tidyverse/style/issues/191.
23+
(#1346, @MichaelChirico)
24+
2025
## New and improved features
2126

2227
* New `get_r_string()` helper to get the R-equivalent value of a string, especially useful for R-4-style raw strings.

R/brace_linter.R

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,36 @@ brace_linter <- function(allow_single_line = FALSE) {
5757
},
5858
# double curly
5959
"not(
60-
(@line1 = parent::expr/preceding-sibling::OP-LEFT-BRACE/@line1) or
61-
(@line1 = following-sibling::expr/OP-LEFT-BRACE/@line1)
60+
(@line1 = parent::expr/preceding-sibling::OP-LEFT-BRACE/@line1)
61+
or (@line1 = following-sibling::expr/OP-LEFT-BRACE/@line1)
6262
)",
6363
# allow `(`, `,` and `%>%` on preceding line
6464
#
6565
# note that '{' is not supported in RHS call of base-R's native pipe (`|>`),
6666
# so no exception needs to be made for this operator
6767
"not(
6868
@line1 > parent::expr/preceding-sibling::*[not(self::COMMENT)][1][
69-
self::OP-LEFT-PAREN or
70-
self::OP-COMMA or
71-
(self::SPECIAL and text() = '%>%')
69+
self::OP-LEFT-PAREN
70+
or self::OP-COMMA
71+
or (self::SPECIAL and text() = '%>%')
7272
]/@line2
7373
)"
7474
))
7575

7676
# TODO (AshesITR): if c_style_braces is TRUE, invert the preceding-sibling condition
7777
xp_open_curly <- glue::glue("//OP-LEFT-BRACE[
78-
{ xp_cond_open } and (
79-
not(@line1 = parent::expr/preceding-sibling::*/@line2) or
80-
@line1 = following-sibling::*[1][not(self::COMMENT)]/@line1
78+
{ xp_cond_open }
79+
and (
80+
not(@line1 = parent::expr/preceding-sibling::*/@line2)
81+
or @line1 = following-sibling::*[1][not(self::COMMENT or self::OP-RIGHT-BRACE)]/@line1
8182
)
8283
]")
8384

8485
xp_open_preceding <- "parent::expr/preceding-sibling::*[1][self::OP-RIGHT-PAREN or self::ELSE or self::REPEAT]"
8586

8687
xp_paren_brace <- glue::glue("//OP-LEFT-BRACE[
8788
@line1 = { xp_open_preceding }/@line1
88-
and
89-
@col1 = { xp_open_preceding }/@col2 + 1
89+
and @col1 = { xp_open_preceding }/@col2 + 1
9090
]")
9191

9292
xp_cond_closed <- xp_and(c(
@@ -98,20 +98,22 @@ brace_linter <- function(allow_single_line = FALSE) {
9898
"not(
9999
@line1 = ancestor::expr/following-sibling::*[1][
100100
self::OP-COMMA or self::OP-RIGHT-BRACKET or self::OP-RIGHT-PAREN
101-
]/@line1
101+
]
102+
/@line1
102103
)",
103104
# double curly
104105
"not(
105-
(@line1 = parent::expr/following-sibling::OP-RIGHT-BRACE/@line1) or
106-
(@line1 = preceding-sibling::expr/OP-RIGHT-BRACE/@line1)
106+
(@line1 = parent::expr/following-sibling::OP-RIGHT-BRACE/@line1)
107+
or (@line1 = preceding-sibling::expr/OP-RIGHT-BRACE/@line1)
107108
)"
108109
))
109110

110111
# TODO (AshesITR): if c_style_braces is TRUE, skip the not(ELSE) condition
111112
xp_closed_curly <- glue::glue("//OP-RIGHT-BRACE[
112-
{ xp_cond_closed } and (
113-
(@line1 = preceding-sibling::*[1]/@line2) or
114-
(@line1 = parent::expr/following-sibling::*[1][not(self::ELSE)]/@line1)
113+
{ xp_cond_closed }
114+
and (
115+
(@line1 = preceding-sibling::*[1][not(self::OP-LEFT-BRACE)]/@line2)
116+
or (@line1 = parent::expr/following-sibling::*[1][not(self::ELSE)]/@line1)
115117
)
116118
]")
117119

tests/testthat/test-brace_linter.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,14 @@ test_that("brace_linter lints if/else matching braces correctly", {
409409
linter
410410
)
411411
})
412+
413+
# Keep up to date with https://github.com/tidyverse/style/issues/191
414+
test_that("empty brace expressions are always allowed inline", {
415+
expect_lint("while (FALSE) {}", NULL, brace_linter())
416+
expect_lint("while (FALSE) { }", NULL, brace_linter())
417+
# only applies when `{` is "attached" to the preceding token on the same line
418+
expect_lint("while (FALSE)\n{}", rex::rex("Opening curly braces"), brace_linter())
419+
expect_lint("while (FALSE)\n{ }", rex::rex("Opening curly braces"), brace_linter())
420+
expect_lint("while (FALSE) {}", NULL, brace_linter(allow_single_line = TRUE))
421+
expect_lint("while (FALSE) { }", NULL, brace_linter(allow_single_line = TRUE))
422+
})

0 commit comments

Comments
 (0)