Skip to content

Commit 4c7fa7a

Browse files
[skip style] [skip vbump] Restyle files
1 parent 6f03292 commit 4c7fa7a

File tree

4 files changed

+136
-125
lines changed

4 files changed

+136
-125
lines changed

R/utils-get_code_dependency.R

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -211,86 +211,86 @@ extract_occurrence <- function(pd) {
211211
}
212212
}
213213

214-
# Handle data(object)/data("object")/data(object, envir = ) independently.
215-
data_call <- find_call(pd, "data")
216-
if (data_call) {
217-
sym <- pd[data_call + 1, "text"]
218-
return(c(gsub("^['\"]|['\"]$", "", sym), "<-"))
219-
}
220-
# Handle assign(x = ).
221-
assign_call <- find_call(pd, "assign")
222-
if (assign_call) {
223-
# Check if parameters were named.
224-
# "','" is for unnamed parameters, where "SYMBOL_SUB" is for named.
225-
# "EQ_SUB" is for `=` appearing after the name of the named parameter.
226-
if (any(pd$token == "SYMBOL_SUB")) {
227-
params <- pd[pd$token %in% c("SYMBOL_SUB", "','", "EQ_SUB"), "text"]
228-
# Remove sequence of "=", ",".
229-
if (length(params > 1)) {
230-
remove <- integer(0)
231-
for (i in 2:length(params)) {
232-
if (params[i - 1] == "=" & params[i] == ",") {
233-
remove <- c(remove, i - 1, i)
234-
}
235-
}
236-
if (length(remove)) params <- params[-remove]
237-
}
238-
pos <- match("x", setdiff(params, ","), nomatch = match(",", params, nomatch = 0))
239-
if (!pos) {
240-
return(character(0L))
214+
# Handle data(object)/data("object")/data(object, envir = ) independently.
215+
data_call <- find_call(pd, "data")
216+
if (data_call) {
217+
sym <- pd[data_call + 1, "text"]
218+
return(c(gsub("^['\"]|['\"]$", "", sym), "<-"))
219+
}
220+
# Handle assign(x = ).
221+
assign_call <- find_call(pd, "assign")
222+
if (assign_call) {
223+
# Check if parameters were named.
224+
# "','" is for unnamed parameters, where "SYMBOL_SUB" is for named.
225+
# "EQ_SUB" is for `=` appearing after the name of the named parameter.
226+
if (any(pd$token == "SYMBOL_SUB")) {
227+
params <- pd[pd$token %in% c("SYMBOL_SUB", "','", "EQ_SUB"), "text"]
228+
# Remove sequence of "=", ",".
229+
if (length(params > 1)) {
230+
remove <- integer(0)
231+
for (i in 2:length(params)) {
232+
if (params[i - 1] == "=" & params[i] == ",") {
233+
remove <- c(remove, i - 1, i)
241234
}
242-
# pos is indicator of the place of 'x'
243-
# 1. All parameters are named, but none is 'x' - return(character(0L))
244-
# 2. Some parameters are named, 'x' is in named parameters: match("x", setdiff(params, ","))
245-
# - check "x" in params being just a vector of named parameters.
246-
# 3. Some parameters are named, 'x' is not in named parameters
247-
# - check first appearance of "," (unnamed parameter) in vector parameters.
248-
} else {
249-
# Object is the first entry after 'assign'.
250-
pos <- 1
251235
}
252-
sym <- pd[assign_call + pos, "text"]
253-
return(c(gsub("^['\"]|['\"]$", "", sym), "<-"))
236+
if (length(remove)) params <- params[-remove]
254237
}
255-
256-
# What occurs in a function body is not tracked.
257-
x <- pd[!is_in_function(pd), ]
258-
sym_cond <- which(x$token %in% c("SPECIAL", "SYMBOL", "SYMBOL_FUNCTION_CALL"))
259-
260-
if (length(sym_cond) == 0) {
238+
pos <- match("x", setdiff(params, ","), nomatch = match(",", params, nomatch = 0))
239+
if (!pos) {
261240
return(character(0L))
262241
}
263-
# Watch out for SYMBOLS after $ and @. For x$a x@a: x is object, a is not.
264-
# For x$a, a's ID is $'s ID-2 so we need to remove all IDs that have ID = $ID - 2.
265-
dollar_ids <- x[x$token %in% c("'$'", "'@'"), "id"]
266-
if (length(dollar_ids)) {
267-
object_ids <- x[sym_cond, "id"]
268-
after_dollar <- object_ids[(object_ids - 2) %in% dollar_ids]
269-
sym_cond <- setdiff(sym_cond, which(x$id %in% after_dollar))
270-
}
242+
# pos is indicator of the place of 'x'
243+
# 1. All parameters are named, but none is 'x' - return(character(0L))
244+
# 2. Some parameters are named, 'x' is in named parameters: match("x", setdiff(params, ","))
245+
# - check "x" in params being just a vector of named parameters.
246+
# 3. Some parameters are named, 'x' is not in named parameters
247+
# - check first appearance of "," (unnamed parameter) in vector parameters.
248+
} else {
249+
# Object is the first entry after 'assign'.
250+
pos <- 1
251+
}
252+
sym <- pd[assign_call + pos, "text"]
253+
return(c(gsub("^['\"]|['\"]$", "", sym), "<-"))
254+
}
271255

272-
ass_cond <- grep("ASSIGN", x$token)
273-
if (!length(ass_cond)) {
274-
return(c("<-", unique(x[sym_cond, "text"])))
275-
}
256+
# What occurs in a function body is not tracked.
257+
x <- pd[!is_in_function(pd), ]
258+
sym_cond <- which(x$token %in% c("SPECIAL", "SYMBOL", "SYMBOL_FUNCTION_CALL"))
276259

277-
sym_cond <- sym_cond[sym_cond > ass_cond] # NOTE 1
278-
# If there was an assignment operation detect direction of it.
279-
if (unique(x$text[ass_cond]) == "->") { # NOTE 2
280-
sym_cond <- rev(sym_cond)
281-
}
260+
if (length(sym_cond) == 0) {
261+
return(character(0L))
262+
}
263+
# Watch out for SYMBOLS after $ and @. For x$a x@a: x is object, a is not.
264+
# For x$a, a's ID is $'s ID-2 so we need to remove all IDs that have ID = $ID - 2.
265+
dollar_ids <- x[x$token %in% c("'$'", "'@'"), "id"]
266+
if (length(dollar_ids)) {
267+
object_ids <- x[sym_cond, "id"]
268+
after_dollar <- object_ids[(object_ids - 2) %in% dollar_ids]
269+
sym_cond <- setdiff(sym_cond, which(x$id %in% after_dollar))
270+
}
282271

283-
after <- match(min(x$id[ass_cond]), sort(x$id[c(min(ass_cond), sym_cond)])) - 1
284-
ans <- append(x[sym_cond, "text"], "<-", after = max(1, after))
285-
roll <- in_parenthesis(pd)
286-
if (length(roll)) {
287-
c(setdiff(ans, roll), roll)
288-
} else {
289-
ans
290-
}
272+
ass_cond <- grep("ASSIGN", x$token)
273+
if (!length(ass_cond)) {
274+
return(c("<-", unique(x[sym_cond, "text"])))
275+
}
276+
277+
sym_cond <- sym_cond[sym_cond > ass_cond] # NOTE 1
278+
# If there was an assignment operation detect direction of it.
279+
if (unique(x$text[ass_cond]) == "->") { # NOTE 2
280+
sym_cond <- rev(sym_cond)
281+
}
291282

292-
### NOTE 2: What if there are 2 assignments: e.g. a <- b -> c.
293-
### NOTE 1: For cases like 'eval(expression(b <- b + 2))' removes 'eval(expression('.
283+
after <- match(min(x$id[ass_cond]), sort(x$id[c(min(ass_cond), sym_cond)])) - 1
284+
ans <- append(x[sym_cond, "text"], "<-", after = max(1, after))
285+
roll <- in_parenthesis(pd)
286+
if (length(roll)) {
287+
c(setdiff(ans, roll), roll)
288+
} else {
289+
ans
290+
}
291+
292+
### NOTE 2: What if there are 2 assignments: e.g. a <- b -> c.
293+
### NOTE 1: For cases like 'eval(expression(b <- b + 2))' removes 'eval(expression('.
294294
}
295295

296296
#' Extract side effects
@@ -389,7 +389,7 @@ detect_libraries <- function(graph) {
389389
which(
390390
unlist(
391391
lapply(
392-
graph, function(x){
392+
graph, function(x) {
393393
any(grepl(pattern = paste(defaults, collapse = "|"), x = x))
394394
}
395395
)
@@ -478,4 +478,3 @@ split_code <- function(code) {
478478
# semicolon is treated by R parser as a separate call.
479479
gsub("^([[:space:]])*;(.+)$", "\\1\\2", new_code, perl = TRUE)
480480
}
481-

tests/testthat/test-qenv_eval_code.R

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -92,47 +92,55 @@ testthat::test_that("an error when calling eval_code returns a qenv.error object
9292
})
9393

9494
testthat::test_that(
95-
"a warning when calling eval_code returns a qenv object which has warnings as attributes of code", {
96-
q <- eval_code(qenv(), quote("iris_data <- iris"))
97-
q <- eval_code(q, quote("p <- hist(iris_data[, 'Sepal.Length'], ff = '')"))
98-
testthat::expect_s4_class(q, "qenv")
99-
testthat::expect_equal(
100-
lapply(q@code, attr, "warning"),
101-
list(NULL, "> \"ff\" is not a graphical parameter\n")
102-
)
103-
})
95+
"a warning when calling eval_code returns a qenv object which has warnings as attributes of code",
96+
{
97+
q <- eval_code(qenv(), quote("iris_data <- iris"))
98+
q <- eval_code(q, quote("p <- hist(iris_data[, 'Sepal.Length'], ff = '')"))
99+
testthat::expect_s4_class(q, "qenv")
100+
testthat::expect_equal(
101+
lapply(q@code, attr, "warning"),
102+
list(NULL, "> \"ff\" is not a graphical parameter\n")
103+
)
104+
}
105+
)
104106

105107
testthat::test_that(
106-
"eval_code associates warnings with call by adding attribute to code element", {
107-
q <- eval_code(qenv(), c("x <- 1", "y <- 1", "warning('warn1')"))
108-
testthat::expect_equal(
109-
lapply(q@code, attr, "warning"),
110-
list(NULL, NULL, "> warn1\n")
111-
)
112-
})
108+
"eval_code associates warnings with call by adding attribute to code element",
109+
{
110+
q <- eval_code(qenv(), c("x <- 1", "y <- 1", "warning('warn1')"))
111+
testthat::expect_equal(
112+
lapply(q@code, attr, "warning"),
113+
list(NULL, NULL, "> warn1\n")
114+
)
115+
}
116+
)
113117

114118

115119
testthat::test_that(
116-
"eval_code associates messages with call by adding attribute to code element", {
117-
q <- eval_code(qenv(), quote("iris_data <- head(iris)"))
118-
q <- eval_code(q, quote("message('This is a message')"))
119-
testthat::expect_s4_class(q, "qenv")
120-
testthat::expect_equal(
121-
lapply(q@code, attr, "message"),
122-
list(
123-
NULL,
124-
"> This is a message\n"
120+
"eval_code associates messages with call by adding attribute to code element",
121+
{
122+
q <- eval_code(qenv(), quote("iris_data <- head(iris)"))
123+
q <- eval_code(q, quote("message('This is a message')"))
124+
testthat::expect_s4_class(q, "qenv")
125+
testthat::expect_equal(
126+
lapply(q@code, attr, "message"),
127+
list(
128+
NULL,
129+
"> This is a message\n"
130+
)
125131
)
126-
)
127-
})
132+
}
133+
)
128134

129135
testthat::test_that(
130-
"eval_code returns a qenv object with empty messages and warnings as code attributes, when none are returned", {
131-
q <- eval_code(qenv(), quote("iris_data <- head(iris)"))
132-
testthat::expect_s4_class(q, "qenv")
133-
testthat::expect_null(attr(q@code, "message"))
134-
testthat::expect_null(attr(q@code, "warning"))
135-
})
136+
"eval_code returns a qenv object with empty messages and warnings as code attributes, when none are returned",
137+
{
138+
q <- eval_code(qenv(), quote("iris_data <- head(iris)"))
139+
testthat::expect_s4_class(q, "qenv")
140+
testthat::expect_null(attr(q@code, "message"))
141+
testthat::expect_null(attr(q@code, "warning"))
142+
}
143+
)
136144

137145
testthat::test_that("eval_code returns a qenv object with dependency attribute", {
138146
q <- eval_code(qenv(), "iris_data <- head(iris)")
@@ -150,13 +158,15 @@ testthat::test_that("eval_code returns a qenv object with dependency attribute t
150158
)
151159
})
152160
testthat::test_that(
153-
"eval_code returns a qenv object with dependency attribute that extracts functions after '<-' part", {
154-
q3 <- eval_code(qenv(), c("library(survival)", "head(iris)"))
155-
testthat::expect_identical(
156-
lapply(q3@code, attr, "dependency"),
157-
list(
158-
c("<-", "library", "survival"),
159-
c("<-", "head", "iris")
161+
"eval_code returns a qenv object with dependency attribute that extracts functions after '<-' part",
162+
{
163+
q3 <- eval_code(qenv(), c("library(survival)", "head(iris)"))
164+
testthat::expect_identical(
165+
lapply(q3@code, attr, "dependency"),
166+
list(
167+
c("<-", "library", "survival"),
168+
c("<-", "head", "iris")
169+
)
160170
)
161-
)
162-
})
171+
}
172+
)

tests/testthat/test-qenv_extract.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
testthat::test_that("`[.` returns empty qenv for names not in qenv", {
32
data <- within(qenv(), {
43
x <- 1
@@ -65,4 +64,3 @@ testthat::test_that("`[.` extract proper elements of @id, @warnings and @message
6564
testthat::expect_null(get_code_attr(qs, "warning"))
6665
testthat::expect_null(get_code_attr(qs, "message"))
6766
})
68-

tests/testthat/test-qenv_get_code.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
get_code_g <- function(qenv, ...){
1+
get_code_g <- function(qenv, ...) {
22
gsub("\n", "", get_code(qenv, ...), fixed = TRUE)
33
}
44

@@ -393,7 +393,7 @@ testthat::test_that("get_code does not break if @linksto is put in the last line
393393

394394
testthat::test_that("@linksto makes a line being returned for an affected binding", {
395395
code <-
396-
"a <- 1 # @linksto b
396+
"a <- 1 # @linksto b
397397
b <- 2"
398398
q <- eval_code(qenv(), code)
399399
testthat::expect_identical(
@@ -475,7 +475,8 @@ testthat::test_that("comments fall into proper calls", {
475475
q <- eval_code(qenv(), code)
476476
testthat::expect_identical(
477477
get_code(q),
478-
c("\n # initial comment\n a <- 1\n",
478+
c(
479+
"\n # initial comment\n a <- 1\n",
479480
" b <- 2 # inline comment\n",
480481
" c <- 3\n",
481482
" # inbetween comment\n d <- 4\n # finishing comment\n "
@@ -500,7 +501,8 @@ testthat::test_that("comments get pasted when they fall into calls", {
500501
q <- qenv() |> eval_code(code)
501502
testthat::expect_identical(
502503
get_code(q),
503-
c("\n # initial comment\n a <- 1 # A comment\n",
504+
c(
505+
"\n # initial comment\n a <- 1 # A comment\n",
504506
" b <- 2 # inline comment\n",
505507
" c <- 3 # C comment\n",
506508
" # inbetween comment\n d <- 4\n # finishing comment\n "
@@ -646,8 +648,10 @@ testthat::test_that("detects occurrence of a function definition with a @linksto
646648
q <- eval_code(qenv(), code)
647649
testthat::expect_identical(
648650
get_code(q, names = "x"),
649-
c("\n foo <- function() {\n env <- parent.frame()\n env$x <- 0\n }\n",
650-
"foo() # @linksto x\n")
651+
c(
652+
"\n foo <- function() {\n env <- parent.frame()\n env$x <- 0\n }\n",
653+
"foo() # @linksto x\n"
654+
)
651655
)
652656
})
653657
# $ ---------------------------------------------------------------------------------------------------------------
@@ -892,6 +896,6 @@ testthat::describe("Backticked symbol", {
892896

893897
testthat::test_that("get_code raises warning for missing names", {
894898
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")
899+
testthat::expect_null(get_code(q, names = "c"))
900+
testthat::expect_warning(get_code(q, names = "c"), " not found in code: c")
897901
})

0 commit comments

Comments
 (0)