-
Notifications
You must be signed in to change notification settings - Fork 188
New consecutive_mutate_linter #2305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
712a10e
3907fbb
95b0fc4
e9e5eda
b26f492
4f1337f
57fa304
f0c05c8
6ef9f19
650b96b
bf4e1e7
af41255
c155981
dc528d7
3bbf6a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#' Force consecutive calls to mutate() into just one when possible | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wording could be more consistent to other linters There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PTAL |
||
#' | ||
#' `dplyr::mutate()` accepts any number of columns, so sequences like | ||
#' `DF %>% dplyr::mutate(..1) %>% dplyr::mutate(..2)` are redundant -- | ||
#' they can always be expressed with a single call to `dplyr::mutate()`. | ||
#' | ||
#' An exception is for some SQL back-ends, where the translation logic may not be | ||
#' as sophisticated as that in the default `dplyr`, for example in | ||
#' `DF %>% mutate(a = a + 1) %>% mutate(b = a - 2)`. | ||
#' | ||
#' @param invalid_backends Character vector of packages providing dplyr backends | ||
#' which may not be compatible with combining `mutate()` calls in all cases. | ||
#' Defaults to `"dbplyr"` since not all SQL backends can handle re-using | ||
#' a variable defined in the same `mutate()` expression. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = "x %>% mutate(a = 1) %>% mutate(b = 2)", | ||
#' linters = consecutive_mutate_linter() | ||
#' ) | ||
#' | ||
#' # okay | ||
MichaelChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' code <- "library(dbplyr)\nx %>% mutate(a = 1) %>% mutate(a = a + 1)" | ||
#' writeLines(code) | ||
#' lint( | ||
#' text = code, | ||
#' linters = consecutive_mutate_linter() | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("consecutive_mutate_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
consecutive_mutate_linter <- function(invalid_backends = "dbplyr") { | ||
attach_pkg_xpath <- glue(" | ||
//SYMBOL_FUNCTION_CALL[text() = 'library' or text() = 'require'] | ||
/parent::expr | ||
/following-sibling::expr | ||
/*[self::SYMBOL or self::STR_CONST] | ||
") | ||
|
||
namespace_xpath <- glue(" | ||
//SYMBOL_PACKAGE[{ xp_text_in_table(invalid_backends) }] | ||
| | ||
//COMMENT[ | ||
contains(text(), '@import') | ||
and ( | ||
{xp_or(sprintf(\"contains(text(), '%s')\", invalid_backends))} | ||
) | ||
] | ||
") | ||
|
||
# match on the expr, not the SYMBOL_FUNCTION_CALL, to ensure | ||
# namespace-qualified calls only match if the namespaces do. | ||
# expr[2] needed in expr[1][expr[2]] to skip matches on pipelines | ||
# starting like mutate(DF, ...) %>% foo() %>% mutate(). | ||
# similarly, expr[1][expr[call='mutate']] covers pipelines | ||
# starting like mutate(DF, ...) %>% mutate(...) | ||
mutate_cond <- xp_and( | ||
"expr/SYMBOL_FUNCTION_CALL[text() = 'mutate']", | ||
"not(SYMBOL_SUB[text() = '.keep' or text() = '.by'])" | ||
) | ||
xpath <- glue(" | ||
(//PIPE | //SPECIAL[{ xp_text_in_table(magrittr_pipes) }]) | ||
/preceding-sibling::expr[expr[2][{ mutate_cond }] or ({ mutate_cond })] | ||
/following-sibling::expr[{ mutate_cond }] | ||
") | ||
|
||
Linter(function(source_expression) { | ||
# need the full file to also catch usages at the top level | ||
if (!is_lint_level(source_expression, "file")) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_expression$full_xml_parsed_content | ||
|
||
attach_str <- get_r_string(xml_find_all(xml, attach_pkg_xpath)) | ||
if (any(invalid_backends %in% attach_str)) { | ||
return(list()) | ||
} | ||
|
||
namespace_expr <- xml_find_first(xml, namespace_xpath) | ||
if (!is.na(namespace_expr)) { | ||
return(list()) | ||
} | ||
|
||
bad_expr <- xml_find_all(xml, xpath) | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
source_expression = source_expression, | ||
lint_message = "Unify consecutive calls to mutate().", | ||
type = "warning" | ||
) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
test_that("consecutive_mutate_linter skips allowed usages", { | ||
linter <- consecutive_mutate_linter() | ||
|
||
expect_lint("DF %>% mutate(x = 1)", NULL, linter) | ||
|
||
# intervening expression | ||
expect_lint("DF %>% mutate(x = 1) %>% filter(x > 2) %>% mutate(y = 2)", NULL, linter) | ||
|
||
# pipeline starts with mutate() | ||
expect_lint("mutate(DF, x = 1) %>% arrange(y) %>% mutate(z = 2)", NULL, linter) | ||
|
||
# new dplyr: .keep and .by arguments are ignored | ||
expect_lint("DF %>% mutate(a = 1) %>% mutate(a = a / sum(a), .by = b)", NULL, linter) | ||
expect_lint("DF %>% mutate(a = 1) %>% mutate(a = b, .keep = 'none')", NULL, linter) | ||
expect_lint("DF %>% mutate(a = a / sum(a), .by = b) %>% mutate(c = 1)", NULL, linter) | ||
expect_lint("DF %>% mutate(a = 1, .keep = 'none') %>% mutate(a = a + 1)", NULL, linter) | ||
}) | ||
|
||
patrick::with_parameters_test_that( | ||
"consecutive_mutate_linter skips files loading SQL backends", | ||
{ | ||
linter <- consecutive_mutate_linter(invalid_backends = backend) | ||
|
||
expect_lint( | ||
trim_some(glue::glue(" | ||
library({backend}) | ||
DF %>% mutate(a = a + 1) %>% mutate(b = a - 2) | ||
")), | ||
NULL, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(glue::glue(" | ||
require('{backend}') | ||
DF %>% mutate(a = a + 1) %>% mutate(b = a - 2) | ||
")), | ||
NULL, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(glue(" | ||
conn %>% | ||
tbl({backend}::sql('SELECT 1 AS x')) %>% | ||
mutate(a = x + 1) %>% | ||
mutate(b = a + 1) | ||
")), | ||
NULL, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(glue(" | ||
conn %>% | ||
tbl({backend}:::sql('SELECT 1 AS x')) %>% | ||
mutate(a = x + 1) %>% | ||
mutate(b = a + 1) | ||
")), | ||
NULL, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(glue(" | ||
#' @import {backend} | ||
NULL | ||
|
||
DF %>% mutate(a = a + 1) %>% mutate(b = a - 2) | ||
")), | ||
NULL, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(glue(" | ||
#' @importFrom {backend} sql | ||
NULL | ||
|
||
conn %>% | ||
tbl(sql('SELECT 1 AS x')) %>% | ||
mutate(a = x + 1) %>% | ||
mutate(b = a + 1) | ||
")), | ||
NULL, | ||
linter | ||
) | ||
}, | ||
.test_name = c("dbplyr", "custom.backend"), | ||
backend = c("dbplyr", "custom.backend") | ||
) | ||
|
||
test_that("consecutive_mutate_linter blocks simple disallowed usages", { | ||
linter <- consecutive_mutate_linter() | ||
lint_msg <- rex::rex("Unify consecutive calls to mutate().") | ||
|
||
# one test of inline usage | ||
expect_lint("DF %>% mutate(a = 1) %>% mutate(b = 2)", lint_msg, linter) | ||
|
||
expect_lint( | ||
trim_some(" | ||
DF %>% | ||
mutate(a = 1) %>% | ||
mutate(b = 2) | ||
"), | ||
lint_msg, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
DF %>% | ||
dplyr::mutate(a = 1) %>% | ||
dplyr::mutate(b = 2) | ||
"), | ||
lint_msg, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
DF %>% | ||
mutate(a = 1) %>% | ||
# a comment on b | ||
mutate(b = 2) | ||
"), | ||
lint_msg, | ||
linter | ||
) | ||
|
||
# mutate to open pipeline followed by mutate | ||
expect_lint("mutate(DF, x = 1) %>% mutate(x = 2)", lint_msg, linter) | ||
}) | ||
|
||
test_that("'parallel' calls are not linted", { | ||
linter <- consecutive_mutate_linter() | ||
|
||
expect_lint("foo(mutate(DF1, x = 1), mutate(DF2, y = 2))", NULL, linter) | ||
|
||
expect_lint("foo(DF1 %>% mutate(x = 1), DF2 %>% mutate(y = 2))", NULL, linter) | ||
|
||
expect_lint("DF1 %>% mutate(x = 1) %>% inner_join(DF2 %>% mutate(y = 2))", NULL, linter) | ||
}) | ||
|
||
test_that("native pipe is linted", { | ||
skip_if_not_r_version("4.1.0") | ||
|
||
linter <- consecutive_mutate_linter() | ||
lint_msg <- rex::rex("Unify consecutive calls to mutate().") | ||
|
||
expect_lint("DF |> mutate(a = 1) |> mutate(b = 2)", lint_msg, linter) | ||
# Ditto mixed pipes | ||
expect_lint("DF %>% mutate(a = 1) |> mutate(b = 2)", lint_msg, linter) | ||
}) |
Uh oh!
There was an error while loading. Please reload this page.