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

Handle dependencies with no exports #1509

Merged
merged 6 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -42,6 +42,7 @@

## Bug fixes

* `object_length_linter()` does not fail in case there are dependencies with no exports (e.g. data-only packages) (#1509, @IndrajeetPatil).
* `get_source_expressions()` no longer fails on R files that match a knitr pattern (#743, #879, #1406, @AshesITR).
* Parse error lints now appear with the linter name `"error"` instead of `NA` (#1405, @AshesITR).
Also, linting no longer runs if the `source_expressions` contain invalid string data that would cause error messages
Expand Down
28 changes: 18 additions & 10 deletions R/namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ namespace_imports <- function(path = find_package()) {
}

# this loads the namespaces, but is the easiest way to do it
# test package availablity to avoid failing out as in #1360
# test package availability to avoid failing out as in #1360
# typically, users are running this on their own package directories and thus
# will have the namespace dependencies installed, but we can't guarantee this.
safe_get_exports <- function(ns) {
# check package exists for both import(x) and importFrom(x, y) usages
if (!requireNamespace(ns[[1L]], quietly = TRUE)) {
return(empty_namespace_data())
}

# importFrom directives appear as list(ns, imported_funs)
if (length(ns) > 1L) {
return(data.frame(pkg = ns[[1L]], fun = ns[[2L]], stringsAsFactors = FALSE))
}

data.frame(pkg = ns, fun = getNamespaceExports(ns), stringsAsFactors = FALSE)
# relevant only if there are any exported objects
fun <- getNamespaceExports(ns)
if (length(fun) > 0L) {
data.frame(pkg = ns, fun = fun, stringsAsFactors = FALSE)
}
}

empty_namespace_data <- function() {
Expand All @@ -36,14 +41,17 @@ empty_namespace_data <- function() {
# filter namespace_imports() for S3 generics
# this loads all imported namespaces
imported_s3_generics <- function(ns_imports) {
is_generic <- vapply(
seq_len(nrow(ns_imports)),
function(i) {
fun_obj <- get(ns_imports$fun[i], envir = asNamespace(ns_imports$pkg[i]))
is.function(fun_obj) && is_s3_generic(fun_obj)
},
logical(1L)
)
# `NROW()` is preferred over `nrow()` because, for dependencies with no exports,
# `ns_import` argument can be `NULL`.
# `nrow(NULL)`: `NULL`, while `NROW(NULL)`: `0`
is_generic <- vapply(
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
seq_len(NROW(ns_imports)),
function(i) {
fun_obj <- get(ns_imports$fun[i], envir = asNamespace(ns_imports$pkg[i]))
is.function(fun_obj) && is_s3_generic(fun_obj)
},
logical(1L)
)

ns_imports[is_generic, ]
}
Expand Down