Skip to content

Commit

Permalink
Check if trimmed tag value is empty
Browse files Browse the repository at this point in the history
Fixes #1228
  • Loading branch information
hadley committed Mar 30, 2022
1 parent 623c473 commit ecaec67
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# roxygen2 (development version)

* All tags warn now if you only provide whitespace (#1228).

* Add support for inheriting 'note' fields via `@inherit pkg::fun note` (@pat-s, #1218)

* Problems with the first tag in each block are now reported with the
Expand Down
16 changes: 10 additions & 6 deletions R/tag-parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ NULL
#' @export
#' @rdname tag_parsers
tag_value <- function(x) {
if (x$raw == "") {
if (str_trim(x$raw) == "") {
roxy_tag_warning(x, "requires a value")
} else if (!rdComplete(x$raw, is_code = FALSE)) {
roxy_tag_warning(x, "mismatched braces or quotes")
Expand All @@ -30,7 +30,7 @@ tag_value <- function(x) {
#' @export
#' @rdname tag_parsers
tag_inherit <- function(x) {
if (x$raw == "") {
if (str_trim(x$raw) == "") {
roxy_tag_warning(x, "requires a value")
} else if (!rdComplete(x$raw, is_code = FALSE)) {
roxy_tag_warning(x, "mismatched braces or quotes")
Expand Down Expand Up @@ -64,7 +64,7 @@ tag_inherit <- function(x) {
#' @export
#' @rdname tag_parsers
tag_name <- function(x) {
if (x$raw == "") {
if (str_trim(x$raw) == "") {
roxy_tag_warning(x, "requires a name")
} else if (!rdComplete(x$raw, is_code = FALSE)) {
roxy_tag_warning(x, "mismatched braces or quotes")
Expand Down Expand Up @@ -161,7 +161,7 @@ tag_toggle <- function(x) {
#' @export
#' @rdname tag_parsers
tag_code <- function(x) {
if (x$raw == "") {
if (str_trim(x$raw) == "") {
roxy_tag_warning(x, "requires a value")
} else {
tryCatch({
Expand All @@ -178,7 +178,7 @@ tag_code <- function(x) {
#' @export
#' @rdname tag_parsers
tag_examples <- function(x) {
if (x$raw == "") {
if (str_trim(x$raw) == "") {
return(roxy_tag_warning(x, "requires a value"))
}

Expand All @@ -193,14 +193,18 @@ tag_examples <- function(x) {
#' @export
#' @rdname tag_parsers
tag_markdown <- function(x) {
if (str_trim(x$raw) == "") {
return(roxy_tag_warning(x, "requires a value"))
}

x$val <- markdown_if_active(x$raw, x)
x
}

#' @export
#' @rdname tag_parsers
tag_markdown_with_sections <- function(x) {
if (x$raw == "") {
if (str_trim(x$raw) == "") {
return(roxy_tag_warning(x, "requires a value"))
}

Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-tag-parser.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test_that("tags containing only whitespace generate warning", {
tag <- roxy_tag("foo", " ")

expect_parse_failure <- function(code) {
expect_warning(out <- code, "requires")
expect_null(out)
}

expect_parse_failure(tag_value(tag))
expect_parse_failure(tag_inherit(tag))
expect_parse_failure(tag_name(tag))
expect_parse_failure(tag_two_part(tag))
expect_parse_failure(tag_name_description(tag))
expect_parse_failure(tag_code(tag))
expect_parse_failure(tag_examples(tag))
expect_parse_failure(tag_markdown(tag))
expect_parse_failure(tag_markdown_with_sections(tag))
})

0 comments on commit ecaec67

Please sign in to comment.