diff --git a/NEWS.md b/NEWS.md index 8537b30..edc8bb3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ ## NEW FEATURES ## CHANGES +* `FunctionReporter` now detects function calls made in default argument values (e.g. `function(x, y = helper(x))`), for both regular functions and R6 methods. Previously only the function body was parsed, so these dependencies were missing from the network. (#304) ## BUGFIXES * Fixed runtime error when `FunctionReporter` parses a `do.call()` whose function argument is a string naming a non-syntactic function, such as `do.call("[<-", ...)`. (#335) diff --git a/R/FunctionReporter.R b/R/FunctionReporter.R index 0dc2b9d..29a6ce4 100644 --- a/R/FunctionReporter.R +++ b/R/FunctionReporter.R @@ -357,13 +357,10 @@ FunctionReporter <- R6::R6Class( , assertthat::is.string(fname) ) - # Get only the body of the function + # Parse the body and the default argument values of the function. # We will potentially miss calls if they are in attributes of the closure, # e.g., the way the decorators package implements decorators - f <- body(get(fname, envir = pkg_env)) - - # get the literal code of the function - f_vec <- .parse_function(f) + f_vec <- .parse_closure(get(fname, envir = pkg_env)) # Figure out which ones mix matches <- match( @@ -390,6 +387,16 @@ FunctionReporter <- R6::R6Class( return(edgeDT) } +# [description] parse a closure's body and default argument values into a +# character vector of symbols, so that calls made in default +# arguments (e.g. function(x = helper())) are also detected +.parse_closure <- function(fn) { + return(c( + .parse_function(body(fn)) + , .parse_function(formals(fn)) + )) +} + # [description] parse out a function's body into a character # vector separating the individual symbols .parse_function <- function (x) { @@ -544,12 +551,14 @@ FunctionReporter <- R6::R6Class( , pkg_env , pkg_functions ) { - # Get body of method - mbody <- body(get(class_name, envir = pkg_env)[[method_type]][[method_name]]) + method <- get(class_name, envir = pkg_env)[[method_type]][[method_name]] - # Parse into symbols + # Parse the body and the default argument values of the method into symbols mbodyDT <- data.table::data.table( - SYMBOL = unique(.parse_R6_expression(mbody)) + SYMBOL = unique(c( + .parse_R6_expression(body(method)) + , .parse_R6_expression(formals(method)) + )) ) # Match to R6 methods diff --git a/tests/testthat/test-FunctionReporter-class.R b/tests/testthat/test-FunctionReporter-class.R index 2e8eaee..ddd0d60 100644 --- a/tests/testthat/test-FunctionReporter-class.R +++ b/tests/testthat/test-FunctionReporter-class.R @@ -284,6 +284,38 @@ test_that(".parse_function handles do.call with a non-syntactic function name", expect_true("innerfunc1" %in% result) }) +test_that(".parse_closure finds calls made in default argument values", { + myfunc <- function(a, b = 1, c = defaultfunc(a), d = NULL) { + bodyfunc() + } + result <- pkgnet:::.parse_closure(myfunc) + expect_true(all(c("defaultfunc", "bodyfunc") %in% result)) +}) + +test_that(".called_by creates edges for calls made in default argument values", { + pkg_env <- new.env() + pkg_env$helper <- function(x) x + pkg_env$other <- function() NULL + pkg_env$caller <- function(obj, res = helper(obj)) { + other() + } + edgeDT <- pkgnet:::.called_by( + fname = "caller" + , all_functions = c("helper", "other", "caller") + , pkg_env = pkg_env + ) + expect_setequal(edgeDT[["TARGET"]], c("helper", "other")) + expect_true(all(edgeDT[["SOURCE"]] == "caller")) +}) + +test_that(".parse_R6_expression finds calls made in default argument values", { + myr6method <- function(x = regularfunc1(), y = self$public_method()) { + NULL + } + result <- pkgnet:::.parse_R6_expression(formals(myr6method)) + expect_true(all(c("regularfunc1", "self$public_method") %in% result)) +}) + test_that(".parse_R6_expression correctly parses expressions for symbols", { # Correctly parses body of function and finds all function symbols expect_true({