Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -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)
Expand Down
27 changes: 18 additions & 9 deletions R/FunctionReporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-FunctionReporter-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading