Skip to content

Commit

Permalink
Merge pull request #271 from toppyy/scrub_function_defs
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Oct 16, 2024
2 parents 2e0ab81 + d13894b commit 89063e1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion R/telemetry.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ expr_to_json <- function(x, name_map) {
expr_deparse(scrubbed, width = 500L)
}

expr_scrub <- function(x, name_map) {
expr_scrub <- function(x, name_map = character()) {
do_scrub <- function(xx, callee = FALSE) {
if (is.character(xx)) {
return("<character>")
Expand All @@ -248,6 +248,9 @@ expr_scrub <- function(x, name_map) {
return(xx)
} else if (is.atomic(xx)) {
return(xx)
} else if (is_missing(xx)) {
# Arguments without default values are empty
return(xx)
} else if (is_symbol(xx)) {
if (callee) {
return(xx)
Expand All @@ -264,6 +267,8 @@ expr_scrub <- function(x, name_map) {
} else if (is_call(xx)) {
args <- map(as.list(xx)[-1], do_scrub)
call2(do_scrub(xx[[1]], callee = TRUE), !!!args)
} else if (is_pairlist(xx)) {
as.pairlist(map(as.list(xx), do_scrub))
} else {
paste0("Don't know how to scrub ", paste(class(xx), collapse = "/"))
}
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,11 @@
Error in `rel_try()`:
! union_all: {"version":"0.3.1","message":"Error in union_all","name":"union_all","x":{"...1":"integer","...2":"integer"},"y":{"...1":"integer","...2":"integer"}}

# scrubbing function declarations

Code
expr <- expr(across(x:y, function(arg) mean(arg, na.rm = TRUE)))
expr_scrub(expr)
Output
across(...1:...2, function(arg) mean(...3, na.rm = TRUE))

10 changes: 10 additions & 0 deletions tests/testthat/test-telemetry.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,13 @@ test_that("telemetry and union_all()", {
union_all(tibble(a = 1:3, b = 4:6))
})
})

test_that("scrubbing function declarations", {
expect_snapshot({
expr <- expr(
across(x:y, function(arg) mean(arg, na.rm = TRUE))
)

expr_scrub(expr)
})
})

0 comments on commit 89063e1

Please sign in to comment.