Skip to content

Commit 9a98927

Browse files
special handling when %>% comes before conditional
1 parent 27eee4a commit 9a98927

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454

5555
## Minor changes and fixes
5656

57+
* No curly braces are added to else statements if they are within a pipe, as
58+
this can change evaluation logic of code involving the magrittr dot in rare
59+
cases (#816).
5760
* line breaks between `}` and `else` are removed (#793).
5861
* styler won't format code chunks with explicit `tidy = FALSE` in an Rmd or Rnw
5962
code header anymore. This can be handy when the code can't be parsed, e.g.

R/rules-tokens.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ wrap_else_multiline_curly <- function(pd, indent_by = 2, space_after = 0) {
113113
if (contains_else_expr(pd) &&
114114
pd_is_multi_line(pd) &&
115115
contains_else_expr_that_needs_braces(pd) &&
116-
!any(pd$stylerignore)) {
116+
!any(pd$stylerignore) &&
117+
pd$token_before[1] != "SPECIAL-PIPE") {
117118
else_idx <- which(pd$token == "ELSE")
118119
pd$spaces[else_idx] <- 1L
119120
all_to_be_wrapped_ind <- seq2(else_idx + 1L, nrow(pd))

tests/testthat/test-token_adding_removing.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ test_that("braces only added to pipe if RHS is a symbol", {
3636
transformer = style_text
3737
), NA)
3838
})
39+
40+
41+
test_that("No braces are added if conditional statement is within pipe", {
42+
expect_warning(test_collection("token_adding_removing", "else-pipe",
43+
transformer = style_text
44+
), NA)
45+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mtcars %>%
2+
mutate(
3+
x = 1
4+
) %>%
5+
if (FALSE) {
6+
mutate(., country = 2)
7+
} else .
8+
9+
# adding braces around . in else changes evaluation

tests/testthat/token_adding_removing/else-pipe-in_tree

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mtcars %>%
2+
mutate(
3+
x = 1
4+
) %>%
5+
if (FALSE) {
6+
mutate(., country = 2)
7+
} else .
8+
9+
# adding braces around . in else changes evaluation

0 commit comments

Comments
 (0)