Skip to content

Commit

Permalink
bugfix: names being matched in parent environments
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynorth committed Oct 2, 2020
1 parent 8e97733 commit 52e4667
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: roxyglobals
Title: Roxgen2 Global Variable Declarations
Version: 0.2.0
Version: 0.2.1
Authors@R: c(
person(given = "Anthony", family = "North", role = c("aut", "cre"), email = "anthony.jl.north@gmail.com"),
person(given = "Miles", family = "McBain", role = "ctb", email = "miles.mcbain@gmail.com", comment = c(ORCID = "0000-0003-2865-2548")))
Expand Down
20 changes: 15 additions & 5 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ extract_globals <- function(func) {
globals <- c()

enter <- function(type, name, expr, walker) {
undefined <- !exists(
name, envir = walker$globalenv, mode = ifelse(type == "function", type, "any")
)

if (undefined && (type == "variable" || type == "function" && name == c(":="))) {
undefined <- !is_defined(name, walker$globalenv)
if (undefined && (type == "variable" || type == "function" && name == ":=")) {
env$globals <- c(globals, name)
}
}

codetools::collectUsage(func, enterGlobal = enter)
unique(globals)
}

#' Is defined
#'
#' @param name a binding name
#' @param env an environment
#' @param inherit look for names in parent environments
#'
#' @keywords internal
#' @noRd
is_defined <- function(name, env, inherit = FALSE) {
exists(name, envir = env, inherits = inherit) ||
exists(name, envir = baseenv(), inherits = inherit)
}

0 comments on commit 52e4667

Please sign in to comment.