Skip to content

Commit

Permalink
Deps scan: support import package
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Nov 4, 2024
1 parent 7f4c1fa commit 641214f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
5 changes: 3 additions & 2 deletions R/scan-deps-queries.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ q_library_0 <- function() {
rhs: (identifier) @fn-name
)
) @dep-code
(#any-of? @ns-name "base" "xfun" "pacman" "modules")
(#any-of? @ns-name "base" "xfun" "pacman" "modules" "import")
(#any-of? @fn-name
"library" "require" "loadNamespace" "requireNamespace"
"pkg_attach" "pkg_attach2"
"p_load"
"module" "import"))'
"module" "import"
"from" "here" "into"))'
), names = rep("q_library_0", 2))
}

Expand Down
40 changes: 37 additions & 3 deletions R/scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ re_r_dep <- paste0(collapse = "|", c(
"setClass", "setGeneric",
"pkg_attach",
"p_load",
"module"
"module",
"import"
))

scan_path_deps <- function(path) {
Expand Down Expand Up @@ -176,6 +177,13 @@ prot_xfun_pkg_attach2 <- function(...) { }
prot_pacman_p_load <- function(..., char, install, update, character.only) { }
prot_modules_import <- function(from, ..., attach = TRUE, where = parent.frame()) { }
prot_modules_module <- function(expr = {}, topEncl = NULL, envir = parent.frame()) { }
prot_import_from <- function(.from, ..., .character_only = FALSE) { }
prot_import_here <- function(.from, ..., .character_only = FALSE) { }
prot_import_into <- function(
.into, ..., .from, .library = NULL, .directory = ".", .all = NULL,
.except = character(), .chdir = TRUE, .character_only = FALSE,
.S3 = FALSE) {
}

safe_parse_pkg_from_call <- function(ns, fn, code) {
tryCatch(
Expand All @@ -195,7 +203,10 @@ parse_pkg_from_call <- function(ns, fn, code) {
"pkg_attach2" = prot_xfun_pkg_attach2,
"p_load" = prot_pacman_p_load,
"import" = prot_modules_import,
"module" = prot_modules_module
"module" = prot_modules_module,
"from" = prot_import_from,
"here" = prot_import_here,
"into" = prot_import_into
)
matched <- match.call(fun, expr, expand.dots = FALSE)
switch(fn,
Expand All @@ -210,7 +221,9 @@ parse_pkg_from_call <- function(ns, fn, code) {
"import" =
parse_pkg_from_call_modules_import(ns, fn, matched),
"module" =
parse_pkg_from_call_modules_module(ns, fn, matched)
parse_pkg_from_call_modules_module(ns, fn, matched),
"from" = , "here" = , "into" =
parse_pkg_from_call_import(ns, fn, matched)
)
}

Expand Down Expand Up @@ -287,6 +300,27 @@ parse_pkg_from_call_modules_module <- function(ns, fn, matched) {
NULL
}

parse_pkg_from_call_import <- function(ns, fn, matched) {
if (!is.na(ns) && ns != "import") return(NULL)
from <- matched[[".from"]]
if (is.symbol(from)) {
if (!identical(matched[[".character_only"]], TRUE)) {
from <- as.character(from)
}
}
if (!is.character(from) || length(from) != 1) {
return(NULL)
}

# '.from' can also be an R script; if it appears to be a path, then ignore it
# https://github.com/rstudio/renv/issues/1743
if (grepl("\\.[rR]$", from, perl = TRUE) && grepl("[/\\]", from)) {
return(NULL)
}

from
}

# -------------------------------------------------------------------------

scan_path_deps_do_rmd <- function(code, path) {
Expand Down

0 comments on commit 641214f

Please sign in to comment.