r-lib / styler

Non-invasive pretty printing of R code
https://styler.r-lib.org
Other
725 stars 71 forks source link

Empty line in @examples causes uninformative error #1095

Closed giocomai closed 1 year ago

giocomai commented 1 year ago

Having an empty line in the @examples section throws an uninformative error.

I understand that such empty lines should not exist in a package, but running styler regularly while actively working on a function led to this issue, which took some time to troubleshoot.

Either ignoring (as there are no styling issues involved) or throwing an informative error would be preferable.

styler::style_text("
#' Empty line in examples
#'
#' @examples
#' 
example <- function() {

}"
)
#> Error in `map()`:
#> ℹ In index: 1.
#> ℹ With name: 2.
#> Caused by error in `purrr::map()`:
#> ℹ In index: 1.
#> ℹ With name: 1.
#> Caused by error in `map()`:
#> ℹ In index: 1.
#> ℹ With name: 0.
#> Caused by error in `map2()`:
#> ℹ In index: 1.
#> Caused by error in `$<-.data.frame`:
#> ! replacement has 1 row, data has 0

#> Backtrace:
#>      ▆
#>   1. ├─styler::style_text("\n#' Empty line in examples\n#'\n#' @examples\n#' \nexample <- function() {\n\n}")
#>   2. │ └─styler (local) transformer(text)
#>   3. │   └─styler:::parse_transform_serialize_roxygen(...)
#>   4. │     ├─... %>% flatten_chr()
#>   5. │     └─purrr::map_at(...)
#>   6. │       └─purrr::map(.x[where], .f, ..., .progress = .progress)
#>   7. │         └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>   8. │           ├─purrr:::with_indexed_errors(...)
#>   9. │           │ └─base::withCallingHandlers(...)
#>  10. │           ├─purrr:::call_with_cleanup(...)
#>  11. │           └─styler (local) .f(.x[[i]], ...)
#>  12. │             ├─... %>% flatten_chr()
#>  13. │             └─purrr::map(...)
#>  14. │               └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>  15. │                 ├─purrr:::with_indexed_errors(...)
#>  16. │                 │ └─base::withCallingHandlers(...)
#>  17. │                 ├─purrr:::call_with_cleanup(...)
#>  18. │                 └─styler (local) .f(.x[[i]], ...)
#>  19. │                   ├─... %>% flatten_chr()
#>  20. │                   └─purrr::map(...)
#>  21. │                     └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>  22. │                       ├─purrr:::with_indexed_errors(...)
#>  23. │                       │ └─base::withCallingHandlers(...)
#>  24. │                       ├─purrr:::call_with_cleanup(...)
#>  25. │                       └─styler (local) .f(.x[[i]], ...)
#>  26. │                         ├─... %>% flatten_chr()
#>  27. │                         └─purrr::map2(...)
#>  28. │                           └─purrr:::map2_("list", .x, .y, .f, ..., .progress = .progress)
#>  29. │                             ├─purrr:::with_indexed_errors(...)
#>  30. │                             │ └─base::withCallingHandlers(...)
#>  31. │                             ├─purrr:::call_with_cleanup(...)
#>  32. │                             └─styler (local) .f(.x[[i]], .y[[i]], ...)
#>  33. │                               └─code_snippet %>% ...
#>  34. ├─purrr::flatten_chr(.)
#>  35. ├─purrr::flatten_chr(.)
#>  36. ├─purrr::flatten_chr(.)
#>  37. ├─purrr::flatten_chr(.)
#>  38. ├─styler:::parse_transform_serialize_r(...)
#>  39. │ └─styler::compute_parse_data_nested(text, transformers, more_specs)
#>  40. │   └─pd_nested %>% add_cache_block()
#>  41. ├─styler:::add_cache_block(.)
#>  42. │ ├─base::`$<-`(`*tmp*`, "block", value = `<int>`)
#>  43. │ └─base::`$<-.data.frame`(`*tmp*`, "block", value = `<int>`)
#>  44. │   └─base::stop(...)
#>  45. └─base::.handleSimpleError(...)
#>  46.   └─purrr (local) h(simpleError(msg, call))
#>  47.     └─cli::cli_abort(...)
#>  48.       └─rlang::abort(...)

Created on 2023-02-05 with reprex v2.0.2

Similar result happens when the empty line is inside a \dontrun:

styler::style_text("
#' Empty line in examples
#'
#' @examples
#' \\dontrun{
#'
#' }
example <- function() {

}"
)
lorenzwalthert commented 1 year ago

@giocomai thanks for the report with the two examples. @IndrajeetPatil to me, this looks like a recently introduced regression, since with release 1.7.0, I don't see that happening. IIRC, we changed empty line handling in roxygen code examples recently somehow...

lorenzwalthert commented 1 year ago

git bisect yields: cee9d524950feb9967309b126764aef0ebc7264f is the offending commit.