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

Allow to opt out of linting filter() in conjunct_test_linter #2110

Merged
merged 6 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* `seq_linter()` recommends `rev()` in the lint message for lints like `nrow(x):1` (#1542, @MichaelChirico).
* New `xp_call_name()` helper to facilitate writing custom linters (#2023, @MichaelChirico). This helper converts a matched XPath to the R function to which it corresponds. This is useful for including the "offending" function in the lint's message.
* `function_argument_linter()` detects usage of `missing()` for the linted argument (#1546, @MichaelChirico). The simplest fix for `function_argument_linter()` lints is typically to set that argument to `NULL` by default, in which case it's usually preferable to update function logic checking `missing()` to check `is.null()` instead.
* Allow to opt out of linting `filter()` expressions in `conjunct_test_linter` via new `allow_filter` argument which defaults to `FALSE` (#2110, @salim-b)
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved

### New linters

Expand Down
50 changes: 36 additions & 14 deletions R/conjunct_test_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#'
#' Similar reasoning applies to `&&` usage inside [stopifnot()] and `assertthat::assert_that()` calls.
#'
#' Relatedly, `dplyr::filter(DF, A & B)` is the same as `dplyr::filter(DF, A, B)`, but the
#' latter will be more readable / easier to format for long conditions. Note that this linter
#' assumes usages of `filter()` are `dplyr::filter()`; if you're using another function named `filter()`,
#' e.g. [stats::filter()], please namespace-qualify it to avoid false positives.
#' Relatedly, `dplyr::filter(DF, A & B)` is the same as `dplyr::filter(DF, A, B)`, but the latter will be more readable
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
#' / easier to format for long conditions. Note that this linter assumes usages of `filter()` are `dplyr::filter()`;
#' if you're using another function named `filter()`, e.g. [stats::filter()], please namespace-qualify it to avoid
#' false positives. You can omit linting `filter()` expressions altogether via `allow_filter = TRUE`.
#'
#' @param allow_named_stopifnot Logical, `TRUE` by default. If `FALSE`, "named" calls to `stopifnot()`,
#' available since R 4.0.0 to provide helpful messages for test failures, are also linted.
#' @param allow_filter Logical, `FALSE` by default. If `TRUE`, `filter()` expressions are not linted.
#'
#' @examples
#' # will produce lints
Expand All @@ -32,6 +33,14 @@
#' linters = conjunct_test_linter(allow_named_stopifnot = FALSE)
#' )
#'
#' lint(
#' text = "dplyr::filter(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the toil -- would you mind putting this into an inline expression? it'll be more consistent with examples from other linters. Typically when examples run to >1 line we write the examples little differently (search for writeLines()), but I don't think it's necessary in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this? 88734f3

#' mtcars,
#' mpg > 20 & vs == 0
#' )",
#' linters = conjunct_test_linter()
#' )
#'
#' # okay
#' lint(
#' text = "expect_true(x || (y && z))",
Expand All @@ -43,10 +52,19 @@
#' linters = conjunct_test_linter(allow_named_stopifnot = TRUE)
#' )
#'
#' lint(
#' text = "dplyr::filter(
#' mtcars,
#' mpg > 20 & vs == 0
#' )",
#' linters = conjunct_test_linter(allow_filter = TRUE)
#' )
#'
#' @evalRd rd_tags("conjunct_test_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
conjunct_test_linter <- function(allow_named_stopifnot = TRUE) {
conjunct_test_linter <- function(allow_named_stopifnot = TRUE,
allow_filter = FALSE) {
expect_true_assert_that_xpath <- "
//SYMBOL_FUNCTION_CALL[text() = 'expect_true' or text() = 'assert_that']
/parent::expr
Expand Down Expand Up @@ -103,22 +121,26 @@ conjunct_test_linter <- function(allow_named_stopifnot = TRUE) {
sprintf(as.character(replacement_fmt), matched_fun),
"The latter will produce better error messages in the case of failure."
)
test_lints <- xml_nodes_to_lints(
lints <- xml_nodes_to_lints(
test_expr,
source_expression = source_expression,
lint_message = lint_message,
type = "warning"
)

filter_expr <- xml_find_all(xml, filter_xpath)
if (!allow_filter) {
filter_expr <- xml_find_all(xml, filter_xpath)

filter_lints <- xml_nodes_to_lints(
filter_expr,
source_expression = source_expression,
lint_message = "Use dplyr::filter(DF, A, B) instead of dplyr::filter(DF, A & B).",
type = "warning"
)
filter_lints <- xml_nodes_to_lints(
filter_expr,
source_expression = source_expression,
lint_message = "Use dplyr::filter(DF, A, B) instead of dplyr::filter(DF, A & B).",
type = "warning"
)

lints <- c(lints, filter_lints)
}

c(test_lints, filter_lints)
lints
})
}
28 changes: 23 additions & 5 deletions man/conjunct_test_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/test-conjunct_test_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ test_that("conjunct_test_linter blocks simple disallowed usages", {
expect_lint("DF %>% dplyr::filter(A & B)", lint_msg, linter)
})

test_that("conjunct_test_linter respects its allow_filter argument", {
linter <- conjunct_test_linter(allow_filter = TRUE)

expect_lint("dplyr::filter(DF, A & B)", NULL, linter)
expect_lint("dplyr::filter(DF, A & B & C)", NULL, linter)
expect_lint("DF %>% dplyr::filter(A & B)", NULL, linter)
})

test_that("filter() is assumed to be dplyr::filter() by default, unless o/w specified", {
linter <- conjunct_test_linter()

Expand Down