Skip to content

Commit 462b01c

Browse files
move pipe adder one level up so we have the context of the enclosing call and can check if it's substitute or not.
Note that this won't work if substitute itself is a placeholder (see issue)
1 parent 6023284 commit 462b01c

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# styler 1.6.2.9000 (Development version)
22

3+
* Piped function without brackets `substitute(x %>% y)` don't get `()` added
4+
anymore, as this can change outcome of the code (#876).
35
* Alignment detection respects stylerignore (#850).
46
* Add vignette on distributing style guide (#846, #861).
57
* Enable pre-commit.ci (#843).

R/rules-tokens.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ resolve_semicolon <- function(pd) {
1717
}
1818

1919
add_brackets_in_pipe <- function(pd) {
20+
if (!identical(pd$text[next_non_comment(pd, 0L)], "substitute")) {
21+
pd$child <- map(pd$child, add_brackets_in_pipe_child)
22+
}
23+
pd
24+
}
25+
26+
add_brackets_in_pipe_child <- function(pd) {
2027
is_pipe <- pd$token %in% c("SPECIAL-PIPE", "PIPE")
2128
Reduce(add_brackets_in_pipe_one, which(is_pipe), init = pd)
2229
}

tests/testthat/test-token_adding_removing.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ test_that("No braces are added if conditional statement is within pipe", {
4343
transformer = style_text
4444
), NA)
4545
})
46+
47+
test_that("No brace is added within `substitute()`", {
48+
expect_warning(test_collection("token_adding_removing", "substitute",
49+
transformer = style_text
50+
), NA)
51+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
expr <- substitute(airquality %>% FUN_EXPR, env = list(FUN_EXPR = call("FUN_head")))
2+
a %>%
3+
x

tests/testthat/token_adding_removing/substitute-in_tree

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
expr <- substitute(airquality %>% FUN_EXPR, env = list(FUN_EXPR = call("FUN_head")))
2+
a %>%
3+
x()

0 commit comments

Comments
 (0)