Skip to content

Commit

Permalink
namespace_linter() handles backticked operators (#1753)
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil authored Oct 26, 2022
1 parent 1ac48e9 commit d0023d4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

* `object_usage_linter()` no longer silently ignores usage warnings that don't contain a quoted name (#1714, @AshesITR)

* `namespace_linter()` correctly recognizes backticked operators to be exported from respectives namespaces (like `` rlang::`%||%` ``) (#1752, @IndrajeetPatil)

## Changes to defaults

* Set the default for the `except` argument in `duplicate_argument_linter()` to `c("mutate", "transmute")`.
Expand Down
3 changes: 3 additions & 0 deletions R/namespace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ build_ns_get_int_lints <- function(packages, symbols, symbol_nodes, namespaces,
build_ns_get_lints <- function(packages, symbols, symbol_nodes, namespaces, source_expression) {
lints <- list()

# strip backticked symbols; `%>%` is the same as %>% (#1752).
symbols <- gsub("^`(.*)`$", "\\1", symbols)

## Case 4: foo is not an export in pkg::foo

unexported <- !vapply(
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-namespace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ test_that("namespace_linter respects check_exports and check_nonexports argument
expect_lint("stats:::ssd(c(1,2,3))", NULL, namespace_linter(check_exports = FALSE, check_nonexports = FALSE))
})

test_that("namespace_linter can work with backticked symbols", {
skip_if_not_installed("rlang")
linter <- namespace_linter()

expect_lint("rlang::`%||%`", NULL, linter)
expect_lint("rlang::`%||%`()", NULL, linter)

expect_lint("rlang::'%||%'", NULL, linter)
expect_lint("rlang::'%||%'()", NULL, linter)
expect_lint('rlang::"%||%"', NULL, linter)
expect_lint('rlang::"%||%"()', NULL, linter)

expect_lint("rlang::`%>%`", "'%>%' is not exported from {rlang}.", linter)
expect_lint("rlang::'%>%'()", "'%>%' is not exported from {rlang}.", linter)
expect_lint('rlang::"%>%"()', "'%>%' is not exported from {rlang}.", linter)
})

test_that("namespace_linter blocks disallowed usages", {
linter <- namespace_linter()

Expand Down

0 comments on commit d0023d4

Please sign in to comment.