diff --git a/R/roxygen-examples-find.R b/R/roxygen-examples-find.R index bf61983c9..dde93f586 100644 --- a/R/roxygen-examples-find.R +++ b/R/roxygen-examples-find.R @@ -61,6 +61,17 @@ find_dont_seqs <- function(bare) { find_dont_closings <- function(bare, dont_openings) { opening <- cumsum(bare == "{") closing <- cumsum(bare == "}") + if (sum(closing) == 0L) { + rlang::abort(paste0( + "Failed to find closing braces for a \\dont* statement in a roxygen ", + "code example. This is most likely caused by nested \\donttest, ", + "\\dontrun or \\dontshow statement. These are not supported with ", + "{styler}, not even when {styler} is turned off for these lines as per ", + "the documentation at ", + "https://styler.r-lib.org/articles/styler.html#ignoring-certain-lines. ", + "See also GitHub issue https://github.com/r-lib/styler/issues/498." + )) + } diff <- opening - closing level_dont <- diff[dont_openings] match_closing <- intersect( diff --git a/tests/testthat/test-roxygen-examples-parse.R b/tests/testthat/test-roxygen-examples-parse.R index 5a1a56c96..8c02e6b41 100644 --- a/tests/testthat/test-roxygen-examples-parse.R +++ b/tests/testthat/test-roxygen-examples-parse.R @@ -41,6 +41,19 @@ test_that("donts can be parsed", { ) }) +test_that("Nested dont* statements return informative error message", { + expect_error( + style_roxygen_code_example_one( + c( + "#' @examples", "#' # styler: off", "#' \\donttest{", "#' \\dontshow{", + "#' 1+1", "#' }", "#' }", "#' # styler: on" + ), + transformers = tidyverse_style(scope = "spaces"), base_indention = 2L + ), + regexp = "nested \\\\donttest" + ) +}) + test_that("braces examples can be parsed", { expect_equal( parse_roxygen(