Skip to content

Commit

Permalink
Merge pull request #876 from lorenzwalthert/issue-873
Browse files Browse the repository at this point in the history
- Don't add `()` within substitute piped calls (#873).
  • Loading branch information
lorenzwalthert authored Dec 17, 2021
2 parents 52cef76 + 462b01c commit c2e25f9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# styler 1.6.2.9000 (Development version)

* Piped function without brackets `substitute(x %>% y)` don't get `()` added
anymore, as this can change outcome of the code (#876).
* Alignment detection respects stylerignore (#850).
* Add vignette on distributing style guide (#846, #861).
* Enable pre-commit.ci (#843).
Expand Down
7 changes: 7 additions & 0 deletions R/rules-tokens.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ resolve_semicolon <- function(pd) {
}

add_brackets_in_pipe <- function(pd) {
if (!identical(pd$text[next_non_comment(pd, 0L)], "substitute")) {
pd$child <- map(pd$child, add_brackets_in_pipe_child)
}
pd
}

add_brackets_in_pipe_child <- function(pd) {
is_pipe <- pd$token %in% c("SPECIAL-PIPE", "PIPE")
Reduce(add_brackets_in_pipe_one, which(is_pipe), init = pd)
}
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-token_adding_removing.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ test_that("No braces are added if conditional statement is within pipe", {
transformer = style_text
), NA)
})

test_that("No brace is added within `substitute()`", {
expect_warning(test_collection("token_adding_removing", "substitute",
transformer = style_text
), NA)
})
3 changes: 3 additions & 0 deletions tests/testthat/token_adding_removing/substitute-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
expr <- substitute(airquality %>% FUN_EXPR, env = list(FUN_EXPR = call("FUN_head")))
a %>%
x
39 changes: 39 additions & 0 deletions tests/testthat/token_adding_removing/substitute-in_tree

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/token_adding_removing/substitute-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
expr <- substitute(airquality %>% FUN_EXPR, env = list(FUN_EXPR = call("FUN_head")))
a %>%
x()

0 comments on commit c2e25f9

Please sign in to comment.