Skip to content
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

Further improvements for lengths_linter() #1749

Merged
merged 9 commits into from
Oct 27, 2022
3 changes: 2 additions & 1 deletion R/lengths_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ 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']]]
/parent::expr
/parent::expr[expr/SYMBOL[text() = 'length']]
")

Linter(function(source_expression) {
Expand Down
59 changes: 36 additions & 23 deletions tests/testthat/test-lengths_linter.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
test_that("lengths_linter skips allowed usages", {
linter <- lengths_linter()

expect_lint("length(x)", NULL, linter)
expect_lint("function(x) length(x) + 1L", NULL, linter)
expect_lint("vapply(x, fun, integer(length(y)))", NULL, linter)
expect_lint("sapply(x, sqrt, simplify = length(x))", NULL, linter)

# TODO(#1570): also throw a lint here, and for map(x, length)
expect_lint("lapply(x, length)", NULL, lengths_linter())
expect_lint("lapply(x, length)", NULL, 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()
)
linter <- lengths_linter()
lint_msg <- rex::rex("Use lengths() to find the length of each element in a list.")

expect_lint("sapply(x, length)", lint_msg, linter)
expect_lint("sapply(x, FUN = length)", lint_msg, linter)
expect_lint("sapply(FUN = length, x)", lint_msg, linter)

expect_lint("vapply(x, length, integer(1L))", lint_msg, linter)
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
})

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()
)
linter <- lengths_linter()
lint_msg <- rex::rex("Use lengths() to find the length of each element in a list.")

expect_lint("purrr::map_dbl(x, length)", lint_msg, linter)
expect_lint("map_dbl(x, .f = length)", lint_msg, linter)
expect_lint("map_dbl(.f = length, x)", lint_msg, linter)
expect_lint("map_int(x, length)", lint_msg, linter)
})

test_that("lengths_linter blocks simple disallowed usages with pipes", {
skip_if_not_r_version("4.1.0")

linter <- lengths_linter()
lint_msg <- rex::rex("Use lengths() to find the length of each element in a list.")

expect_lint("x |> sapply(length)", lint_msg, linter)
expect_lint("x %>% sapply(length)", lint_msg, linter)

expect_lint("x |> map_int(length)", lint_msg, linter)
expect_lint("x %>% map_int(length)", lint_msg, linter)
})