Skip to content

Commit

Permalink
feature: supports special symbols with backticks
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Oct 16, 2024
1 parent 8a0ca10 commit 8b5e76e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
6 changes: 4 additions & 2 deletions R/utils-get_code_dependency.R
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ extract_occurrence <- function(calls_pd) {

# What occurs in a function body is not tracked.
x <- call_pd[!is_in_function(call_pd), ]
sym_cond <- which(x$token %in% c("SYMBOL", "SYMBOL_FUNCTION_CALL"))
sym_cond <- which(x$token %in% c("SPECIAL", "SYMBOL", "SYMBOL_FUNCTION_CALL"))

if (length(sym_cond) == 0) {
return(character(0L))
Expand Down Expand Up @@ -380,10 +380,12 @@ extract_side_effects <- function(calls_pd) {
#' @keywords internal
#' @noRd
graph_parser <- function(x, graph) {
# normalize x to remove surrounding backticks
x <- gsub("^`|`$", "", x)
occurrence <- vapply(
graph, function(call) {
ind <- match("<-", call, nomatch = length(call) + 1L)
x %in% call[seq_len(ind - 1L)]
x %in% gsub("^`|`$", "", call[seq_len(ind - 1L)])
},
logical(1)
)
Expand Down
55 changes: 55 additions & 0 deletions tests/testthat/test-get_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,58 @@ testthat::test_that("data() call is returned when data name is provided as a cha
)
)
})

testthat::describe("Backticked special symbols", {
testthat::it("starting with underscore code dependency is being detected", {
td <- teal_data() |>
within({
`_add_column_` <- function(lhs, rhs) dplyr::bind_cols(lhs, rhs)
IRIS <- `_add_column_`(iris, dplyr::tibble(new_col = "new column"))
})

testthat::expect_identical(
get_code(td, datanames = "IRIS"),
paste(
sep = "\n",
"`_add_column_` <- function(lhs, rhs) dplyr::bind_cols(lhs, rhs)",
"IRIS <- `_add_column_`(iris, dplyr::tibble(new_col = \"new column\"))"
)
)
})

testthat::it("with spaces code dependency is being detected", {
td <- teal_data() |>
within({
`add column` <- function(lhs, rhs) dplyr::bind_cols(lhs, rhs)
IRIS <- `add column`(iris, dplyr::tibble(new_col = "new column"))
})

testthat::expect_identical(
get_code(td, datanames = "IRIS"),
paste(
sep = "\n",
"`add column` <- function(lhs, rhs) dplyr::bind_cols(lhs, rhs)",
"IRIS <- `add column`(iris, dplyr::tibble(new_col = \"new column\"))"
)
)
})

testthat::it("with non-native pipe code dependency is being detected", {
td <- teal_data() |>
within({
`%add_column%` <- function(lhs, rhs) dplyr::bind_cols(lhs, rhs)
IRIS <- `%add_column%`(iris, dplyr::tibble(new_col = "new column"))
})

# Note that the original code is changed to use the non-native pipe operator
# correctly.
testthat::expect_identical(
get_code(td, datanames = "IRIS"),
paste(
sep = "\n",
"`%add_column%` <- function(lhs, rhs) dplyr::bind_cols(lhs, rhs)",
"IRIS <- iris %add_column% dplyr::tibble(new_col = \"new column\")"
)
)
})
})

0 comments on commit 8b5e76e

Please sign in to comment.