-
Notifications
You must be signed in to change notification settings - Fork 187
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
Add potential sequence()
lints to seq_linter()
#2618
base: main
Are you sure you want to change the base?
Changes from 4 commits
32049af
332f861
405644e
999034c
75e56a4
69b4ad2
396a239
231abff
cfc17ee
072ef13
2a8d243
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 | ||
---|---|---|---|---|
|
@@ -26,6 +26,11 @@ | |||
#' linters = seq_linter() | ||||
#' ) | ||||
#' | ||||
#' lint( | ||||
#' text = "unlist(lapply(x, seq_len))", | ||||
#' linters = seq_linter() | ||||
#' ) | ||||
#' | ||||
#' # okay | ||||
#' lint( | ||||
#' text = "seq_along(x)", | ||||
|
@@ -42,6 +47,11 @@ | |||
#' linters = seq_linter() | ||||
#' ) | ||||
#' | ||||
#' lint( | ||||
#' text = "sapply(x, seq_len)", | ||||
IndrajeetPatil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
#' linters = seq_linter() | ||||
#' ) | ||||
#' | ||||
#' @evalRd rd_tags("seq_linter") | ||||
#' @seealso [linters] for a complete list of linters available in lintr. | ||||
#' @export | ||||
|
@@ -66,6 +76,15 @@ seq_linter <- function() { | |||
] | ||||
") | ||||
|
||||
map_funcs <- xp_text_in_table(c("sapply", "lapply", "map")) | ||||
sequence_xpath <- glue(" | ||||
//SYMBOL_FUNCTION_CALL[ { map_funcs } ] | ||||
/parent::expr/parent::expr[ | ||||
preceding-sibling::expr/SYMBOL_FUNCTION_CALL[text() = 'unlist'] | ||||
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. nit: I would try and keep the logic in one direction on the AST: check sapply, then check seq_len, then check unlist, roughly
Also, please switch to use the new lintr/R/condition_call_linter.R Line 83 in 6ed78c2
|
||||
and expr/SYMBOL[text() = 'seq_len'] | ||||
]" | ||||
) | ||||
|
||||
## The actual order of the nodes is document order | ||||
## In practice we need to handle length(x):1 | ||||
get_fun <- function(expr, n) { | ||||
|
@@ -113,6 +132,16 @@ seq_linter <- function() { | |||
) | ||||
) | ||||
|
||||
xml_nodes_to_lints(badx, source_expression, lint_message, type = "warning") | ||||
seq_lints <- xml_nodes_to_lints(badx, source_expression, lint_message, type = "warning") | ||||
|
||||
potential_sequence_calls <- xml_find_all(xml, sequence_xpath) | ||||
sequence_lints <- xml_nodes_to_lints( | ||||
potential_sequence_calls, | ||||
source_expression, | ||||
"Use sequence() to generate a concatenated sequence of seq_len().", | ||||
type = "warning" | ||||
) | ||||
|
||||
c(seq_lints, sequence_lints) | ||||
}) | ||||
} |
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 |
---|---|---|
|
@@ -123,6 +123,24 @@ test_that("reverse seq is ok", { | |
) | ||
}) | ||
|
||
test_that("finds potential sequence() replacements", { | ||
linter <- seq_linter() | ||
lint_msg <- rex::rex("Use sequence()") | ||
|
||
expect_lint( | ||
"unlist(lapply(x, seq_len))", | ||
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. The lint now only covers What about
(it would be nice to get a sense of how many hits these are getting before investing too much time -- and also feel free to earmark some of this as a TODO in a follow-up issue, though 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.
I don't think there is a direct equivalence:
I have edited the xpath to make sure this type of call doesn't lint. |
||
lint_msg, | ||
linter | ||
) | ||
|
||
# Even for prefixed purrr:: calls | ||
expect_lint( | ||
"unlist(purrr::map(x, seq_len))", | ||
lint_msg, | ||
linter | ||
) | ||
}) | ||
|
||
test_that("Message vectorization works for multiple lints", { | ||
linter <- seq_linter() | ||
|
||
|
@@ -173,6 +191,18 @@ test_that("Message vectorization works for multiple lints", { | |
), | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some("{ | ||
1:NROW(x) | ||
unlist(lapply(y, seq_len)) | ||
}"), | ||
list( | ||
list(rex::rex("seq_len(NROW(...))", anything, "1:NROW(...)"), line_number = 2L), | ||
list(rex::rex("sequence()"), line_number = 3L) | ||
), | ||
linter | ||
) | ||
}) | ||
|
||
test_that("Message recommends rev() correctly", { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
sequence()
is not a very well-known function (<500 calls on CRAN vs more than 80K forseq()
), here is a good opportunity to quickly introduce it:"to use the
sequence()
function, e.g.unlist(lapply(ints, seq))
"