Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion R/att_from_rscripts.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ att_from_rscript <- function(path) {
# Replace newlines `\n` by space
file <- gsub("\\\\n", " ", file)

pkg_points <- file %>%
# Ignore `::` patterns found in string literals (e.g. CSS selectors)
# Match both double-quoted and single-quoted strings, including escaped quotes
file_no_strings <- gsub("\"(?:[^\"\\\\]|\\\\.)*\"|'(?:[^'\\\\]|\\\\.)*'", "", file)

pkg_points <- file_no_strings %>%
.[grep("^#", ., invert = TRUE)] %>%
str_extract_all("[[:alnum:]\\.]+(?=::)") %>%
unlist()
Expand Down Expand Up @@ -77,6 +81,8 @@ att_from_rscripts <- function(path = "R", pattern = "*.[.](r|R)$", recursive = T
all_f <- setdiff(all_f, exclude_files)
}

# `att_from_rscript()` parses R code, so keep only R scripts
all_f <- all_f[grepl("\\.[rR]$", all_f)]

lapply(all_f, att_from_rscript) %>%
unlist() %>%
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-rscript.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,44 @@ test_that("folder_to_exclude works in att_from_rscripts", {


})

test_that("att_from_rscript ignores :: found in CSS strings", {
css_script <- tempfile(fileext = ".R")
on.exit(unlink(css_script), add = TRUE)
writeLines(
c(
"x <- '.pretty .state label::after { color: red; }'",
"glue::glue('ok')"
),
con = css_script
)

res <- att_from_rscript(path = css_script)

expect_true("glue" %in% res)
expect_false("label" %in% res)
})

test_that("att_from_rscripts ignores css files in scanned directories", {
dir_with_files <- tempfile(pattern = "rscripts_css")
dir.create(dir_with_files)
on.exit(unlink(dir_with_files, recursive = TRUE), add = TRUE)

writeLines(
c(
".pretty .state label::before {",
" color: red;",
"}"
),
con = file.path(dir_with_files, "global.css")
)
writeLines(
"glue::glue('ok')",
con = file.path(dir_with_files, "ok.R")
)

res <- att_from_rscripts(path = dir_with_files)

expect_true("glue" %in% res)
expect_false("label" %in% res)
})