Skip to content

Commit

Permalink
handle code included in curly brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
m7pr committed May 8, 2024
1 parent f195d1c commit c622769
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions R/utils-get_code_dependency.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ get_code_dependency <- function(code, names, check_names = TRUE) {
return(code)
}

# If code is bound in curly brackets, remove them.
tcode <- trimws(code)
if (grepl("^\\{", tcode) & grepl("\\}$", tcode)) {

Check warning on line 38 in R/utils-get_code_dependency.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/utils-get_code_dependency.R,line=38,col=28,[vector_logic_linter] Conditional expressions require scalar logical operators (&& and ||)
code <- gsub("^\\{", "", gsub("\\}$", "", tcode))
}


code <- parse(text = code, keep.source = TRUE)
pd <- utils::getParseData(code)
calls_pd <- extract_calls(pd)
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-get_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ testthat::test_that("handles the code without symbols on rhs", {
)
})

testthat::test_that("handles the code included in curly brackets", {
code <- "{1 + 1;a <- 5}"

testthat::expect_identical(
get_code(teal_data(a = 5, code = code), datanames = "a"),
paste(warning_message, "a <- 5", sep = "\n")
)
})


testthat::test_that("extracts the code of a binding from character vector containing simple code", {
code <- c(
"a <- 1",
Expand Down Expand Up @@ -421,6 +431,24 @@ testthat::test_that("ignores occurrence in a function definition that has functi
)
})

testthat::test_that("ignores occurrence in a function definition if there is multiple function definitions", {
code <- c(
"b <- 2",
"foo <- function(b) { function(c) {b <- c + 2 }}",
"b <- b + 1",
"bar <- function(b) print(b)"
)
tdata <- eval_code(teal_data(), code)
testthat::expect_identical(
get_code(tdata, datanames = "b"),
"b <- 2"
)
testthat::expect_identical(
get_code(tdata, datanames = "foo"),
"foo <- function(b) {\n function(c) {\n b <- c + 2\n }\n}"
)
})

testthat::test_that("ignores occurrence in a function definition in lapply", {
code <- c(
"a <- list(a = 1, b = 2, c = 3)",
Expand Down

0 comments on commit c622769

Please sign in to comment.