Skip to content

Commit f86bc13

Browse files
committed
assign side_effects and occurrence as attributes of code
1 parent f18d55a commit f86bc13

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

R/qenv-eval_code.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
7979
}
8080

8181
attr(current_code, "id") <- sample.int(.Machine$integer.max, size = 1)
82+
83+
# UNSURE if the removal of curly brackets is still needed.
84+
tcode <- trimws(current_code)
85+
if (any(grepl("^\\{.*\\}$", tcode))) {
86+
tcode <- sub("^\\{(.*)\\}$", "\\1", tcode)
87+
}
88+
89+
parsed_code <- parse(text = tcode, keep.source = TRUE)
90+
pd <- utils::getParseData(parsed_code)
91+
pd <- normalize_pd(pd)
92+
calls_pd <- extract_calls(pd)
93+
94+
attr(current_code, "side_effects") <- extract_side_effects(calls_pd)[[1]]
95+
attr(current_code, "occurrence") <- extract_occurrence(calls_pd)[[1]]
8296
object@code <- c(object@code, list(current_code))
8397
}
8498

R/utils-get_code_dependency.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ get_code_dependency <- function(code, names, check_names = TRUE) {
4444

4545
pd <- utils::getParseData(parsed_code)
4646
pd <- normalize_pd(pd)
47-
calls_pd <- extract_calls(pd)
47+
calls_pd <- extract_calls(pd) # STILL NEEDED for check_names
4848

4949
if (check_names) {
5050
# Detect if names are actually in code.
@@ -60,10 +60,10 @@ get_code_dependency <- function(code, names, check_names = TRUE) {
6060
}
6161
}
6262

63-
graph <- code_graph(calls_pd)
63+
graph <- extract_code_graph(code)
6464
ind <- unlist(lapply(names, function(x) graph_parser(x, graph)))
6565

66-
lib_ind <- detect_libraries(calls_pd)
66+
lib_ind <- detect_libraries(calls_pd) # SHOULD BE REWRITTEN TO WORK ON code
6767

6868
code_ids <- sort(unique(c(lib_ind, ind)))
6969
code[code_ids]
@@ -208,10 +208,10 @@ sub_arrows <- function(call) {
208208
#'
209209
#' @keywords internal
210210
#' @noRd
211-
code_graph <- function(calls_pd) {
212-
cooccurrence <- extract_occurrence(calls_pd)
211+
extract_code_graph <- function(code) {
212+
cooccurrence <- lapply(code, attr, "occurrence")
213213

214-
side_effects <- extract_side_effects(calls_pd)
214+
side_effects <- lapply(code, attr, "side_effects")
215215

216216
mapply(c, side_effects, cooccurrence, SIMPLIFY = FALSE)
217217
}

tests/testthat/test-qenv_get_code.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ testthat::test_that("detects cooccurrence properly even if all objects are on lh
688688
# @ ---------------------------------------------------------------------------------------------------------------
689689

690690
testthat::test_that("understands @ usage and do not treat rhs of @ as objects (only lhs)", {
691-
code <- list(
691+
code <- c(
692692
"setClass('aclass', slots = c(a = 'numeric', x = 'numeric', y = 'numeric')) # @linksto a x",
693693
"x <- new('aclass', a = 1:3, x = 1:3, y = 1:3)",
694694
"a <- new('aclass', a = 1:3, x = 1:3, y = 1:3)",
@@ -697,14 +697,14 @@ testthat::test_that("understands @ usage and do not treat rhs of @ as objects (o
697697
"a@x <- x@a"
698698
)
699699
q <- qenv()
700-
q@code <- code # we don't use eval_code so the code is not run
700+
q <- eval_code(q, code)
701701
testthat::expect_identical(
702702
get_code_g(q, names = "x"),
703-
unlist(code[1:2])
703+
code[1:2]
704704
)
705705
testthat::expect_identical(
706706
get_code_g(q, names = "a"),
707-
unlist(code)
707+
code
708708
)
709709
})
710710

0 commit comments

Comments
 (0)