Skip to content

Commit

Permalink
Correctly handle callback for unparseable code
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jun 5, 2024
1 parent 7b7fd7d commit fa1e4a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ evaluate <- function(input,

parsed <- parse_all(input, filename, stop_on_error != 2L)
if (inherits(err <- attr(parsed, 'PARSE_ERROR'), 'error')) {
source <- new_source(parsed$src, call[[1]], output_handler$source)
source <- new_source(parsed$src, expression(), output_handler$source)
output_handler$error(err)
err$call <- NULL # the call is unlikely to be useful
return(list(source, err))
Expand Down
4 changes: 3 additions & 1 deletion R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ render <- function(x) if (isS4(x)) methods::show(x) else print(x)
#'
#' @param source Function to handle the echoed source code under evaluation.
#' This function should take two arguments (`src` and `call`), and return
#' an object that will be inserted into the evaluate outputs.
#' an object that will be inserted into the evaluate outputs. `src` is the
#' unparsed text of the source code, and `call` is the parsed language object
#' If `src` is unparsable, `call` will be `expression()`.
#'
#' Return `src` for the default evaluate behaviour. Return `NULL` to
#' drop the source from the output.
Expand Down
4 changes: 3 additions & 1 deletion man/new_output_handler.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test-eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,19 @@ test_that("can conditionally omit output with output handler", {
expect_length(out, 2)
expect_snapshot(replay(out))
})

test_that("source handled called correctly when src is unparseable", {
src <- NULL
call <- NULL
capture_args <- function(src, call) {
src <<- src
call <<- call

src
}
handler <- new_output_handler(source = capture_args)

evaluate("x + ", output_handler = handler)
expect_equal(src, new_source("x + "))
expect_equal(call, expression())
})

0 comments on commit fa1e4a0

Please sign in to comment.