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

Add examples to documentation: Part-6 (o-p) #1664

Merged
merged 10 commits into from
Oct 11, 2022
Prev Previous commit
Next Next commit
Examples for outer_negation_linter() and object_usage_linter()
  • Loading branch information
IndrajeetPatil committed Oct 10, 2022
commit 5e4b7195f5cfbfe11971f1a0003a809786d3074f
17 changes: 17 additions & 0 deletions R/object_usage_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
#' will be skipped. This argument will be passed to `skipWith` argument of
#' `codetools::checkUsage()`.
#'
#' @examples
#' # will produce lints
#' lint(
#' text = "foo <- function() { x <- 1 }",
#' linters = object_usage_linter()
#' )
#'
#' # okay
#' lint(
#' text = "foo <- function(x) { x <- 1 }",
#' linters = object_usage_linter()
#' )
#'
#' lint(
#' text = "foo <- function() { x <- 1; return(x) }",
#' linters = object_usage_linter()
#' )
#' @evalRd rd_linters("package_development")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
Expand Down
23 changes: 23 additions & 0 deletions R/outer_negation_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@
#' `all(!x)` and `!any(x)`. Negating after aggregation only requires inverting
#' one logical value, and is typically more readable.
#'
#' @examples
#' # will produce lints
#' lint(
#' text = "all(!x)",
#' linters = outer_negation_linter()
#' )
#'
#' lint(
#' text = "any(!x)",
#' linters = outer_negation_linter()
#' )
#'
#' # okay
#' lint(
#' text = "!any(x)",
#' linters = outer_negation_linter()
#' )
#'
#' lint(
#' text = "!all(x)",
#' linters = outer_negation_linter()
#' )
#'
#' @evalRd rd_tags("outer_negation_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
Expand Down
18 changes: 18 additions & 0 deletions man/object_usage_linter.Rd

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

24 changes: 24 additions & 0 deletions man/outer_negation_linter.Rd

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

22 changes: 12 additions & 10 deletions tests/testthat/test-outer_negation_linter.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
test_that("outer_negation_linter skips allowed usages", {
expect_lint("x <- any(y)", NULL, outer_negation_linter())
expect_lint("y <- all(z)", NULL, outer_negation_linter())
linter <- outer_negation_linter()

expect_lint("x <- any(y)", NULL, linter)
expect_lint("y <- all(z)", NULL, linter)

# extended usage of any is not covered
expect_lint("any(!a & b)", NULL, outer_negation_linter())
expect_lint("all(a | !b)", NULL, outer_negation_linter())
expect_lint("any(!a & b)", NULL, linter)
expect_lint("all(a | !b)", NULL, linter)

expect_lint("any(a, b)", NULL, outer_negation_linter())
expect_lint("all(b, c)", NULL, outer_negation_linter())
expect_lint("any(!a, b)", NULL, outer_negation_linter())
expect_lint("all(a, !b)", NULL, outer_negation_linter())
expect_lint("any(a, !b, na.rm = TRUE)", NULL, outer_negation_linter())
expect_lint("any(a, b)", NULL, linter)
expect_lint("all(b, c)", NULL, linter)
expect_lint("any(!a, b)", NULL, linter)
expect_lint("all(a, !b)", NULL, linter)
expect_lint("any(a, !b, na.rm = TRUE)", NULL, linter)
# ditto when na.rm is passed quoted
expect_lint("any(a, !b, 'na.rm' = TRUE)", NULL, outer_negation_linter())
expect_lint("any(a, !b, 'na.rm' = TRUE)", NULL, linter)
})

test_that("outer_negation_linter blocks simple disallowed usages", {
Expand Down