Skip to content

Commit a8016bc

Browse files
committed
simplify the check_names condition in get_code_dependency
1 parent 839fca6 commit a8016bc

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

R/utils-get_code_dependency.R

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,19 @@ get_code_dependency <- function(code, names, check_names = TRUE) {
3333
return(code)
3434
}
3535

36+
graph <- lapply(code, attr, "dependency")
37+
3638
if (check_names) {
37-
# Detect if names are actually in code.
38-
parsed_code <- parse(text = trimws(code), keep.source = TRUE)
39-
pd <- normalize_pd(utils::getParseData(parsed_code))
40-
symbols <- pd[pd$token == "SYMBOL", "text"]
41-
if (any(pd$text == "assign")) {
42-
calls_pd <- extract_calls(pd)
43-
assign_calls <- Filter(function(call) find_call(call, "assign"), calls_pd)
44-
ass_str <- unlist(lapply(assign_calls, function(call) call[call$token == "STR_CONST", "text"]))
45-
ass_str <- gsub("^['\"]|['\"]$", "", ass_str)
46-
symbols <- c(ass_str, symbols)
47-
}
39+
symbols <- unlist(lapply(graph, function(call) {
40+
ind <- match("<-", call, nomatch = length(call) + 1L)
41+
call[seq_len(ind - 1L)]
42+
}))
43+
4844
if (!all(names %in% unique(symbols))) {
4945
warning("Object(s) not found in code: ", toString(setdiff(names, symbols)))
5046
}
5147
}
5248

53-
graph <- lapply(code, attr, "dependency")
5449
ind <- unlist(lapply(names, function(x) graph_parser(x, graph)))
5550

5651
lib_ind <- detect_libraries(graph)

tests/testthat/test-qenv_get_code.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,3 +886,12 @@ testthat::describe("Backticked symbol", {
886886
)
887887
})
888888
})
889+
890+
891+
# missing objects -------------------------------------------------------------------------------------------------
892+
893+
testthat::test_that("get_code raises warning for missing names", {
894+
q <- eval_code(qenv(), code = c("a<-1;b<-2"))
895+
testthat::expect_null(get_code(q, names = 'c'))
896+
testthat::expect_warning(get_code(q, names = 'c'), " not found in code: c")
897+
})

0 commit comments

Comments
 (0)