Skip to content

Commit 516fcf6

Browse files
Merge pull request #816 from lorenzwalthert/issue-815
- Avoid adding braces to conditional expression in pipe (#816).
2 parents 1aaae90 + 9a98927 commit 516fcf6

File tree

10 files changed

+72
-21
lines changed

10 files changed

+72
-21
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
* in function calls, code after `= #\n` is indented correctly (#814).
5962
* styler won't format code chunks with explicit `tidy = FALSE` in an Rmd or Rnw

R/nest.R

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,6 @@ add_attributes_caching <- function(pd_flat, transformers, more_specs) {
302302
pd_flat
303303
}
304304

305-
#' @describeIn add_token_terminal Removes column `terimnal_token_before`. Might
306-
#' be used to prevent the use of invalidated information, e.g. if tokens were
307-
#' added to the nested parse table.
308-
#' @keywords internal
309-
remove_terminal_token_before_and_after <- function(pd_flat) {
310-
pd_flat$token_before <- NULL
311-
pd_flat$token_after <- NULL
312-
pd_flat
313-
}
314-
315305
#' Helper for setting spaces
316306
#'
317307
#' @param spaces_after_prefix An integer vector with the number of spaces

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))

R/style-guides.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ tidyverse_style <- function(scope = "tokens",
168168
force_assignment_op = force_assignment_op,
169169
resolve_semicolon = resolve_semicolon,
170170
add_brackets_in_pipe = add_brackets_in_pipe,
171-
remove_terminal_token_before_and_after = remove_terminal_token_before_and_after,
172171
wrap_if_else_while_for_fun_multi_line_in_curly =
173172
if (strict) wrap_if_else_while_for_fun_multi_line_in_curly
174173
)

man/add_token_terminal.Rd

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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+
})

tests/testthat/test-transformers-drop.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ test_that("tidyverse transformers are correctly dropped", {
8585

8686
names_tokens <- c(
8787
"fix_quotes",
88-
if (getRversion() < 3.6) "force_assignment_op",
89-
"remove_terminal_token_before_and_after"
88+
if (getRversion() < 3.6) "force_assignment_op"
9089
)
9190
expect_setequal(names(t_fun$token), names_tokens)
9291
})
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)