-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#' Block usage of return() in magrittr pipelines | ||
#' | ||
#' [return()] inside a magrittr pipeline does not actually execute `return()` | ||
#' like you'd expect: `\(x) { x %>% return(); FALSE }` will return `FALSE`! | ||
#' It will technically work "as expected" if this is the final statement | ||
#' in the function body, but such usage is misleading. Instead, assign | ||
#' the pipe outcome to a variable and return that. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = "function(x) x %>% return()", | ||
#' linters = pipe_return_linter() | ||
#' ) | ||
#' | ||
#' # okay | ||
#' code <- "function(x) {\n y <- sum(x)\n return(y)\n}" | ||
#' writeLines(code) | ||
#' lint( | ||
#' text = code, | ||
#' linters = pipe_return_linter() | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("pipe_return_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
pipe_return_linter <- make_linter_from_xpath( | ||
# NB: Native pipe disallows this at the parser level, so there's no need | ||
# to lint in valid R code. | ||
xpath = " | ||
//SPECIAL[text() = '%>%'] | ||
/following-sibling::expr[expr/SYMBOL_FUNCTION_CALL[text() = 'return']] | ||
", | ||
lint_message = paste( | ||
"Using return() as the final step of a magrittr pipeline", | ||
"is an anti-pattern. Instead, assign the output of the pipeline to", | ||
"a well-named object and return that." | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
test_that("pipe_return_linter skips allowed usages", { | ||
linter <- pipe_return_linter() | ||
|
||
normal_pipe_lines <- trim_some(" | ||
x %>% | ||
filter(str > 5) %>% | ||
summarize(str = sum(str)) | ||
") | ||
expect_lint(normal_pipe_lines, NULL, linter) | ||
|
||
normal_function_lines <- trim_some(" | ||
pipeline <- function(x) { | ||
out <- x %>% | ||
filter(str > 5) %>% | ||
summarize(str = sum(str)) | ||
return(out) | ||
} | ||
") | ||
expect_lint(normal_function_lines, NULL, linter) | ||
|
||
nested_return_lines <- trim_some(" | ||
pipeline <- function(x) { | ||
x_squared <- x %>% | ||
sapply(function(xi) { | ||
return(xi ** 2) | ||
}) | ||
return(x_squared) | ||
} | ||
") | ||
expect_lint(nested_return_lines, NULL, linter) | ||
}) | ||
|
||
test_that("pipe_return_linter blocks simple disallowed usages", { | ||
lines <- trim_some(" | ||
pipeline <- function(x) { | ||
out <- x %>% | ||
filter(str > 5) %>% | ||
summarize(str = sum(str)) %>% | ||
return() | ||
} | ||
") | ||
expect_lint( | ||
lines, | ||
rex::rex("Using return() as the final step of a magrittr pipeline"), | ||
pipe_return_linter() | ||
) | ||
}) |