Skip to content

Commit

Permalink
find_globals() has been moved to xfun long ago
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed May 15, 2024
1 parent c49e590 commit 43896bc
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 21 deletions.
2 changes: 1 addition & 1 deletion R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ purge_cache = function(options) {

cache_globals = function(option, code) {
if (is.character(option)) option else {
(if (isFALSE(option)) find_symbols else find_globals)(code)
(if (isFALSE(option)) find_symbols else xfun:::find_globals)(code)
}
}

Expand Down
8 changes: 0 additions & 8 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ new_cache = function() {
list(purge = cache_purge, save = cache_save, load = cache_load, objects = cache_objects,
exists = cache_exists, output = cache_output, library = cache_library)
}
# analyze code and find out global variables
find_globals = function(code) {
fun = eval(parse_only(c('function(){', code, '}')))
setdiff(codetools::findGlobals(fun), known_globals)
}
known_globals = c(
'{', '[', '(', ':', '<-', '=', '+', '-', '*', '/', '%%', '%/%', '%*%', '%o%', '%in%'
)

# analyze code and find out all possible variables (not necessarily global variables)
find_symbols = function(code) {
Expand Down
12 changes: 0 additions & 12 deletions tests/testit/test-cache.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
library(testit)

assert('find_globals() identifies global variables', {
# nothing from outside environment
(find_globals('x=1') %==% character(0))
# qwer must be created outside somewhere
(find_globals('a=1; b=a; d=qwer') %==% 'qwer')
(find_globals('a=function(){f=2;g}') %==% 'g')
# y was assigned locally in z, but there is another y outside from nowhere
(find_globals('z=function(){y=1};y') %==% 'y')
# more complicated cases: operators, subscripts, ...
(find_globals(c('a=1%*%1%o%2 %in% d', 'b=d%%10+3%/%2-z[1:3]')) %==% c('d', 'z'))
})

assert('find_symbols() identifies all symbols', {
(find_symbols('x = x + 1; rnorm(1, std = z)') %==% c('x', 'rnorm', 'z'))
})
Expand Down

0 comments on commit 43896bc

Please sign in to comment.