Skip to content

Commit a99e7d4

Browse files
New consecutive_mutate_linter (#2305)
1 parent 0815b2a commit a99e7d4

12 files changed

+319
-4
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Collate:
8181
'condition_message_linter.R'
8282
'conjunct_test_linter.R'
8383
'consecutive_assertion_linter.R'
84+
'consecutive_mutate_linter.R'
8485
'cyclocomp_linter.R'
8586
'declared_functions.R'
8687
'deprecated.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export(comparison_negation_linter)
4040
export(condition_message_linter)
4141
export(conjunct_test_linter)
4242
export(consecutive_assertion_linter)
43+
export(consecutive_mutate_linter)
4344
export(consecutive_stopifnot_linter)
4445
export(cyclocomp_linter)
4546
export(default_linters)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* `which_grepl_linter()` for discouraging `which(grepl(ptn, x))` in favor of directly using `grep(ptn, x)` (part of #884, @MichaelChirico).
3434
* `list_comparison_linter()` for discouraging comparisons on the output of `lapply()`, e.g. `lapply(x, sum) > 10` (part of #884, @MichaelChirico).
3535
* `print_linter()` for discouraging usage of `print()` on string literals like `print("Reached here")` or `print(paste("Found", nrow(DF), "rows."))` (#1894, @MichaelChirico).
36+
* `consecutive_mutate_linter()` for encouraging consecutive calls to `dplyr::mutate()` to be combined (part of #884, @MichaelChirico).
3637
* `if_switch_linter()` for encouraging `switch()` over repeated `if`/`else` tests (part of #884, @MichaelChirico).
3738
* `nested_pipe_linter()` for discouraging pipes within pipes, e.g. `df1 %>% inner_join(df2 %>% select(a, b))` (part of #884, @MichaelChirico).
3839
* `nrow_subset_linter()` for discouraging usage like `nrow(subset(x, conditions))` in favor of something like `with(x, sum(conditions))` which doesn't require a full subset of `x` (part of #884, @MichaelChirico).

R/consecutive_mutate_linter.R

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#' Require consecutive calls to mutate() to be combined when possible
2+
#'
3+
#' `dplyr::mutate()` accepts any number of columns, so sequences like
4+
#' `DF %>% dplyr::mutate(..1) %>% dplyr::mutate(..2)` are redundant --
5+
#' they can always be expressed with a single call to `dplyr::mutate()`.
6+
#'
7+
#' An exception is for some SQL back-ends, where the translation logic may not be
8+
#' as sophisticated as that in the default `dplyr`, for example in
9+
#' `DF %>% mutate(a = a + 1) %>% mutate(b = a - 2)`.
10+
#'
11+
#' @param invalid_backends Character vector of packages providing dplyr backends
12+
#' which may not be compatible with combining `mutate()` calls in all cases.
13+
#' Defaults to `"dbplyr"` since not all SQL backends can handle re-using
14+
#' a variable defined in the same `mutate()` expression.
15+
#'
16+
#' @examples
17+
#' # will produce lints
18+
#' lint(
19+
#' text = "x %>% mutate(a = 1) %>% mutate(b = 2)",
20+
#' linters = consecutive_mutate_linter()
21+
#' )
22+
#'
23+
#' # okay
24+
#' lint(
25+
#' text = "x %>% mutate(a = 1, b = 2)",
26+
#' linters = consecutive_mutate_linter()
27+
#' )
28+
#'
29+
#' code <- "library(dbplyr)\nx %>% mutate(a = 1) %>% mutate(a = a + 1)"
30+
#' writeLines(code)
31+
#' lint(
32+
#' text = code,
33+
#' linters = consecutive_mutate_linter()
34+
#' )
35+
#'
36+
#' @evalRd rd_tags("consecutive_mutate_linter")
37+
#' @seealso [linters] for a complete list of linters available in lintr.
38+
#' @export
39+
consecutive_mutate_linter <- function(invalid_backends = "dbplyr") {
40+
attach_pkg_xpath <- glue("
41+
//SYMBOL_FUNCTION_CALL[text() = 'library' or text() = 'require']
42+
/parent::expr
43+
/following-sibling::expr
44+
/*[self::SYMBOL or self::STR_CONST]
45+
")
46+
47+
namespace_xpath <- glue("
48+
//SYMBOL_PACKAGE[{ xp_text_in_table(invalid_backends) }]
49+
|
50+
//COMMENT[
51+
contains(text(), '@import')
52+
and (
53+
{xp_or(sprintf(\"contains(text(), '%s')\", invalid_backends))}
54+
)
55+
]
56+
")
57+
58+
# match on the expr, not the SYMBOL_FUNCTION_CALL, to ensure
59+
# namespace-qualified calls only match if the namespaces do.
60+
# expr[2] needed in expr[1][expr[2]] to skip matches on pipelines
61+
# starting like mutate(DF, ...) %>% foo() %>% mutate().
62+
# similarly, expr[1][expr[call='mutate']] covers pipelines
63+
# starting like mutate(DF, ...) %>% mutate(...)
64+
mutate_cond <- xp_and(
65+
"expr/SYMBOL_FUNCTION_CALL[text() = 'mutate']",
66+
"not(SYMBOL_SUB[text() = '.keep' or text() = '.by'])"
67+
)
68+
xpath <- glue("
69+
(//PIPE | //SPECIAL[{ xp_text_in_table(magrittr_pipes) }])
70+
/preceding-sibling::expr[expr[2][{ mutate_cond }] or ({ mutate_cond })]
71+
/following-sibling::expr[{ mutate_cond }]
72+
")
73+
74+
Linter(function(source_expression) {
75+
# need the full file to also catch usages at the top level
76+
if (!is_lint_level(source_expression, "file")) {
77+
return(list())
78+
}
79+
80+
xml <- source_expression$full_xml_parsed_content
81+
82+
attach_str <- get_r_string(xml_find_all(xml, attach_pkg_xpath))
83+
if (any(invalid_backends %in% attach_str)) {
84+
return(list())
85+
}
86+
87+
namespace_expr <- xml_find_first(xml, namespace_xpath)
88+
if (!is.na(namespace_expr)) {
89+
return(list())
90+
}
91+
92+
bad_expr <- xml_find_all(xml, xpath)
93+
94+
xml_nodes_to_lints(
95+
bad_expr,
96+
source_expression = source_expression,
97+
lint_message = "Unify consecutive calls to mutate().",
98+
type = "warning"
99+
)
100+
})
101+
}

inst/lintr/linters.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ comparison_negation_linter,readability consistency
1414
condition_message_linter,best_practices consistency
1515
conjunct_test_linter,package_development best_practices readability configurable pkg_testthat
1616
consecutive_assertion_linter,style readability consistency
17+
consecutive_mutate_linter,consistency readability configurable efficiency
1718
consecutive_stopifnot_linter,style readability consistency deprecated
1819
cyclocomp_linter,style readability best_practices default configurable
1920
duplicate_argument_linter,correctness common_mistakes configurable

man/configurable_linters.Rd

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

man/consecutive_mutate_linter.Rd

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

man/consistency_linters.Rd

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

man/efficiency_linters.Rd

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

man/linters.Rd

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

man/readability_linters.Rd

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

0 commit comments

Comments
 (0)