Skip to content

Commit f94f43e

Browse files
authored
Use new warn_roxy_tag() and warn_roxy_block() (#1317)
* Implement `warn_roxy_tag()` and use instead of `roxy_tag_warning()`. * Markdown parser always checks for matched `{` * Code is parsed once and recorded in `tag_code()` * New get `roxy_generated_tag()` helper that takes a block * File name resolution happens in `rd_section_reexport()` so we can use consistent warnings (always mentioning the tag). All tags now have file and line information, even if it's for the block.
1 parent b5ec35e commit f94f43e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1010
-422
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ export(tag_words)
238238
export(tag_words_line)
239239
export(update_collate)
240240
export(vignette_roclet)
241+
export(warn_roxy_tag)
241242
import(rlang)
242243
import(stringr)
243244
importFrom(R6,R6Class)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# roxygen2 (development version)
22

3+
* All warning messages have been reviewed to hopefully be more informative
4+
and actionable (#1317).
5+
36
* DOIs, arXiv links, and urls in the `Description` field of the `DESCRIPTION`
47
are now converted to the appropriate Rd markup (@dieghernan, #1265, #1164).
58

R/block.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ block_find_object <- function(block, env) {
143143
))
144144

145145
# Add in defaults generated from the object
146-
defaults <- object_defaults(object)
147-
defaults <- c(defaults, list(roxy_tag("backref", block$file, block$file)))
146+
defaults <- object_defaults(object, block)
147+
defaults <- c(defaults, list(roxy_generated_tag(block, "backref", block$file)))
148148

149149
default_tags <- map_chr(defaults, "tag")
150150
defaults <- defaults[!default_tags %in% block_tags(block)]
@@ -181,7 +181,7 @@ block_get_tag <- function(block, tag) {
181181
} else if (n == 1) {
182182
block$tags[[matches]]
183183
} else {
184-
roxy_tag_warning(block$tags[[matches[[2]]]], "May only use one @", tag, " per block")
184+
warn_roxy_block(block, "Block must contain only one @{tag}")
185185
block$tags[[matches[[1]]]]
186186
}
187187
}
@@ -289,3 +289,8 @@ parse_description <- function(tags) {
289289

290290
c(compact(list(title, description, details)), tags)
291291
}
292+
293+
warn_roxy_block <- function(block, message, ...) {
294+
message[[1]] <- paste0("[", block$file, ":", block$line, "] ", message[[1]])
295+
cli::cli_warn(message, ..., .envir = parent.frame())
296+
}

R/markdown-link.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ parse_link <- function(destination, contents, state) {
105105
if (!all(xml_name(contents) %in% c("text", "softbreak", "linebreak"))) {
106106
incorrect <- setdiff(unique(xml_name(contents)), c("text", "softbreak", "linebreak"))
107107

108-
roxy_tag_warning(state$tag,
109-
"Links must contain plain text. Problematic link: ", destination
110-
)
108+
warn_roxy_tag(state$tag, c(
109+
"markdown links must contain plain text",
110+
i = "Problematic link: {destination}"
111+
))
111112
return("")
112113
}
113114

R/markdown-state.R

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ markdown_on <- function(value = NULL) {
1616
return(isTRUE(markdown_env$`markdown-support`))
1717
}
1818

19+
local_markdown <- function(env = parent.frame()) {
20+
old <- markdown_env$`markdown-support`
21+
markdown_on(TRUE)
22+
withr::defer(markdown_on(old), envir = env)
23+
}
24+
1925
markdown_activate <- function(tags) {
2026
## markdown on/off based on global flag and presence of @md & @nomd
2127

@@ -24,12 +30,15 @@ markdown_activate <- function(tags) {
2430
has_nomd <- "noMd" %in% names
2531

2632
if (has_md && has_nomd) {
27-
roxy_tag_warning(tags[[1]], "Both @md and @noMd, no markdown parsing")
33+
md_tag <- tags[names == "md"][[1]]
34+
warn_roxy_tag(md_tag, "conflicts with @noMd; turning markdown parsing off")
35+
36+
md <- FALSE
37+
} else {
38+
md <- roxy_meta_get("markdown", FALSE)
39+
if (has_md) md <- TRUE
40+
if (has_nomd) md <- FALSE
2841
}
2942

30-
md <- roxy_meta_get("markdown", FALSE)
31-
if (has_md) md <- TRUE
32-
if (has_nomd) md <- FALSE
33-
3443
markdown_on(md)
3544
}

R/markdown.R

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ mdxml_children_to_rd <- function(xml, state) {
183183
mdxml_node_to_rd <- function(xml, state) {
184184
if (!inherits(xml, "xml_node") ||
185185
! xml_type(xml) %in% c("text", "element")) {
186-
roxy_tag_warning(state$tag, "Internal markdown translation failure")
186+
warn_roxy_tag(state$tag, c(
187+
"markdown translation failed",
188+
x = "Unexpected internal error",
189+
i = "Please file an issue at https://github.com/r-lib/roxygen2/issues"
190+
))
187191
return("")
188192
}
189193

@@ -221,11 +225,18 @@ mdxml_node_to_rd <- function(xml, state) {
221225
}
222226

223227
mdxml_unknown <- function(xml, tag) {
224-
roxy_tag_warning(tag, "Unknown xml node: ", xml_name(xml))
228+
warn_roxy_tag(tag, c(
229+
"markdown translation failed",
230+
x = "Internal error: unknown xml node {xml_name(xml)}",
231+
i = "Please file an issue at https://github.com/r-lib/roxygen2/issues"
232+
))
225233
escape_comment(xml_text(xml))
226234
}
227235
mdxml_unsupported <- function(xml, tag, feature) {
228-
roxy_tag_warning(tag, "Use of ", feature, " is not currently supported")
236+
warn_roxy_tag(tag, c(
237+
"markdown translation failed",
238+
x = "{feature} are not currently supported"
239+
))
229240
escape_comment(xml_text(xml))
230241
}
231242

@@ -398,7 +409,7 @@ mdxml_html_block <- function(xml, state) {
398409

399410
mdxml_html_inline <- function(xml, state) {
400411
if (state$tag$tag != "includeRmd") {
401-
return(mdxml_unsupported(xml, state$tag, "inline HTML"))
412+
return(mdxml_unsupported(xml, state$tag, "inline HTML components"))
402413
}
403414
paste0(
404415
"\\if{html}{\\out{",

R/namespace.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ roxy_tag_ns.roxy_tag_exportS3Method <- function(x, block, env, import_only = FAL
175175

176176
obj <- block$object
177177
if (length(x$val) < 2 && !inherits(obj, "s3method")) {
178-
roxy_tag_warning(x,
179-
"`@exportS3Method` and `@exportS3Method generic` must be used with an S3 method"
180-
)
178+
warn_roxy_tag(x, "must be used with an S3 method when it has 0 or 1 values")
181179
return()
182180
}
183181

@@ -234,7 +232,7 @@ roxy_tag_parse.roxy_tag_rawNamespace <- function(x) {
234232
}
235233
#' @export
236234
roxy_tag_ns.roxy_tag_rawNamespace <- function(x, block, env, import_only = FALSE) {
237-
x$val
235+
x$raw
238236
}
239237

240238
#' @export

R/object-defaults.R

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
object_defaults <- function(x) UseMethod("object_defaults")
1+
object_defaults <- function(x, block) UseMethod("object_defaults")
22

33
#' @export
4-
object_defaults.default <- function(x) list()
4+
object_defaults.default <- function(x, block) list()
55

66
#' @exportS3Method object_defaults "function"
7-
object_defaults.function <- function(x) {
7+
object_defaults.function <- function(x, block) {
88
list(
9-
roxy_tag("usage", NULL, object_usage(x)),
9+
roxy_generated_tag(block, "usage", object_usage(x)),
1010
# Used in process_inherit_params()
11-
roxy_tag(".formals", NULL, names(formals(x$value)))
11+
roxy_generated_tag(block, ".formals", names(formals(x$value)))
1212
)
1313
}
1414

@@ -25,19 +25,19 @@ object_defaults.s4generic <- object_defaults.function
2525
object_defaults.s4method <- object_defaults.function
2626

2727
#' @export
28-
object_defaults.data <- function(x) {
28+
object_defaults.data <- function(x, block) {
2929
str_out <- rd(object_format(x$value))
3030

3131
list(
32-
roxy_tag("docType", NULL, "data"),
33-
roxy_tag("format", NULL, str_out),
34-
roxy_tag("keywords", NULL, "datasets"),
35-
roxy_tag("usage", NULL, object_usage(x))
32+
roxy_generated_tag(block, "docType", "data"),
33+
roxy_generated_tag(block, "format", str_out),
34+
roxy_generated_tag(block, "keywords", "datasets"),
35+
roxy_generated_tag(block, "usage", object_usage(x))
3636
)
3737
}
3838

3939
#' @export
40-
object_defaults.package <- function(x) {
40+
object_defaults.package <- function(x, block) {
4141
desc <- x$value$desc
4242

4343
description <- as.character(desc$Description)
@@ -49,41 +49,41 @@ object_defaults.package <- function(x) {
4949
}
5050

5151
list(
52-
roxy_tag("docType", NULL, "package"),
53-
roxy_tag("name", NULL, package_suffix(desc$Package)),
52+
roxy_generated_tag(block, "docType", "package"),
53+
roxy_generated_tag(block, "name", package_suffix(desc$Package)),
5454
# "NULL" prevents addition of default aliases, see also #202
55-
roxy_tag("aliases", NULL, paste("NULL", desc$Package, package_suffix(desc$Package))),
56-
roxy_tag("title", NULL, paste0(desc$Package, ": ", desc$Title)),
57-
roxy_tag("description", NULL, description),
58-
roxy_tag("seealso", NULL, package_seealso(desc)),
59-
roxy_tag("author", NULL, package_authors(desc))
55+
roxy_generated_tag(block, "aliases", paste("NULL", desc$Package, package_suffix(desc$Package))),
56+
roxy_generated_tag(block, "title", paste0(desc$Package, ": ", desc$Title)),
57+
roxy_generated_tag(block, "description", description),
58+
roxy_generated_tag(block, "seealso", package_seealso(desc)),
59+
roxy_generated_tag(block, "author", package_authors(desc))
6060
)
6161
}
6262

6363
#' @export
64-
object_defaults.import <- function(x) {
64+
object_defaults.import <- function(x, block) {
6565
list(
66-
roxy_tag("docType", NULL, "import"),
67-
roxy_tag("name", NULL, "reexports"),
68-
roxy_tag("keywords", NULL, "internal"),
69-
roxy_tag("title", NULL, "Objects exported from other packages"),
70-
roxy_tag(".reexport", NULL, list(pkg = x$value$pkg, fun = x$value$fun))
66+
roxy_generated_tag(block, "docType", "import"),
67+
roxy_generated_tag(block, "name", "reexports"),
68+
roxy_generated_tag(block, "keywords", "internal"),
69+
roxy_generated_tag(block, "title", "Objects exported from other packages"),
70+
roxy_generated_tag(block, ".reexport", list(pkg = x$value$pkg, fun = x$value$fun))
7171
)
7272
}
7373

7474
#' @export
75-
object_defaults.s4class <- function(x) {
75+
object_defaults.s4class <- function(x, block) {
7676
list(
77-
roxy_tag("docType", NULL, "class")
77+
roxy_generated_tag(block, "docType", "class")
7878
)
7979
}
8080

8181
#' @export
82-
object_defaults.rcclass <- function(x) {
82+
object_defaults.rcclass <- function(x, block) {
8383
list(
84-
roxy_tag("docType", NULL, "class"),
84+
roxy_generated_tag(block, "docType", "class"),
8585
if (!is.null(x$methods))
86-
roxy_tag(".methods", NULL, x$methods)
86+
roxy_generated_tag(block, ".methods", x$methods)
8787
)
8888
}
8989

R/object-import.R

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
# Re-export ----------------------------------------------------------------
2-
rd_section_reexport <- function(pkg, fun) {
3-
stopifnot(is.character(pkg), is.character(fun))
2+
rd_section_reexport <- function(pkg, fun, file) {
3+
stopifnot(is.character(pkg), is.character(fun), is.character(file))
44
stopifnot(length(pkg) == length(fun))
55

6-
rd_section("reexport", list(pkg = pkg, fun = fun))
6+
rd_section("reexport", list(pkg = pkg, fun = fun, file = file))
77
}
88

99
#' @export
1010
roxy_tag_rd.roxy_tag_.reexport <- function(x, base_path, env) {
11-
rd_section_reexport(x$val$pkg, x$val$fun)
11+
file <- find_topic_filename(x$val$pkg, x$val$fun, tag = x)
12+
rd_section_reexport(x$val$pkg, x$val$fun, file)
1213
}
1314
#' @export
1415
merge.rd_section_reexport <- function(x, y, ...) {
1516
stopifnot(identical(class(x), class(y)))
16-
rd_section_reexport(c(x$value$pkg, y$value$pkg), c(x$value$fun, y$value$fun))
17+
rd_section_reexport(
18+
c(x$value$pkg, y$value$pkg),
19+
c(x$value$fun, y$value$fun),
20+
c(x$value$file, y$value$file)
21+
)
1722
}
1823
#' @export
1924
format.rd_section_reexport <- function(x, ...) {
20-
pkgs <- split(x$value$fun, x$value$pkg)
21-
pkg_links <- map2(names(pkgs), pkgs, function(pkg, funs) {
22-
funs <- sort_c(funs)
23-
files <- map_chr(
24-
funs,
25-
try_find_topic_in_package,
26-
pkg = pkg,
27-
where = " in re-export"
28-
)
25+
26+
info <- data.frame(
27+
pkg = x$value$pkg,
28+
fun = x$value$fun,
29+
file = x$value$file,
30+
stringsAsFactors = FALSE
31+
)
32+
33+
pkgs <- split(info, x$value$pkg)
34+
pkg_links <- map(pkgs, function(pkg) {
35+
pkg <- pkg[order(pkg$fun), ]
2936
links <- paste0(
30-
"\\code{\\link[", pkg,
31-
ifelse(files == funs, "", paste0(":", files)),
32-
"]{", escape(funs), "}}",
37+
"\\code{\\link[", pkg$pkg,
38+
ifelse(pkg$file == pkg$fun, "", paste0(":", pkg$file)),
39+
"]{", escape(pkg$fun), "}}",
3340
collapse = ", ")
34-
paste0("\\item{", pkg, "}{", links, "}")
41+
paste0("\\item{", pkg$pkg, "}{", links, "}")
3542
})
3643

3744
paste0(

R/object-r6.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#' @export
2-
object_defaults.r6class <- function(x) {
2+
object_defaults.r6class <- function(x, block) {
33
r6on <- roxy_meta_get("r6", TRUE)
44
if (isTRUE(r6on)) {
55
list(
6-
roxy_tag("docType", NULL, NULL),
7-
roxy_tag(".r6data", NULL, extract_r6_data(x$value))
6+
roxy_generated_tag(block, "docType", NULL),
7+
roxy_generated_tag(block, ".r6data", extract_r6_data(x$value))
88
)
99
} else {
1010
NextMethod()

0 commit comments

Comments
 (0)