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 1 commit
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
46 changes: 32 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,12 @@
#' linters = conjunct_test_linter(allow_named_stopifnot = FALSE)
#' )
#'
#' lint(
#' text = "dplyr::filter(mtcars,
#' mpg > 20 & vs == 0)",
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
#' linters = conjunct_test_linter()
#' )
#'
#' # okay
#' lint(
#' text = "expect_true(x || (y && z))",
Expand All @@ -43,10 +50,17 @@
#' 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 +117,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
})
}
24 changes: 19 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.

Loading