Skip to content

Commit 3ba65f2

Browse files
committed
treat empty calls or comments as separate calls
1 parent ef00c58 commit 3ba65f2

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

R/qenv-eval_code.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
3131
parsed_code <- parse(text = code, keep.source = TRUE)
3232
object@env <- rlang::env_clone(object@env, parent = parent.env(.GlobalEnv))
3333
if (length(parsed_code) == 0) {
34+
# empty code, or just comments
35+
attr(code, "id") <- sample.int(.Machine$integer.max, size = 1)
36+
attr(code, "dependency") <- extract_dependency(parsed_code) # in case comment contains @linksto tag
37+
object@code <- c(object@code, list(code))
3438
return(object)
3539
}
3640
code_split <- split_code(paste(code, collapse = "\n"))

R/utils-get_code_dependency.R

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,20 +315,22 @@ extract_occurrence <- function(pd) {
315315
#' @noRd
316316
extract_side_effects <- function(pd) {
317317
linksto <- grep("@linksto", pd[pd$token == "COMMENT", "text"], value = TRUE)
318-
unlist(strsplit(sub("\\s*#\\s*@linksto\\s+", "", linksto), "\\s+"))
318+
unlist(strsplit(sub("\\s*#.*@linksto\\s+", "", linksto), "\\s+"))
319319
}
320320

321321
#' @param parsed_code results of `parse(text = code, keep.source = TRUE` (parsed text)
322322
#' @keywords internal
323323
#' @noRd
324324
extract_dependency <- function(parsed_code) {
325325
pd <- normalize_pd(utils::getParseData(parsed_code))
326-
reordered_pd <- extract_calls(pd)[[1]]
327-
# extract_calls is needed to reorder the pd so that assignment operator comes before symbol names
328-
# extract_calls is needed also to substitute assignment operators into specific format with fix_arrows
329-
# extract_calls is needed to omit empty calls that contain only one token `"';'"`
330-
# This cleaning is needed as extract_occurrence assumes arrows are fixed, and order is different than in original pd.
331-
c(extract_side_effects(reordered_pd), extract_occurrence(reordered_pd))
326+
reordered_pd <- extract_calls(pd)
327+
if (length(reordered_pd) > 0) {
328+
# extract_calls is needed to reorder the pd so that assignment operator comes before symbol names
329+
# extract_calls is needed also to substitute assignment operators into specific format with fix_arrows
330+
# extract_calls is needed to omit empty calls that contain only one token `"';'"`
331+
# This cleaning is needed as extract_occurrence assumes arrows are fixed, and order is different than in original pd.
332+
c(extract_side_effects(reordered_pd[[1]]), extract_occurrence(reordered_pd[[1]]))
333+
}
332334
}
333335

334336
# graph_parser ----

tests/testthat/test-qenv_eval_code.R

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,28 @@ testthat::test_that("comments from the same line are associated with it's call",
258258
)
259259
})
260260

261-
testthat::test_that("comments alone passed to eval_code are skipped", {
261+
testthat::test_that("comments passed alone to eval_code are separate calls", {
262262
code <- c("x <- 5", "# comment")
263263
q <- eval_code(eval_code(qenv(), code[1]), code[2])
264264
testthat::expect_identical(
265265
get_code(q),
266-
code[1]
266+
pasten(code)
267+
)
268+
})
269+
270+
testthat::test_that("comments passed alone to eval_code that contain @linksto tag have detected dependency", {
271+
code <- c("x <- 5", "# comment @linksto x")
272+
q <- eval_code(eval_code(qenv(), code[1]), code[2])
273+
testthat::expect_identical(
274+
get_code(q),
275+
pasten(code)
276+
)
277+
testthat::expect_identical(
278+
get_code(q, names = 'x'),
279+
pasten(code)
280+
)
281+
testthat::expect_identical(
282+
attr(q@code[[2]], "dependency"),
283+
"x"
267284
)
268285
})

0 commit comments

Comments
 (0)