Skip to content

Commit a075fce

Browse files
committed
cleanup: remove method from eval_code and removal of unecessary functions
1 parent 7abc267 commit a075fce

File tree

2 files changed

+29
-47
lines changed

2 files changed

+29
-47
lines changed

R/qenv-eval_code.R

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,29 @@
3232
#' @export
3333
setGeneric("eval_code", function(object, code, cache = FALSE, ...) standardGeneric("eval_code"))
3434

35-
setMethod("eval_code", signature = c("qenv"), function(object, code, cache = FALSE, ...) {
35+
setMethod("eval_code", signature = c(object = "qenv", code = "ANY"), function(object, code, cache = FALSE, ...) {
3636
code <- .preprocess_code(code) # preprocess code to ensure it is a character vector
37+
srcref <- attr(code, "wholeSrcref")
38+
if (is.expression(code) && length(srcref) == 0L) {
39+
result <- Reduce(function(u, v) {
40+
if (inherits(v, "=") && identical(typeof(v), "language")) {
41+
# typeof(`=`) is language, but it doesn't dispatch on it, so we need to
42+
# explicitly pass it as first class of the object
43+
class(v) <- unique(c("language", class(v)))
44+
}
45+
.eval_code(u, v, cache = FALSE, ...)
46+
}, init = object, x = code)
47+
return(result)
48+
} else if (is.expression(code)) {
49+
code <- paste(attr(code, "wholeSrcref"), collapse = "\n")
50+
}
51+
.eval_code(object = object, code = code, cache = cache, ...)
52+
})
53+
54+
setMethod("eval_code", signature = c("qenv.error", "ANY"), function(object, code, cache = FALSE, ...) object)
55+
56+
#' @keywords internal
57+
.eval_code <- function(object, code, cache = FALSE, ...) {
3758
parsed_code <- parse(text = code, keep.source = TRUE)
3859
object@.xData <- rlang::env_clone(object@.xData, parent = parent.env(.GlobalEnv))
3960
if (length(parsed_code) == 0) {
@@ -66,7 +87,7 @@ setMethod("eval_code", signature = c("qenv"), function(object, code, cache = FAL
6687
errorCondition(
6788
message = sprintf(
6889
"%s \n when evaluating qenv code:\n%s",
69-
.ansi_strip(conditionMessage(e)),
90+
cli::ansi_strip(conditionMessage(e)),
7091
current_code
7192
),
7293
class = c("qenv.error", "try-error", "simpleError"),
@@ -75,11 +96,11 @@ setMethod("eval_code", signature = c("qenv"), function(object, code, cache = FAL
7596
}
7697
),
7798
warning = function(w) {
78-
attr(current_code, "warning") <<- .ansi_strip(sprintf("> %s\n", conditionMessage(w)))
99+
attr(current_code, "warning") <<- cli::ansi_strip(sprintf("> %s\n", conditionMessage(w)))
79100
invokeRestart("muffleWarning")
80101
},
81102
message = function(m) {
82-
attr(current_code, "message") <<- .ansi_strip(sprintf("> %s", conditionMessage(m)))
103+
attr(current_code, "message") <<- cli::ansi_strip(sprintf("> %s", conditionMessage(m)))
83104
invokeRestart("muffleMessage")
84105
}
85106
)
@@ -93,49 +114,10 @@ setMethod("eval_code", signature = c("qenv"), function(object, code, cache = FAL
93114

94115
lockEnvironment(object@.xData, bindings = TRUE)
95116
object
96-
})
97-
98-
99-
setMethod("eval_code", signature = c("qenv", "expression"), function(object, code, cache = FALSE, ...) {
100-
srcref <- attr(code, "wholeSrcref")
101-
if (length(srcref)) {
102-
eval_code(object, code = paste(attr(code, "wholeSrcref"), collapse = "\n"))
103-
} else {
104-
Reduce(function(u, v) {
105-
if (inherits(v, "=") && identical(typeof(v), "language")) {
106-
# typeof(`=`) is language, but it doesn't dispatch on it, so we need to
107-
# explicitly pass it as first class of the object
108-
class(v) <- unique(c("language", class(v)))
109-
}
110-
eval_code(u, v)
111-
}, init = object, x = code)
112-
}
113-
})
117+
}
114118

115119
setGeneric(".preprocess_code", function(code) standardGeneric(".preprocess_code"))
116-
117-
setMethod(".preprocess_code", signature = c("ANY"), function(code) {
118-
as.character(code)
119-
})
120-
120+
setMethod(".preprocess_code", signature = c("ANY"), function(code) as.character(code))
121121
setMethod(".preprocess_code", signature = c("language"), function(code) {
122122
paste(vapply(lang2calls(code), deparse1, collapse = "\n", character(1L)))
123123
})
124-
125-
setMethod("eval_code", signature = c("qenv.error", "ANY"), function(object, code, cache = FALSE, ...) {
126-
object
127-
})
128-
129-
# if cli is installed rlang adds terminal printing characters
130-
# which need to be removed
131-
.ansi_strip <- function(chr) {
132-
if (requireNamespace("cli", quietly = TRUE)) {
133-
cli::ansi_strip(chr)
134-
} else {
135-
chr
136-
}
137-
}
138-
139-
get_code_attr <- function(qenv, attr) {
140-
unlist(lapply(qenv@code, function(x) attr(x, attr)))
141-
}

tests/testthat/test-qenv_join.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ testthat::test_that("Joining two independent qenvs with warnings results in obje
131131
q <- c(q1, q2)
132132

133133
testthat::expect_equal(
134-
unname(get_code_attr(q, "warning")),
134+
vapply(q@code, attr, which = "warning", character(1L), USE.NAMES = FALSE),
135135
c(
136136
"> This is warning 1\n",
137137
"> This is warning 2\n"
@@ -146,7 +146,7 @@ testthat::test_that("Joining two independent qenvs with messages results in obje
146146
q <- c(q1, q2)
147147

148148
testthat::expect_equal(
149-
unname(get_code_attr(q, "message")),
149+
vapply(q@code, attr, which = "message", character(1L), USE.NAMES = FALSE),
150150
c(
151151
"> This is message 1\n",
152152
"> This is message 2\n"

0 commit comments

Comments
 (0)