-
Notifications
You must be signed in to change notification settings - Fork 186
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
New lengths_linter #1568
New lengths_linter #1568
Changes from all commits
77f2c38
4b24a4f
2283a80
b4d2777
26bfbef
b1fb1d2
6a94c3f
6fffe18
f5a7590
f96f720
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,33 @@ | ||
#' Require usage of `lengths()` where possible | ||
#' | ||
#' [lengths()] is a function that was added to base R in version 3.2.0 to | ||
#' get the length of each element of a list. It is equivalent to | ||
#' `sapply(x, length)`, but faster and more readable. | ||
#' | ||
#' @evalRd rd_tags("lengths_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
lengths_linter <- function() { | ||
loop_funs <- c("sapply", "vapply", "map_int", "map_dbl") | ||
xpath <- glue::glue(" | ||
//SYMBOL_FUNCTION_CALL[ {xp_text_in_table(loop_funs)} ] | ||
/parent::expr/parent::expr[expr[position() = 3 and SYMBOL[text() = 'length']]] | ||
IndrajeetPatil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
") | ||
|
||
Linter(function(source_expression) { | ||
if (!is_lint_level(source_expression, "expression")) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_expression$xml_parsed_content | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
source_expression = source_expression, | ||
lint_message = "Use lengths() to find the length of each element in a list.", | ||
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
test_that("lengths_linter skips allowed usages", { | ||
# TODO(#1570): also throw a lint here, and for map(x, length) | ||
expect_lint("lapply(x, length)", NULL, lengths_linter()) | ||
MichaelChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
test_that("lengths_linter blocks simple disallowed base usages", { | ||
expect_lint( | ||
"sapply(x, length)", | ||
rex::rex("Use lengths() to find the length of each element in a list."), | ||
lengths_linter() | ||
) | ||
|
||
expect_lint( | ||
"vapply(x, length, integer(1L))", | ||
rex::rex("Use lengths() to find the length of each element in a list."), | ||
lengths_linter() | ||
) | ||
}) | ||
|
||
test_that("lengths_linter blocks simple disallowed purrr usages", { | ||
expect_lint( | ||
"purrr::map_dbl(x, length)", | ||
rex::rex("Use lengths() to find the length of each element in a list."), | ||
lengths_linter() | ||
) | ||
|
||
expect_lint( | ||
"map_int(x, length)", | ||
rex::rex("Use lengths() to find the length of each element in a list."), | ||
lengths_linter() | ||
) | ||
}) |
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.
Is this sentence complete?
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.
no need for a complete sentence IMO :)