Skip to content

Commit 560b40d

Browse files
improve handling of glued object extraction (#1612)
1 parent 8f9d1cc commit 560b40d

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

R/object_usage_linter.R

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,30 +159,16 @@ extract_glued_symbols <- function(expr) {
159159
return(character())
160160
}
161161
glued_symbols <- new.env(parent = emptyenv())
162+
162163
for (cl in glue_calls) {
163164
parsed_cl <- tryCatch(
164165
parse(text = xml2::xml_text(cl)),
165166
error = function(...) NULL,
166167
warning = function(...) NULL
167168
)[[1L]]
168169
if (is.null(parsed_cl)) next
169-
parsed_cl[[".transformer"]] <- function(text, envir) {
170-
parsed_text <- tryCatch(
171-
parse(text = text, keep.source = TRUE),
172-
error = function(...) NULL,
173-
warning = function(...) NULL
174-
)
175-
parsed_xml <- safe_parse_to_xml(parsed_text)
176-
# covers NULL & NA cases
177-
if (length(parsed_xml) == 0L) {
178-
return("")
179-
}
180-
symbols <- xml2::xml_text(xml2::xml_find_all(parsed_xml, "//SYMBOL"))
181-
for (sym in symbols) {
182-
assign(sym, NULL, envir = glued_symbols)
183-
}
184-
""
185-
}
170+
parsed_cl[[".envir"]] <- glued_symbols
171+
parsed_cl[[".transformer"]] <- symbol_extractor
186172
# #1459: syntax errors in glue'd code are ignored with warning, rather than crashing lint
187173
tryCatch(
188174
eval(parsed_cl),
@@ -196,7 +182,28 @@ extract_glued_symbols <- function(expr) {
196182
}
197183
)
198184
}
199-
ls(envir = glued_symbols, all.names = TRUE)
185+
names(glued_symbols)
186+
}
187+
188+
symbol_extractor <- function(text, envir, data) {
189+
parsed_text <- tryCatch(
190+
parse(text = text, keep.source = TRUE),
191+
error = function(...) NULL,
192+
warning = function(...) NULL
193+
)
194+
if (is.null(parsed_text)) {
195+
return("")
196+
}
197+
parse_data <- utils::getParseData(parsed_text)
198+
# covers NULL & NA cases
199+
if (nrow(parse_data) == 0L) {
200+
return("")
201+
}
202+
symbols <- parse_data$text[parse_data$token == "SYMBOL"]
203+
for (sym in symbols) {
204+
assign(sym, NULL, envir = envir)
205+
}
206+
""
200207
}
201208

202209
get_assignment_symbols <- function(xml) {

tests/testthat/test-object_usage_linter.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,24 @@ test_that("interprets glue expressions", {
351351
}
352352
"), NULL, linter)
353353

354+
# multiple variables in different interpolations
355+
expect_lint(trim_some("
356+
fun <- function() {
357+
local_key <- 'a'
358+
local_value <- 123
359+
glue::glue('Key-value pair: {local_key}={local_value}.')
360+
}
361+
"), NULL, linter)
362+
363+
# multiple variables in single interpolation
364+
expect_lint(trim_some("
365+
fun <- function() {
366+
local_str1 <- 'a'
367+
local_str2 <- 'b'
368+
glue::glue('With our powers combined: {paste(local_str1, local_str2)}.')
369+
}
370+
"), NULL, linter)
371+
354372
# Check non-standard .open and .close
355373
expect_lint(trim_some("
356374
fun <- function() {

0 commit comments

Comments
 (0)