Skip to content

Commit d2aa59d

Browse files
authored
More informative message when @inheritDotParams doesn't find topic (#1779)
Fixes #1602
1 parent 59de3d6 commit d2aa59d

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Markdown links now do a better job of resolving package names: the process is cached for better performance (#1724); it works with infix operators (e.g. `[%in%]`) (#1728); no longer changes the link text (#1662); and includes base packages when reporting ambiguous functions (#1725).
66
* Package documentation now uses `logo.svg` if available, falling back to `logo.png` (#1640).
77
* `@family` no longer adds a trailing space after the colon in the default family prefix (#1628). Custom `rd_family_title` values now automatically get a colon appended if they don't already end with one (#1656).
8+
* `@inheritDotParams` now generates an informative warning when the source function can't be found, instead of a cryptic error (#1602).
89
* `@inheritDotParams` now warns and produces no output when there are no parameters to inherit, instead of generating an empty `\describe` block that caused CRAN HTML validation warnings (#1671).
910
* `@description` no longer errors when the markdown text starts with a heading (#1705).
1011
* Package documentation now only wraps actual URLs in the `URL` field of `DESCRIPTION` (#1420).

R/rd-inherit.R

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ inherit_params <- function(topic, topics) {
168168
inherited_params <- find_params(
169169
inheritor,
170170
topics,
171-
source = topic$get_name()
171+
source = topic$get_name(),
172+
tag = "@inheritParams"
172173
)
173174

174175
for (param in inherited_params) {
@@ -217,7 +218,13 @@ inherit_dot_params <- function(topic, topics, env) {
217218
args <- map2(funs, inheritors$args, select_args_text, topic = topic)
218219

219220
# Then pull out the ones we need
220-
docs <- lapply(inheritors$source, find_params, topics = topics)
221+
docs <- lapply(
222+
inheritors$source,
223+
find_params,
224+
topics = topics,
225+
source = topic$get_name(),
226+
tag = "@inheritDotParams"
227+
)
221228
arg_matches <- function(args, docs) {
222229
match <- map_lgl(docs, \(x) all(x$name %in% args))
223230
matched <- docs[match]
@@ -290,8 +297,8 @@ get_documented_params <- function(topic, only_first = FALSE) {
290297
documented
291298
}
292299

293-
find_params <- function(name, topics, source) {
294-
topic <- get_rd(name, topics, source = source)
300+
find_params <- function(name, topics, source, tag = "@inherits") {
301+
topic <- get_rd(name, topics, source = source, tag = tag)
295302
if (is.null(topic)) {
296303
return()
297304
}
@@ -492,36 +499,39 @@ tweak_links <- function(x, package) {
492499

493500
# Find info in Rd or topic ------------------------------------------------
494501

495-
get_rd <- function(name, topics, source) {
502+
get_rd <- function(name, topics, source, tag = "@inherits") {
496503
if (is_namespaced(name)) {
497504
# External package
498505
parsed <- parse_expr(name)
499506
pkg <- as.character(parsed[[2]])
500507
fun <- as.character(parsed[[3]])
501508

502-
get_rd_from_help(pkg, fun, source)
509+
get_rd_from_help(pkg, fun, source, tag = tag)
503510
} else {
504511
# Current package
505512
rd_name <- topics$find_filename(name)
506513
if (identical(rd_name, NA_character_)) {
507-
warn_roxy_topic(source, "@inherits failed to find topic {.str {name}}")
514+
warn_roxy_topic(
515+
source,
516+
"{tag} failed to find topic {.str {name}} in current package"
517+
)
508518
}
509519
topics$get(rd_name)
510520
}
511521
}
512522

513-
get_rd_from_help <- function(package, alias, source) {
523+
get_rd_from_help <- function(package, alias, source, tag = "@inherits") {
514524
if (!is_installed(package)) {
515525
warn_roxy_topic(
516526
source,
517-
"@inherits failed because {.pkg {package}} is not installed"
527+
"{tag} failed because {.pkg {package}} is not installed"
518528
)
519529
return()
520530
}
521531

522532
help <- utils::help((alias), (package))
523533
if (length(help) == 0) {
524-
warn_roxy_topic(source, "@inherits failed to find topic {package}::{alias}")
534+
warn_roxy_topic(source, "{tag} failed to find topic {package}::{alias}")
525535
return()
526536
}
527537

tests/testthat/_snaps/rd-inherit.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,33 @@
8484
x In topic 'bar': @inheritDotParams failed.
8585
i No arguments inherited from `foo()`.
8686

87-
# useful warnings if can't find topics
87+
# inheritDotParams warns when source not found (#1602)
8888

8989
Code
90-
get_rd("base2::attach", source = "source")
90+
. <- roc_proc_text(rd_roclet(), text)
9191
Message
92-
x In topic 'source': @inherits failed because base2 is not installed.
93-
Output
94-
NULL
92+
x In topic 'test': @inheritDotParams failed to find topic "format" in current package.
93+
x In topic 'test': @inheritDotParams failed.
94+
i No arguments inherited from `format()`.
95+
96+
# useful warnings if can't find topics
97+
9598
Code
96-
get_rd("base::function_not_found", source = "source")
99+
get_rd("not_installed::pkg", source = "source")
97100
Message
98-
x In topic 'source': @inherits failed to find topic base::function_not_found.
101+
x In topic 'source': @inherits failed because not_installed is not installed.
99102
Output
100103
NULL
101104
Code
102-
get_rd("function", RoxyTopics$new(), source = "source")
105+
get_rd("base::doesntexist", source = "source")
103106
Message
104-
x In topic 'source': @inherits failed to find topic "function".
107+
x In topic 'source': @inherits failed to find topic base::doesntexist.
105108
Output
106109
NULL
107110
Code
108-
get_rd("foo::bar()", RoxyTopics$new(), source = "source")
111+
get_rd("doesntexist", RoxyTopics$new(), source = "source")
109112
Message
110-
x In topic 'source': @inherits failed to find topic "foo::bar()".
113+
x In topic 'source': @inherits failed to find topic "doesntexist" in current package.
111114
Output
112115
NULL
113116

tests/testthat/test-rd-inherit.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,14 @@ test_that("warns when no params to inherit (#1671)", {
817817
expect_false("..." %in% names(out[["bar.Rd"]]$get_value("param")))
818818
})
819819

820+
test_that("inheritDotParams warns when source not found (#1602)", {
821+
text <- "
822+
#' Test
823+
#' @inheritDotParams format
824+
test = function(...) {}
825+
"
826+
expect_snapshot(. <- roc_proc_text(rd_roclet(), text))
827+
})
820828

821829
# inherit everything ------------------------------------------------------
822830

@@ -861,10 +869,9 @@ test_that("can inherit all from single function", {
861869

862870
test_that("useful warnings if can't find topics", {
863871
expect_snapshot({
864-
get_rd("base2::attach", source = "source")
865-
get_rd("base::function_not_found", source = "source")
866-
get_rd("function", RoxyTopics$new(), source = "source")
867-
get_rd("foo::bar()", RoxyTopics$new(), source = "source")
872+
get_rd("not_installed::pkg", source = "source")
873+
get_rd("base::doesntexist", source = "source")
874+
get_rd("doesntexist", RoxyTopics$new(), source = "source")
868875
})
869876
})
870877

0 commit comments

Comments
 (0)