diff --git a/R/att_from_rscripts.R b/R/att_from_rscripts.R index ce36c4c..c94ec69 100644 --- a/R/att_from_rscripts.R +++ b/R/att_from_rscripts.R @@ -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() @@ -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() %>% diff --git a/tests/testthat/test-rscript.R b/tests/testthat/test-rscript.R index d8a279f..395245d 100644 --- a/tests/testthat/test-rscript.R +++ b/tests/testthat/test-rscript.R @@ -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) +})