From d0023d4e8ceeb5b79be0521bc8a40052373191f0 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Wed, 26 Oct 2022 08:14:20 +0200 Subject: [PATCH] `namespace_linter()` handles backticked operators (#1753) --- NEWS.md | 2 ++ R/namespace_linter.R | 3 +++ tests/testthat/test-namespace_linter.R | 17 +++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/NEWS.md b/NEWS.md index f3f67f0b9..61a33fadb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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")`. diff --git a/R/namespace_linter.R b/R/namespace_linter.R index 49e4ce3e7..2d2d5c4c6 100644 --- a/R/namespace_linter.R +++ b/R/namespace_linter.R @@ -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( diff --git a/tests/testthat/test-namespace_linter.R b/tests/testthat/test-namespace_linter.R index fba9d7624..fd3686fc8 100644 --- a/tests/testthat/test-namespace_linter.R +++ b/tests/testthat/test-namespace_linter.R @@ -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()