-
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.
* New lengths_linter * tag follow-up issue in comment * fix doc issue by merging main & redoc * merge & redoc Co-authored-by: Indrajeet Patil <[email protected]>
- Loading branch information
1 parent
82ca559
commit 554a11a
Showing
11 changed files
with
95 additions
and
3 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,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']]] | ||
") | ||
|
||
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" | ||
) | ||
}) | ||
} |
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.
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,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()) | ||
}) | ||
|
||
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() | ||
) | ||
}) |