From 2fc67cc6ff43a756e538d826561110b62e992e58 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 24 Nov 2026 07:05:16 -0600 Subject: [PATCH 1/4] Fix open issues #37, #39, #40, #41, #42 - #37: bare @exportS3Method registers the method; pkg::generic keeps its prefix for delayed registration - #39: support @examplesIf, rendered as roxygen2 does - #40: comma-separated @param names count for each formal - #41: missing-examples check runs per Rd page - #42: warn about markdown that ships verbatim in the Rd --- CLAUDE.md | 2 +- R/cran.R | 184 ++++- R/namespace.R | 45 +- R/rd.R | 84 +- R/tags.R | 51 +- README.md | 8 +- fyi.md | 1102 -------------------------- inst/tinytest/test_cran.R | 99 +++ inst/tinytest/test_namespace.R | 92 +++ inst/tinytest/test_rd.R | 8 + inst/tinytest/test_tags.R | 31 + man/RD_NAME_SUBS.Rd | 6 +- man/SUPPORTED_DOC_TAGS.Rd | 2 +- man/SUPPORTED_TAGS.Rd | 2 +- man/append_examples.Rd | 20 + man/check_markdown_rd.Rd | 23 + man/check_rd_filenames.Rd | 4 +- man/documented_params.Rd | 19 + man/find_exports_without_examples.Rd | 10 +- man/find_markdown_lines.Rd | 20 + man/order_params.Rd | 20 + man/rd_filename.Rd | 10 +- man/render_sections.Rd | 2 +- man/resolve_external_params.Rd | 2 +- 24 files changed, 650 insertions(+), 1196 deletions(-) delete mode 100644 fyi.md create mode 100644 man/append_examples.Rd create mode 100644 man/check_markdown_rd.Rd create mode 100644 man/documented_params.Rd create mode 100644 man/find_markdown_lines.Rd create mode 100644 man/order_params.Rd diff --git a/CLAUDE.md b/CLAUDE.md index 9162223..975f656 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -28,7 +28,7 @@ r -e 'tinyrox::document()' ## Supported Tags ### Documentation -`@title`, `@description`, `@details`, `@param`, `@return`, `@value`, `@examples`, `@seealso`, `@references`, `@aliases`, `@keywords`, `@name`, `@rdname`, `@noRd`, `@inheritParams` +`@title`, `@description`, `@details`, `@param`, `@return`, `@value`, `@examples`, `@examplesIf`, `@seealso`, `@references`, `@aliases`, `@keywords`, `@name`, `@rdname`, `@noRd`, `@inheritParams` ### Namespace `@export`, `@exportS3Method`, `@exportClass`, `@import`, `@importFrom`, `@useDynLib` diff --git a/R/cran.R b/R/cran.R index bed04ce..8fcb703 100644 --- a/R/cran.R +++ b/R/cran.R @@ -33,9 +33,13 @@ check_cran <- function(path = ".") { code_result <- check_code_cran(path) example_result <- check_examples_cran(path) - all_issues <- list(code = code_result, examples = example_result) + markdown_result <- check_markdown_rd(path) - has_issues <- length(code_result) > 0 || length(example_result) > 0 + all_issues <- list(code = code_result, examples = example_result, + markdown = markdown_result) + + has_issues <- length(code_result) > 0 || length(example_result) > 0 || + length(markdown_result) > 0 if (!has_issues) { message("No CRAN compliance issues found") @@ -80,14 +84,16 @@ check_examples_cran <- function(path = ".") { return(character()) } - missing_examples <- character() + # document() parses the package again and reports parse warnings + # (orphaned blocks, unknown tags) itself; repeating them here would + # only print each one twice. + blocks <- suppressWarnings(parse_package(path)) + missing_examples <- suppressWarnings(find_exports_without_examples(blocks)) dontrun_fns <- character() long_lines <- character() for (file in r_files) { content <- readLines(file, warn = FALSE) - file_missing <- find_exports_without_examples(content) - missing_examples <- c(missing_examples, file_missing) file_dontrun <- find_dontrun_examples(content) dontrun_fns <- c(dontrun_fns, file_dontrun) file_long <- find_long_example_lines(content, basename(file)) @@ -119,53 +125,50 @@ check_examples_cran <- function(path = ".") { #' Find Exported Functions Without Examples #' -#' Parses R file content to find @export tags without @examples. +#' Examples belong to an Rd page, and blocks sharing an @rdname share one +#' page, so the check runs per topic: an exported function is reported only +#' when no block on its page carries examples. A block that generates no +#' page (a bare @export above an S3 method, or @noRd) is skipped, since +#' there is nowhere for an example to live. #' -#' @param lines Character vector of file lines +#' @param blocks List of documentation blocks from parse_package() #' @return Character vector of function names missing examples -find_exports_without_examples <- function(lines) { - missing <- character() - in_doc_block <- FALSE - has_export <- FALSE - has_examples <- FALSE - block_start <- 0 +find_exports_without_examples <- function(blocks) { + exported <- character() + exported_topic <- character() + topics_with_examples <- character() - for (i in seq_along(lines)) { - line <- lines[i] + for (block in blocks) { + if (!block$type %in% c("function", "nn_module", "data")) { + next + } + tags <- parse_tags(block$lines, block$object, block$file, block$line) - # Start of doc block - if (grepl("^##?'", line)) { - if (!in_doc_block) { - in_doc_block <- TRUE - has_export <- FALSE - has_examples <- FALSE - block_start <- i - } + # Same page test as generate_all_rd() + if (tags$noRd || + (is.null(tags$title) && is.null(tags$description) && + is.null(tags$rdname))) { + next + } - # Check for export tag - if (grepl("^##?'\\s*@export", line)) { - has_export <- TRUE - } + if (!is.null(tags$rdname)) { + topic <- tags$rdname + } else { + topic <- tags$name + } - # Check for examples tag - if (grepl("^##?'\\s*@examples", line)) { - has_examples <- TRUE - } - } else if (in_doc_block) { - # End of doc block - check if it's a function definition - in_doc_block <- FALSE + if (!is.null(tags$examples) && nzchar(trimws(tags$examples))) { + topics_with_examples <- c(topics_with_examples, topic) + } - if (has_export && !has_examples) { - # Try to get function name from next non-blank line - func_name <- extract_function_name(line) - if (!is.null(func_name)) { - missing <- c(missing, func_name) - } - } + is_exported <- tags$export || !is.null(tags$exportS3Method) + if (is_exported && block$type == "function") { + exported <- c(exported, block$object) + exported_topic <- c(exported_topic, topic) } } - missing + exported[!exported_topic %in% topics_with_examples] } #' Find Exported Functions Using dontrun in Examples @@ -255,6 +258,103 @@ find_long_example_lines <- function(lines, filename) { found } +#' Warn About Markdown That Ships Verbatim +#' +#' tinyrox does no markdown parsing, so backticked text and bracketed +#' function links written in the roxygen2 markdown habit reach the Rd +#' unchanged and render literally in help pages and the PDF manual. +#' R CMD check says nothing about them. This reports each affected file +#' once. Set options(tinyrox.markdown_check = FALSE) to silence it for a +#' package that uses backticks on purpose. +#' +#' @param path Path to package root directory +#' @return Named list, one integer vector of line numbers per affected +#' file (invisibly); empty when nothing was found or the check is off. +#' @keywords internal +check_markdown_rd <- function(path = ".") { + if (!isTRUE(getOption("tinyrox.markdown_check", TRUE))) { + return(invisible(list())) + } + + r_files <- list.files(file.path(path, "R"), pattern = "\\.R$", + full.names = TRUE, ignore.case = TRUE) + found <- list() + for (file in r_files) { + hits <- find_markdown_lines(readLines(file, warn = FALSE)) + if (length(hits) > 0) { + found[[basename(file)]] <- hits + } + } + + if (length(found) > 0) { + report <- vapply(names(found), function(f) { + paste0(" ", f, ": ", length(found[[f]]), " line(s), first at ", + f, ":", found[[f]][1]) + }, character(1)) + warning("Markdown is not converted and ships verbatim in the Rd. ", + "Write \\code{x} for a backticked x and \\code{\\link{fn}} ", + "for a bracketed fn() link:\n", + paste(report, collapse = "\n"), + "\nSilence with options(tinyrox.markdown_check = FALSE).", + call. = FALSE) + } + + invisible(found) +} + +#' Find Documentation Lines Containing Markdown +#' +#' Flags backtick pairs and bracketed function links in documentation +#' prose. Example code is skipped (backticks are R syntax there), as are +#' blocks tagged noRd (they ship nowhere) and text already inside an Rd +#' code macro. +#' +#' @param lines Character vector of file lines +#' @return Integer vector of line numbers +#' @keywords internal +find_markdown_lines <- function(lines) { + is_doc <- grepl("^##?'", lines) + if (!any(is_doc)) { + return(integer()) + } + + # Consecutive doc lines form one block, as in parse_file() + block_id <- cumsum(c(TRUE, diff(is_doc) != 0)) + text <- sub("^##?'\\s?", "", lines) + found <- integer() + + for (id in unique(block_id[is_doc])) { + idx <- which(block_id == id) + if (any(grepl("^\\s*@noRd\\b", text[idx]))) { + next + } + in_examples <- FALSE + for (i in idx) { + tag <- regmatches(text[i], regexec("^\\s*@([a-zA-Z0-9]+)", text[i]))[[1]] + if (length(tag) == 2) { + in_examples <- tag[2] %in% c("examples", "example", + "examplesIf") + if (tag[2] %in% c("name", "rdname", "aliases", + SUPPORTED_NS_TAGS)) { + next + } + } + if (in_examples) { + next + } + # Drop spans already in an Rd code macro: \code{`[`} is correct + prose <- gsub("\\\\(code|verb|samp|preformatted|eqn|deqn)\\{[^{}]*\\}", + "", text[i]) + if (grepl("`[^`]+`", prose) || + grepl("\\[[A-Za-z0-9._:]+\\(\\)\\]", prose)) { + found <- c(found, i) + } + } + } + + found +} + #' Extract Function Name from Definition Line #' #' @param line Code line potentially containing function definition diff --git a/R/namespace.R b/R/namespace.R index f76c333..ee0c848 100644 --- a/R/namespace.R +++ b/R/namespace.R @@ -82,14 +82,46 @@ generate_namespace <- function(blocks) { if (!is.null(s3m$generic) && !is.null(s3m$class)) { s3methods <- c(s3methods, list(list(generic = s3m$generic, class = s3m$class))) + } else if (is.character(s3m$explicit) && + grepl("::", s3m$explicit, fixed = TRUE)) { + # @exportS3Method pkg::generic: a method for a generic this + # package does not import. R registers it when pkg loads, + # so the directive must keep the pkg:: prefix. The class is + # the object name with "generic." removed. + prefix <- paste0(sub("^.*::", "", s3m$explicit), ".") + if (startsWith(block$object, prefix) && + nchar(block$object) > nchar(prefix)) { + s3methods <- c(s3methods, list(list( + generic = s3m$explicit, + class = substring(block$object, + nchar(prefix) + 1L) + ))) + } else { + warning("@exportS3Method ", s3m$explicit, " on ", + block$object, " at ", basename(block$file), ":", + block$line, ": the object name does not start ", + "with \"", prefix, "\", so the class is ", + "unknown; nothing was registered.", call. = FALSE) + } } else if (!is.null(s3m$explicit)) { - # Try to parse from function name: generic.class + # Infer from the function name. A known or package-defined + # generic wins (as.data.frame.foo); otherwise split at the + # first dot. + s3_info <- detect_s3_method(block$object, pkg_generics) parts <- strsplit(block$object, "\\.")[[1]] - if (length(parts) >= 2) { + if (!is.null(s3_info)) { + s3methods <- c(s3methods, list(s3_info)) + } else if (length(parts) >= 2) { s3methods <- c(s3methods, list(list( generic = parts[1], class = paste(parts[-1], collapse = ".") ))) + } else { + warning("@exportS3Method on ", block$object, " at ", + basename(block$file), ":", block$line, + " names no generic and class, and the object ", + "name has no dot to infer them from; nothing ", + "was registered.", call. = FALSE) } } } @@ -149,13 +181,20 @@ generate_namespace <- function(blocks) { if (length(exports) > 0 || length(export_classes) > 0) { lines <- c(lines, "") } + # A block carrying both @export and @exportS3Method registers once + s3_keys <- vapply(s3methods, function(x) paste(x$generic, x$class), + character(1)) + s3methods <- s3methods[!duplicated(s3_keys)] s3methods <- s3methods[order( vapply(s3methods, function(x) paste(x$generic, x$class), character(1)) )] for (s3m in s3methods) { gen <- s3m$generic cls <- s3m$class - if (!grepl("^[a-zA-Z._][a-zA-Z0-9._]*$", gen)) { + # pkg::generic stays bare: quoted, R would look for a generic + # literally named "pkg::generic" + if (!grepl("^([a-zA-Z][a-zA-Z0-9.]*::)?[a-zA-Z._][a-zA-Z0-9._]*$", + gen)) { gen <- paste0('"', gen, '"') } if (!grepl("^[a-zA-Z._][a-zA-Z0-9._]*$", cls)) { diff --git a/R/rd.R b/R/rd.R index 34fb541..0b93213 100644 --- a/R/rd.R +++ b/R/rd.R @@ -12,7 +12,7 @@ RD_MARKER <- "% tinyrox says don't edit this manually, but it can't stop you!" #' passed through verbatim as Rd (tinyrox does no markdown parsing), matching #' how the title-only macros elsewhere treat hand-written Rd markup. #' -#' @param sections List of `list(title=, content=)` from parse_tags(). +#' @param sections List of \code{list(title=, content=)} from parse_tags(). #' @return Character vector of Rd lines (empty when there are no sections). #' @keywords internal render_sections <- function(sections) { @@ -25,6 +25,47 @@ render_sections <- function(sections) { lines } +#' Formals a Set of @param Tags Documents +#' +#' One @param can document several formals at once ("@param a,b desc", +#' rendered as one Rd item named "a,b"). The Rd keeps the joined name; the +#' undocumented-parameter check needs the individual ones. +#' +#' @param params Named list of parameter descriptions from parse_tags(). +#' @return Character vector of individual formal names. +#' @keywords internal +documented_params <- function(params) { + if (length(params) == 0) { + return(character()) + } + trimws(unlist(strsplit(names(params), ",", fixed = TRUE))) +} + +#' Order @param Names by the Function's Formals +#' +#' Formals order first, then anything matching no formal in the order +#' written. A joined name ("a,b") sorts at its earliest formal. +#' +#' @param param_names Character vector of @param names. +#' @param formal_names Character vector of formal argument names. +#' @return param_names, reordered. +#' @keywords internal +order_params <- function(param_names, formal_names) { + if (length(formal_names) == 0 || length(param_names) == 0) { + return(param_names) + } + pos <- vapply(param_names, function(p) { + parts <- trimws(strsplit(p, ",", fixed = TRUE)[[1]]) + hit <- match(parts, formal_names) + if (all(is.na(hit))) { + NA_integer_ + } else { + min(hit, na.rm = TRUE) + } + }, integer(1)) + param_names[order(pos, na.last = TRUE)] +} + #' Generate Rd File Content #' #' @param tags Parsed tags from parse_tags(). @@ -78,12 +119,7 @@ generate_rd <- function(tags, formals = NULL, source_file = NULL, } else { formal_names <- character() } - param_order <- if (length(formal_names) > 0) { - c(intersect(formal_names, names(tags$params)), - setdiff(names(tags$params), formal_names)) - } else { - names(tags$params) - } + param_order <- order_params(names(tags$params), formal_names) for (i in seq_along(param_order)) { param <- param_order[i] desc_text <- escape_rd(tags$params[[param]]) @@ -541,8 +577,8 @@ write_rd <- function(content, name, path = ".") { #' #' rd_filename() is supposed to make every name portable. This says so #' out loud rather than trusting it, because the failure is silent in -#' both directions: `R CMD build` drops the file without an error, and -#' the `R CMD check` that follows examines a tarball the file is no +#' both directions: \code{R CMD build} drops the file without an error, and +#' the \code{R CMD check} that follows examines a tarball the file is no #' longer in. #' #' @param files Character vector of paths just written. @@ -561,9 +597,9 @@ check_rd_filenames <- function(files) { #' What a Topic Is Called on Disk #' #' Replacements applied to a topic name to make a file name, longest -#' key first so `[<-` is read before `[`. This is the table roxygen2 -#' uses, so a package moving between the two does not churn its `man` -#' directory, and the two names tinyrox already handled -- `%||%` and +#' key first so \code{[<-} is read before \code{[}. This is the table roxygen2 +#' uses, so a package moving between the two does not churn its \code{man} +#' directory, and the two names tinyrox already handled -- \code{%||%} and #' a leading dot -- come out exactly as they did. #' #' @keywords internal @@ -582,16 +618,16 @@ RD_NAME_SUBS <- c("[<-" = "-subset-", "[" = "-sub-", "<-" = "-set-", #' Turn a Topic Name into a Portable File Name #' -#' A topic can be called anything R can name, and `$.foo`, `[[.foo` -#' and `$<-.foo` are ordinary S3 methods. Those characters are not -#' legal in a file name on every platform R runs on, and `R CMD build` +#' A topic can be called anything R can name, and \code{$.foo}, \code{[[.foo} +#' and \code{$<-.foo} are ordinary S3 methods. Those characters are not +#' legal in a file name on every platform R runs on, and \code{R CMD build} #' does not warn and carry on: it prints "excluding invalid files" and #' **drops them from the tarball**. The package then installs with no -#' documentation for those methods, and `R CMD check` reports nothing, +#' documentation for those methods, and \code{R CMD check} reports nothing, #' because it checks the tarball the files are no longer in. Green, #' and missing its docs. #' -#' The `\\name{}` inside the file keeps the real topic, so help still +#' The \code{\\name{}} inside the file keeps the real topic, so help still #' resolves under it; only the file on disk is renamed. #' #' @param name Topic name. @@ -761,10 +797,7 @@ generate_rd_grouped <- function(topic, entries, all_tags, if (length(merged_params) > 0) { # Order: formals order (unique, preserving first occurrence), then any extras all_formal_names <- unique(all_formal_names) - param_order <- c( - intersect(all_formal_names, names(merged_params)), - setdiff(names(merged_params), all_formal_names) - ) + param_order <- order_params(names(merged_params), all_formal_names) lines <- c(lines, "\\arguments{") for (i in seq_along(param_order)) { param <- param_order[i] @@ -977,7 +1010,7 @@ generate_all_rd <- function(blocks, path = ".", cran_check = TRUE) { block$type %in% c("function", "nn_module") && !is.null(block$formals)) { formal_names <- block$formals$names - undoc <- setdiff(formal_names, names(tags$params)) + undoc <- setdiff(formal_names, documented_params(tags$params)) undoc <- setdiff(undoc, "...") if (length(undoc) > 0) { warning("Undocumented parameters in ", tags$name, ": ", @@ -1004,7 +1037,8 @@ generate_all_rd <- function(blocks, path = ".", cran_check = TRUE) { etags <- resolve_inherit_params(etags, all_tags, entry$block$formals) } - documented <- c(documented, names(etags$params)) + documented <- c(documented, + documented_params(etags$params)) } documented <- unique(documented) for (entry in entries) { @@ -1151,7 +1185,7 @@ resolve_inherit_params <- function(tags, all_tags, formals) { } # Only inherit if not already documented - if (param_name %in% names(tags$params)) { + if (param_name %in% documented_params(tags$params)) { next } @@ -1166,7 +1200,7 @@ resolve_inherit_params <- function(tags, all_tags, formals) { #' Resolve Parameters from External Package Rd Files #' #' Reads an installed package's Rd file to extract parameter documentation -#' for use with `@inheritParams pkg::function`. +#' for use with \code{@inheritParams pkg::function}. #' #' @param source_name Character string like "base::cat" or "stats::lm". #' @return Named list of parameter descriptions, or empty list on failure. diff --git a/R/tags.R b/R/tags.R index ebc30bf..d781ddd 100644 --- a/R/tags.R +++ b/R/tags.R @@ -3,7 +3,7 @@ #' @keywords internal SUPPORTED_DOC_TAGS <- c("title", "description", "details", "param", "return", "returns", "value", "examples", - "example", "seealso", "references", "aliases", + "examplesIf", "example", "seealso", "references", "aliases", "keywords", "family", "name", "rdname", "noRd", "inheritParams", "section", "author", "format", "source") @@ -159,6 +159,22 @@ parse_tags <- function(lines, object_name, file = NULL, line_num = NULL) { result } +#' Add Example Code to What a Block Already Has +#' +#' A block can carry several example tags (plain and conditional); they +#' all go on the one page, in the order written. +#' +#' @param existing Examples collected so far, or NULL. +#' @param code Example code to add. +#' @return The combined examples text. +#' @keywords internal +append_examples <- function(existing, code) { + if (is.null(existing) || !nzchar(trimws(existing))) { + return(code) + } + paste(existing, code, sep = "\n") +} + #' Save a Parsed Tag Value #' #' @param result Current result list to update. @@ -218,10 +234,30 @@ save_tag <- function(result, tag, arg, accumulator, file, line_num) { "example" = { # Examples are verbatim - include the arg if present if (!is.null(arg)) { - result$examples <- paste(c(arg, accumulator), collapse = "\n") + code <- paste(c(arg, accumulator), collapse = "\n") } else { - result$examples <- paste(accumulator, collapse = "\n") + code <- paste(accumulator, collapse = "\n") } + result$examples <- append_examples(result$examples, code) + }, + "examplesIf" = { + # @examplesIf cond: the first line is the condition, the rest the + # examples. Rendered as roxygen2 does, with the guard hidden in + # \dontshow{} so the help page shows only the example code. + body <- accumulator + while (length(body) > 0 && !nzchar(trimws(body[length(body)]))) { + body <- body[-length(body)] + } + code <- paste(body, collapse = "\n") + if (is.null(arg)) { + warning("@examplesIf has no condition; its examples run ", + "unconditionally.", call. = FALSE) + } else { + code <- paste0("\\dontshow{if (", arg, + ") withAutoprint(\\{ # examplesIf}\n", code, + "\n\\dontshow{\\}) # examplesIf}") + } + result$examples <- append_examples(result$examples, code) }, "seealso" = { result$seealso <- value @@ -266,8 +302,15 @@ save_tag <- function(result, tag, arg, accumulator, file, line_num) { if (length(parts) >= 2) { result$exportS3Method <- list(generic = parts[1], class = parts[2]) } else if (length(parts) == 1 && nchar(parts[1]) > 0) { - # Try to infer from object name (e.g., print.foo) + # One argument. "pkg::generic" names another package's generic + # and the class comes from the object name; anything else is + # inferred from the object name alone (e.g., print.foo). result$exportS3Method <- list(explicit = parts[1]) + } else { + # Bare tag: generic and class both come from the object name. + # strsplit("") yields character(0), so without this branch the + # tag vanished and the method was never registered (#37). + result$exportS3Method <- list(explicit = TRUE) } }, "import" = { diff --git a/README.md b/README.md index c72ce20..79d149d 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,11 @@ check_cran() | `@title` | One-line title | | `@description` | Short description | | `@details` | Longer description | -| `@param name` | Parameter documentation | +| `@param name` | Parameter documentation (`@param a,b` covers both) | | `@return` | Return value | | `@value` | Alias for `@return` | | `@examples` | Code examples (verbatim) | +| `@examplesIf cond` | Examples that run only when `cond` is true | | `@seealso` | Cross-references | | `@references` | Citations | | `@section Title:` | Custom section (package-level docs only) | @@ -56,7 +57,7 @@ check_cran() | Tag | Effect | |-----|--------| | `@export` | `export()` | -| `@exportS3Method generic class` | `S3method()` | +| `@exportS3Method generic class` | `S3method()`; bare, generic and class come from the function name; `pkg::generic` registers for another package's generic | | `@import pkg` | `import()` | | `@importFrom pkg sym1 sym2` | `importFrom()` | | `@useDynLib pkg` | `useDynLib()` | @@ -128,6 +129,9 @@ Issues detected: - `setwd()` without `on.exit()` restoration - Hardcoded `set.seed()` without parameter - Exported functions without examples; `\dontrun{}` overuse +- Markdown that ships verbatim: backticked terms and `[fn()]` links. Write + `\code{x}` and `\code{\link{fn}}`, or silence the check with + `options(tinyrox.markdown_check = FALSE)` ## Philosophy diff --git a/fyi.md b/fyi.md deleted file mode 100644 index 85fce36..0000000 --- a/fyi.md +++ /dev/null @@ -1,1102 +0,0 @@ - - - -# fyi: tinyrox - -## Exported Functions (tinyrox::) - -| Function | Arguments | -|----------|-----------| -| `check_code_cran` | path | -| `check_cran` | path | -| `check_description_cran` | path, fix | -| `check_examples_cran` | path | -| `clean` | path, namespace | -| `document` | path, namespace, cran_check | -| `fix_description_cran` | path, backup | - - -## Internal Functions (tinyrox:::) - -| Function | Arguments | -|----------|-----------| -| `check_code_lines` | lines, filename | -| `check_webservice_links` | description, packages | -| `detect_s3_method` | name | -| `escape_rd` | text | -| `escape_regex` | x | -| `extract_formals` | code | -| `extract_function_name` | line | -| `extract_nn_module_formals` | code | -| `find_exports_without_examples` | lines | -| `find_unquoted_names` | text, names | -| `format_object_info` | name, pkg_path | -| `format_usage` | name, args | -| `generate_all_rd` | blocks, path | -| `generate_data_rd` | tags, source_file, format_string | -| `generate_namespace` | blocks | -| `generate_package_rd` | tags, pkg_name, source_file | -| `generate_rd` | tags, formals, source_file | -| `generate_rd_grouped` | topic, entries, all_tags | -| `get_dependency_packages` | desc | -| `get_maintainer_from_desc` | path | -| `get_package_name` | path | -| `parse_file` | file | -| `parse_formals_text` | text | -| `parse_object_definition` | text, file, line_num | -| `parse_package` | path | -| `parse_tags` | lines, object_name, file, line_num | -| `quote_names_in_text` | text, names | -| `resolve_inherit_params` | tags, all_tags, formals | -| `save_tag` | result, tag, arg, accumulator, file, line_num | -| `wrap_text` | text, width | -| `write_namespace` | content, path, mode | -| `write_rd` | content, name, path | - - -## Options (tinyrox) - -| Option | File | Type | -|--------|------|------| -| `warn` | cran.R | set | - - -# Documentation: tinyrox - -## check_code_cran - -### Check R Code for CRAN Issues - -#### Description - -Scans R files for common CRAN policy violations. - -#### Usage - -```r -check_code_cran(path = ".") -``` - -#### Arguments - -- **`path`**: Path to package root directory - -#### Value - -List with issues found - -#### Examples - -```r -check_code_cran() -check_code_cran("path/to/package") -``` - - -## check_code_lines - -### Check Code Lines for Issues - -#### Description - -Check Code Lines for Issues - -#### Usage - -```r -check_code_lines(lines, filename) -``` - -#### Arguments - -- **`lines`**: Character vector of code lines -- **`filename`**: Filename for reporting - -#### Value - -List of issues - - -## check_cran - -### Full CRAN Compliance Check - -#### Description - -Runs all CRAN compliance checks (DESCRIPTION + code). - -#### Usage - -```r -check_cran(path = ".") -``` - -#### Arguments - -- **`path`**: Path to package root directory - -#### Value - -List with all issues - -#### Examples - -```r -check_cran() -check_cran("path/to/package") -``` - - -## check_description_cran - -### Check DESCRIPTION for CRAN Compliance - -#### Description - -Validates Title and Description fields for common CRAN issues: -unquoted package names, missing web service links, etc. - -#### Usage - -```r -check_description_cran(path = ".", fix = FALSE) -``` - -#### Arguments - -- **`path`**: Path to package root directory -- **`fix`**: If TRUE, return fixed text. If FALSE, just warn. - -#### Value - -List with validation results and optionally fixed text - -#### Examples - -```r -check_description_cran() -check_description_cran("path/to/package") -``` - - -## check_examples_cran - -### Check for Missing Examples - -#### Description - -Identifies exported functions that lack examples in their documentation. - -#### Usage - -```r -check_examples_cran(path = ".") -``` - -#### Arguments - -- **`path`**: Path to package root directory - -#### Value - -Character vector of function names missing examples - -#### Examples - -```r -check_examples_cran() -check_examples_cran("path/to/package") -``` - - -## check_webservice_links - -### Check for Missing Web Service Links - -#### Description - -Checks if packages that typically use web services have -corresponding URLs in the Description. - -#### Usage - -```r -check_webservice_links(description, packages) -``` - -#### Arguments - -- **`description`**: Description text -- **`packages`**: Package names from dependencies - -#### Value - -Named list of packages missing links (name = URL) - - -## clean - -### Clean Generated Files - -#### Description - -Removes all Rd files from man/ directory. - -#### Usage - -```r -clean(path = ".", namespace = FALSE) -``` - -#### Arguments - -- **`path`**: Path to package root directory. -- **`namespace`**: Also remove NAMESPACE? Default FALSE. - -#### Examples - -```r -clean() -clean(namespace = TRUE) -``` - - -## detect_s3_method - -### Detect S3 Method from Function Name - -#### Description - -Checks if a function name follows the generic.class pattern where -generic is a known S3 generic function. - -#### Usage - -```r -detect_s3_method(name) -``` - -#### Arguments - -- **`name`**: Function name to check. - -#### Value - -List with generic and class components, or NULL if not an S3 method. - - -## document - -### Generate Documentation for an R Package - -#### Description - -Main function for tinyrox. Parses R source files for documentation -comments and generates Rd files and NAMESPACE. - -#### Usage - -```r -document( - path = ".", - namespace = c("overwrite", "append", "none"), - cran_check = TRUE -) -``` - -#### Arguments - -- **`path`**: Path to package root directory. Default is current directory. -- **`namespace`**: How to handle NAMESPACE generation. One of -- **"overwrite"**: Fully regenerate NAMESPACE (default) -- **"append"**: Insert between ## tinyrox start/end markers -- **"none"**: Don't modify NAMESPACE -- **`cran_check`**: Run CRAN compliance checks (DESCRIPTION quoting, -web service links, code issues, missing examples). Default TRUE. - -#### Value - -Invisibly returns a list with: - - rd_files: character vector of generated Rd file paths - - namespace: path to NAMESPACE file (or NULL if mode="none") - -#### Examples - -```r -# Document current package -document() - -# Document with append mode for NAMESPACE -document(namespace = "append") - -# Document without modifying NAMESPACE -document(namespace = "none") - -# Skip CRAN compliance check -document(cran_check = FALSE) -``` - - -## escape_rd - -### Escape Special Characters for Rd - -#### Description - -Escapes special characters for Rd format, but detects and preserves -existing Rd markup (like \describe, \item, \code, etc.). - -#### Usage - -```r -escape_rd(text) -``` - -#### Arguments - -- **`text`**: Text to escape. - -#### Value - -Escaped text. - - -## escape_regex - -### Escape Regex Special Characters - -#### Description - -Escape Regex Special Characters - -#### Usage - -```r -escape_regex(x) -``` - -#### Arguments - -- **`x`**: String to escape - -#### Value - -Escaped string safe for use in regex - - -## extract_formals - -### Extract Function Formals from Code - -#### Description - -Extract Function Formals from Code - -#### Usage - -```r -extract_formals(code) -``` - -#### Arguments - -- **`code`**: Code starting with "function(" - -#### Value - -List with 'names' (argument names) and 'usage' (formatted for Rd). - - -## extract_function_name - -### Extract Function Name from Definition Line - -#### Description - -Extract Function Name from Definition Line - -#### Usage - -```r -extract_function_name(line) -``` - -#### Arguments - -- **`line`**: Code line potentially containing function definition - -#### Value - -Function name or NULL - - -## extract_nn_module_formals - -### Extract Formals from torch nn_module - -#### Description - -Extracts the formals from the initialize method of an nn_module definition. - -#### Usage - -```r -extract_nn_module_formals(code) -``` - -#### Arguments - -- **`code`**: Code starting with "nn_module(" or "torch::nn_module(" - -#### Value - -List with 'names' (argument names) and 'usage' (formatted for Rd), - or NULL if initialize method not found. - - -## find_exports_without_examples - -### Find Exported Functions Without Examples - -#### Description - -Parses R file content to find @export tags without @examples. - -#### Usage - -```r -find_exports_without_examples(lines) -``` - -#### Arguments - -- **`lines`**: Character vector of file lines - -#### Value - -Character vector of function names missing examples - - -## find_unquoted_names - -### Find Unquoted Names in Text - -#### Description - -Finds package/software names that appear without single quotes. - -#### Usage - -```r -find_unquoted_names(text, names) -``` - -#### Arguments - -- **`text`**: Text to search -- **`names`**: Names to look for - -#### Value - -Character vector of unquoted names found - - -## fix_description_cran - -### Fix DESCRIPTION File - -#### Description - -Automatically fixes common CRAN issues in DESCRIPTION. - -#### Usage - -```r -fix_description_cran(path = ".", backup = TRUE) -``` - -#### Arguments - -- **`path`**: Path to package root directory -- **`backup`**: Create backup file? Default TRUE. - -#### Value - -Invisibly returns TRUE if changes were made - -#### Examples - -```r -fix_description_cran() -fix_description_cran(backup = FALSE) -``` - - -## format_object_info - -### Format Object Info for Data Documentation - -#### Description - -Generates the format description string for a data object. -Tries to load the package namespace to inspect the object. - -#### Usage - -```r -format_object_info(name, pkg_path) -``` - -#### Arguments - -- **`name`**: Object name. -- **`pkg_path`**: Package root path. - -#### Value - -Format string or NULL if object cannot be inspected. - - -## format_usage - -### Format Usage Line - -#### Description - -Formats the function usage, wrapping to multiple lines if needed. -Follows roxygen2 style: each argument on its own line if total > 80 chars. -S3 methods use the \method syntax. - -#### Usage - -```r -format_usage(name, args) -``` - -#### Arguments - -- **`name`**: Function name. -- **`args`**: Character vector of arguments with defaults. - -#### Value - -Formatted usage string. - - -## generate_all_rd - -### Generate All Rd Files for a Package - -#### Description - -Generate All Rd Files for a Package - -#### Usage - -```r -generate_all_rd(blocks, path = ".") -``` - -#### Arguments - -- **`blocks`**: List of documentation blocks from parse_package(). -- **`path`**: Package root path. - -#### Value - -Character vector of generated file paths. - - -## generate_data_rd - -### Generate Rd File Content for Data Objects - -#### Description - -Generate Rd File Content for Data Objects - -#### Usage - -```r -generate_data_rd(tags, source_file = NULL, format_string = NULL) -``` - -#### Arguments - -- **`tags`**: Parsed tags from parse_tags(). -- **`source_file`**: Source file path (for header comment). -- **`format_string`**: Format description (e.g., "An object of class list of length 3"). - -#### Value - -Character string of Rd content. - - -## generate_namespace - -### Generate NAMESPACE Content - -#### Description - -Generate NAMESPACE Content - -#### Usage - -```r -generate_namespace(blocks) -``` - -#### Arguments - -- **`blocks`**: List of documentation blocks from parse_package(). - -#### Value - -Character string of NAMESPACE content. - - -## generate_package_rd - -### Generate Package Documentation Rd - -#### Description - -Generates Rd content for package documentation ("_PACKAGE" directive). - -#### Usage - -```r -generate_package_rd(tags, pkg_name, source_file) -``` - -#### Arguments - -- **`tags`**: Parsed tags from the documentation block. -- **`pkg_name`**: Package name. -- **`source_file`**: Source file path. - -#### Value - -Character string of Rd content. - - -## generate_rd - -### Generate Rd File Content - -#### Description - -Generate Rd File Content - -#### Usage - -```r -generate_rd(tags, formals = NULL, source_file = NULL) -``` - -#### Arguments - -- **`tags`**: Parsed tags from parse_tags(). -- **`formals`**: Character vector of formal argument names (for functions). -- **`source_file`**: Source file path (for header comment). - -#### Value - -Character string of Rd content. - - -## get_dependency_packages - -### Extract Package Names from Dependencies - -#### Description - -Parses Imports, Suggests, Depends, and Enhances fields. - -#### Usage - -```r -get_dependency_packages(desc) -``` - -#### Arguments - -- **`desc`**: DESCRIPTION matrix from read.dcf() - -#### Value - -Character vector of package names - - -## get_maintainer_from_desc - -### Get Maintainer Info from DESCRIPTION - -#### Description - -Reads Authors@R from DESCRIPTION and formats maintainer for Rd. - -#### Usage - -```r -get_maintainer_from_desc(path) -``` - -#### Arguments - -- **`path`**: Package root path. - -#### Value - -Formatted author string or NULL. - - -## get_package_name - -### Get Package Name from DESCRIPTION - -#### Description - -Get Package Name from DESCRIPTION - -#### Usage - -```r -get_package_name(path) -``` - -#### Arguments - -- **`path`**: Package root path. - -#### Value - -Package name. - - -## KNOWN_S3_GENERICS - -### Known S3 Generic Functions - -#### Description - -List of base R S3 generics for auto-detection when @export is used. - -#### Usage - -```r -KNOWN_S3_GENERICS -``` - - -## parse_file - -### Parse R Files for Documentation Blocks - -#### Description - -Extracts documentation comment blocks and their associated objects -from R source files. - -#### Usage - -```r -parse_file(file) -``` - -#### Arguments - -- **`file`**: Path to an R source file. - -#### Value - -A list of documentation blocks, each with components: - - lines: character vector of comment lines (without #') - - object: name of the documented object - - type: "function", "data", or "other" - - formals: for functions, the formal arguments - - -## parse_formals_text - -### Parse Formals Text - -#### Description - -Parse Formals Text - -#### Usage - -```r -parse_formals_text(text) -``` - -#### Arguments - -- **`text`**: Text containing function arguments. - -#### Value - -List with 'names' (argument names) and 'usage' (formatted for Rd). - - -## parse_object_definition - -### Parse Object Definition - -#### Description - -Identifies the object being defined from code text (may be multi-line). - -#### Usage - -```r -parse_object_definition(text, file, line_num) -``` - -#### Arguments - -- **`text`**: The code text (may span multiple lines). -- **`file`**: The source file (for error messages). -- **`line_num`**: The line number (for error messages). - -#### Value - -A list with name, type, and formals, or NULL if not a definition. - - -## parse_package - -### Parse All R Files in a Package - -#### Description - -Parse All R Files in a Package - -#### Usage - -```r -parse_package(path = ".") -``` - -#### Arguments - -- **`path`**: Path to package root. - -#### Value - -List of all documentation blocks from all R files. - - -## parse_tags - -### Parse Tags from Documentation Lines - -#### Description - -Parse Tags from Documentation Lines - -#### Usage - -```r -parse_tags(lines, object_name, file = NULL, line_num = NULL) -``` - -#### Arguments - -- **`lines`**: Character vector of documentation lines (without #'). -- **`object_name`**: Name of the documented object. -- **`file`**: Source file (for error messages). -- **`line_num`**: Starting line number (for error messages). - -#### Value - -A list with parsed tag values. - - -## quote_names_in_text - -### Quote Names in Text - -#### Description - -Wraps unquoted package/software names in single quotes. - -#### Usage - -```r -quote_names_in_text(text, names) -``` - -#### Arguments - -- **`text`**: Text to modify -- **`names`**: Names to quote - -#### Value - -Modified text with names quoted - - -## resolve_inherit_params - -### Resolve @inheritParams Tags - -#### Description - -Copies parameter documentation from source functions to the current function. -Only inherits params that are: (1) in the current function's formals, and -(2) not already documented in the current function. - -#### Usage - -```r -resolve_inherit_params(tags, all_tags, formals) -``` - -#### Arguments - -- **`tags`**: Current function's parsed tags. -- **`all_tags`**: Named list of all parsed tags (name -> tags). -- **`formals`**: Current function's formals (list with names and usage). - -#### Value - -Updated tags with inherited params merged in. - - -## save_tag - -### Save a Parsed Tag Value - -#### Description - -Save a Parsed Tag Value - -#### Usage - -```r -save_tag(result, tag, arg, accumulator, file, line_num) -``` - -#### Arguments - -- **`result`**: Current result list to update. -- **`tag`**: Tag name (e.g., "param", "return"). -- **`arg`**: First-line argument after the tag. -- **`accumulator`**: Continuation lines for the tag. -- **`file`**: Source file path (for error messages). -- **`line_num`**: Line number (for error messages). - - -## SUPPORTED_DOC_TAGS - -### Supported Documentation Tags - -#### Description - -Supported Documentation Tags - -#### Usage - -```r -SUPPORTED_DOC_TAGS -``` - - -## SUPPORTED_NS_TAGS - -### Supported Namespace Tags - -#### Description - -Supported Namespace Tags - -#### Usage - -```r -SUPPORTED_NS_TAGS -``` - - -## SUPPORTED_TAGS - -### All Supported Tags - -#### Description - -All Supported Tags - -#### Usage - -```r -SUPPORTED_TAGS -``` - - -## wrap_text - -### Wrap Text to Width - -#### Description - -Wraps text to specified width, preserving words. - -#### Usage - -```r -wrap_text(text, width = 72) -``` - -#### Arguments - -- **`text`**: Text to wrap. -- **`width`**: Maximum line width. - -#### Value - -Wrapped text with newlines. - - -## write_namespace - -### Write NAMESPACE File - -#### Description - -Write NAMESPACE File - -#### Usage - -```r -write_namespace(content, path = ".", mode = "overwrite") -``` - -#### Arguments - -- **`content`**: NAMESPACE content string. -- **`path`**: Package root path. -- **`mode`**: Either "overwrite" or "append". - - -## write_rd - -### Write Rd File - -#### Description - -Write Rd File - -#### Usage - -```r -write_rd(content, name, path = ".") -``` - -#### Arguments - -- **`content`**: Rd content string. -- **`name`**: Topic name. -- **`path`**: Package root path. - - diff --git a/inst/tinytest/test_cran.R b/inst/tinytest/test_cran.R index 810410c..2b1b755 100644 --- a/inst/tinytest/test_cran.R +++ b/inst/tinytest/test_cran.R @@ -74,3 +74,102 @@ test_long_lines <- function() { expect_equal(length(tinyrox:::find_long_example_lines(lines3, "test.R")), 0) } test_long_lines() + +# Build a throwaway package from named file contents +make_pkg <- function(files) { + pkg <- tempfile("pkg") + dir.create(file.path(pkg, "R"), recursive = TRUE) + writeLines(c("Package: mypkg", "Title: Test", "Version: 0.0.1"), + file.path(pkg, "DESCRIPTION")) + for (f in names(files)) { + writeLines(files[[f]], file.path(pkg, "R", f)) + } + pkg +} + +# Missing-examples check runs per topic, not per block (#41) +test_examples_per_topic <- function() { + pkg <- make_pkg(list("f.R" = c( + "#' Two grouped functions", + "#'", + "#' @param a,b Whole numbers.", + "#' @return A number.", + "#' @examples", + "#' g(1, 2)", + "#' @rdname grouped", + "#' @export", + "g <- function(a, b) a + b", + "", + "#' @rdname grouped", + "#' @export", + "h <- function(a, b) a - b", + "", + "#' @export", + "print.foo <- function(x, ...) cat(\"foo\\n\")", + "", + "#' No examples here", + "#' @export", + "bare <- function() 1" + ))) + on.exit(unlink(pkg, recursive = TRUE)) + + blocks <- tinyrox:::parse_package(pkg) + expect_equal(tinyrox:::find_exports_without_examples(blocks), "bare") + expect_warning(res <- tinyrox::check_examples_cran(pkg), + "without examples: bare$") + expect_equal(res, "bare") + + # Comma-separated @param names document each formal (#40); the only + # undocumented-parameter warning here would be a false one. + w <- character() + withCallingHandlers( + tinyrox::document(pkg, cran_check = TRUE, silent = TRUE), + warning = function(cnd) { + w <<- c(w, conditionMessage(cnd)) + invokeRestart("muffleWarning") + }, + message = function(cnd) invokeRestart("muffleMessage") + ) + expect_false(any(grepl("Undocumented parameters", w))) + rd <- readLines(file.path(pkg, "man", "grouped.Rd")) + expect_true(any(grepl("\\item{a,b}{Whole numbers.}", rd, fixed = TRUE))) +} +test_examples_per_topic() + +# Markdown that ships verbatim is reported once per file (#42) +test_markdown <- function() { + lines <- c( + "#' Title with `code`", # 1: flagged + "#'", + "#' See [other_fn()] for more.", # 3: flagged + "#' @param x A \\code{`[`} method.", # 4: inside \code{}, fine + "#' @param y Index x[i] directly.", # 5: plain brackets, fine + "#' @examples", + "#' `%||%`(1, 2)", # 7: example code, fine + "#' @export", + "f <- function(x, y) 1", + "", + "#' Hidden `thing`", # 11: noRd block, fine + "#' @noRd", + "g <- function() 1" + ) + expect_equal(tinyrox:::find_markdown_lines(lines), c(1L, 3L)) + expect_equal(tinyrox:::find_markdown_lines("x <- 1"), integer()) + + pkg <- make_pkg(list("f.R" = lines, "clean.R" = c( + "#' Clean", + "#' @export", + "k <- function() 1" + ))) + on.exit(unlink(pkg, recursive = TRUE)) + + expect_warning(res <- tinyrox:::check_markdown_rd(pkg), + "f.R: 2 line\\(s\\), first at f.R:1") + expect_equal(names(res), "f.R") + + old <- options(tinyrox.markdown_check = FALSE) + on.exit(options(old), add = TRUE) + expect_silent(res_off <- tinyrox:::check_markdown_rd(pkg)) + expect_equal(length(res_off), 0) +} +test_markdown() diff --git a/inst/tinytest/test_namespace.R b/inst/tinytest/test_namespace.R index e03f059..08ca9a3 100644 --- a/inst/tinytest/test_namespace.R +++ b/inst/tinytest/test_namespace.R @@ -200,3 +200,95 @@ expect_silent(tinyrox:::write_namespace(keep_content, tmp_pkg)) unlink(file.path(tmp_pkg, "NAMESPACE")) expect_silent(tinyrox:::write_namespace(new_content, tmp_pkg)) unlink(tmp_pkg, recursive = TRUE) + +# Bare @exportS3Method infers generic and class from the object name (#37) +blocks_bare <- list( + list( + lines = c("@exportS3Method"), + object = "print.myclass", + type = "function", + formals = list(names = c("x", "..."), usage = c("x", "...")), + file = "test.R", + line = 1 + ), + list( + lines = c("@exportS3Method"), + object = "as.data.frame.myclass", + type = "function", + formals = list(names = "x", usage = "x"), + file = "test.R", + line = 5 + ), + list( + lines = c("@exportS3Method"), + object = "poll.my_client", + type = "function", + formals = list(names = "x", usage = "x"), + file = "test.R", + line = 9 + ) +) +ns_bare <- tinyrox:::generate_namespace(blocks_bare) +expect_true(grepl("S3method(print,myclass)", ns_bare, fixed = TRUE)) +expect_true(grepl("S3method(as.data.frame,myclass)", ns_bare, fixed = TRUE)) +expect_true(grepl("S3method(poll,my_client)", ns_bare, fixed = TRUE)) + +# A bare tag on a name with no dot registers nothing and says so +blocks_nodot <- list( + list( + lines = c("@exportS3Method"), + object = "nodot", + type = "function", + formals = list(names = "x", usage = "x"), + file = "test.R", + line = 1 + ) +) +expect_warning(ns_nodot <- tinyrox:::generate_namespace(blocks_nodot), + "nothing was registered") +expect_false(grepl("S3method", ns_nodot)) + +# @export plus @exportS3Method on one block registers the method once +blocks_both <- list( + list( + lines = c("Print it", "@export", "@exportS3Method"), + object = "print.myclass", + type = "function", + formals = list(names = c("x", "..."), usage = c("x", "...")), + file = "test.R", + line = 1 + ) +) +ns_both <- tinyrox:::generate_namespace(blocks_both) + +# @exportS3Method pkg::generic keeps the prefix for delayed registration +s3_block <- function(tag, object) { + list(list(lines = tag, object = object, type = "function", + formals = list(names = "x", usage = "x"), + file = "test.R", line = 1)) +} +ns_pkg <- tinyrox:::generate_namespace( + s3_block("@exportS3Method knitr::knit_print", "knit_print.my.class")) +expect_true(grepl("S3method(knitr::knit_print,my.class)", ns_pkg, fixed = TRUE)) +expect_false(grepl("\"", ns_pkg, fixed = TRUE)) + +ns_pkg2 <- tinyrox:::generate_namespace( + s3_block("@exportS3Method knitr::knit_print foo", "anything")) +expect_true(grepl("S3method(knitr::knit_print,foo)", ns_pkg2, fixed = TRUE)) + +# R reads the directive as a delayed registration +ns_pkg_file <- file.path(tempfile("nsdir"), "NAMESPACE") +dir.create(dirname(ns_pkg_file)) +writeLines(ns_pkg, ns_pkg_file) +parsed_ns <- parseNamespaceFile(basename(dirname(ns_pkg_file)), + dirname(dirname(ns_pkg_file))) +expect_equal(unname(parsed_ns$S3methods[1, c(1, 2, 4)]), + c("knit_print", "my.class", "knitr")) +unlink(dirname(ns_pkg_file), recursive = TRUE) + +# The object name must start with the generic for the class to be known +expect_warning(ns_pkg_bad <- tinyrox:::generate_namespace( + s3_block("@exportS3Method knitr::knit_print", "print.foo")), + "nothing was registered") +expect_false(grepl("S3method", ns_pkg_bad)) +expect_equal(lengths(regmatches(ns_both, gregexpr("S3method\\(", ns_both))), 1L) diff --git a/inst/tinytest/test_rd.R b/inst/tinytest/test_rd.R index a5102f3..4d0a962 100644 --- a/inst/tinytest/test_rd.R +++ b/inst/tinytest/test_rd.R @@ -461,3 +461,11 @@ test_data_directive_document <- function() { expect_false(any(grepl("measurements", ns, fixed = TRUE))) } test_data_directive_document() + +# Comma-separated @param names sort at their earliest formal (#40) +expect_equal(tinyrox:::order_params(c("z", "b,c", "a"), c("a", "b", "c")), + c("a", "b,c", "z")) +expect_equal(tinyrox:::order_params(c("y", "x"), character()), c("y", "x")) +expect_equal(tinyrox:::documented_params(list("a,b" = "d", c = "e")), + c("a", "b", "c")) +expect_equal(tinyrox:::documented_params(list()), character()) diff --git a/inst/tinytest/test_tags.R b/inst/tinytest/test_tags.R index beec970..9425025 100644 --- a/inst/tinytest/test_tags.R +++ b/inst/tinytest/test_tags.R @@ -122,3 +122,34 @@ expect_equal(tags_returns$return, "The result value") lines_return <- c("Title", "@return The result value") tags_return <- tinyrox:::parse_tags(lines_return, "foo") expect_equal(tags_returns$return, tags_return$return) + +# @examplesIf wraps the examples in a hidden guard, as roxygen2 does (#39) +lines_if <- c("Title", "@examplesIf requireNamespace(\"stats\", quietly = TRUE)", + "f(1)", "", "@export") +tags_if <- tinyrox:::parse_tags(lines_if, "f") +expect_equal(tags_if$examples, paste( + "\\dontshow{if (requireNamespace(\"stats\", quietly = TRUE)) withAutoprint(\\{ # examplesIf}", + "f(1)", + "\\dontshow{\\}) # examplesIf}", + sep = "\n")) +expect_true(tags_if$export) + +# The generated page is valid Rd and keeps the example +rd_if <- tinyrox:::generate_rd(tags_if, list(names = "x", usage = "x")) +rd_if_file <- tempfile(fileext = ".Rd") +writeLines(rd_if, rd_if_file) +expect_equal(length(tools::checkRd(rd_if_file)), 0L) +expect_true(grepl("\\examples{", rd_if, fixed = TRUE)) +unlink(rd_if_file) + +# Plain and conditional examples on one block both reach the page, in order +lines_mix <- c("Title", "@examples", "a()", "@examplesIf interactive()", "b()") +tags_mix <- tinyrox:::parse_tags(lines_mix, "f") +expect_true(grepl("^a\\(\\)\n\\\\dontshow\\{if \\(interactive\\(\\)\\)", + tags_mix$examples)) +expect_true(grepl("b()", tags_mix$examples, fixed = TRUE)) + +# No condition: warn and keep the examples +expect_warning(tags_nocond <- tinyrox:::parse_tags( + c("Title", "@examplesIf", "f(1)"), "f"), "no condition") +expect_equal(tags_nocond$examples, "f(1)") diff --git a/man/RD_NAME_SUBS.Rd b/man/RD_NAME_SUBS.Rd index ca5010b..7066af9 100644 --- a/man/RD_NAME_SUBS.Rd +++ b/man/RD_NAME_SUBS.Rd @@ -11,9 +11,9 @@ RD_NAME_SUBS } \description{ Replacements applied to a topic name to make a file name, longest -key first so `[<-` is read before `[`. This is the table roxygen2 -uses, so a package moving between the two does not churn its `man` -directory, and the two names tinyrox already handled -- `\%||\%` and +key first so \code{[<-} is read before \code{[}. This is the table roxygen2 +uses, so a package moving between the two does not churn its \code{man} +directory, and the two names tinyrox already handled -- \code{\%||\%} and a leading dot -- come out exactly as they did. } \keyword{internal} diff --git a/man/SUPPORTED_DOC_TAGS.Rd b/man/SUPPORTED_DOC_TAGS.Rd index 43d1cca..2a1fa97 100644 --- a/man/SUPPORTED_DOC_TAGS.Rd +++ b/man/SUPPORTED_DOC_TAGS.Rd @@ -4,7 +4,7 @@ \alias{SUPPORTED_DOC_TAGS} \title{Supported Documentation Tags} \format{ -An object of class \code{character} of length 22. +An object of class \code{character} of length 23. } \usage{ SUPPORTED_DOC_TAGS diff --git a/man/SUPPORTED_TAGS.Rd b/man/SUPPORTED_TAGS.Rd index 898dc8c..74c9b0c 100644 --- a/man/SUPPORTED_TAGS.Rd +++ b/man/SUPPORTED_TAGS.Rd @@ -4,7 +4,7 @@ \alias{SUPPORTED_TAGS} \title{All Supported Tags} \format{ -An object of class \code{character} of length 28. +An object of class \code{character} of length 29. } \usage{ SUPPORTED_TAGS diff --git a/man/append_examples.Rd b/man/append_examples.Rd new file mode 100644 index 0000000..c52a83d --- /dev/null +++ b/man/append_examples.Rd @@ -0,0 +1,20 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{append_examples} +\alias{append_examples} +\title{Add Example Code to What a Block Already Has} +\usage{ +append_examples(existing, code) +} +\arguments{ +\item{existing}{Examples collected so far, or NULL.} + +\item{code}{Example code to add.} +} +\value{ +The combined examples text. +} +\description{ +A block can carry several example tags (plain and conditional); they +all go on the one page, in the order written. +} +\keyword{internal} diff --git a/man/check_markdown_rd.Rd b/man/check_markdown_rd.Rd new file mode 100644 index 0000000..68bbd4a --- /dev/null +++ b/man/check_markdown_rd.Rd @@ -0,0 +1,23 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{check_markdown_rd} +\alias{check_markdown_rd} +\title{Warn About Markdown That Ships Verbatim} +\usage{ +check_markdown_rd(path = ".") +} +\arguments{ +\item{path}{Path to package root directory} +} +\value{ +Named list, one integer vector of line numbers per affected + file (invisibly); empty when nothing was found or the check is off. +} +\description{ +tinyrox does no markdown parsing, so backticked text and bracketed +function links written in the roxygen2 markdown habit reach the Rd +unchanged and render literally in help pages and the PDF manual. +R CMD check says nothing about them. This reports each affected file +once. Set options(tinyrox.markdown_check = FALSE) to silence it for a +package that uses backticks on purpose. +} +\keyword{internal} diff --git a/man/check_rd_filenames.Rd b/man/check_rd_filenames.Rd index 7737a62..5de60d7 100644 --- a/man/check_rd_filenames.Rd +++ b/man/check_rd_filenames.Rd @@ -14,8 +14,8 @@ invisible(TRUE) \description{ rd_filename() is supposed to make every name portable. This says so out loud rather than trusting it, because the failure is silent in -both directions: `R CMD build` drops the file without an error, and -the `R CMD check` that follows examines a tarball the file is no +both directions: \code{R CMD build} drops the file without an error, and +the \code{R CMD check} that follows examines a tarball the file is no longer in. } \keyword{internal} diff --git a/man/documented_params.Rd b/man/documented_params.Rd new file mode 100644 index 0000000..4479a34 --- /dev/null +++ b/man/documented_params.Rd @@ -0,0 +1,19 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{documented_params} +\alias{documented_params} +\title{Formals a Set of @param Tags Documents} +\usage{ +documented_params(params) +} +\arguments{ +\item{params}{Named list of parameter descriptions from parse_tags().} +} +\value{ +Character vector of individual formal names. +} +\description{ +One @param can document several formals at once ("@param a,b desc", +rendered as one Rd item named "a,b"). The Rd keeps the joined name; the +undocumented-parameter check needs the individual ones. +} +\keyword{internal} diff --git a/man/find_exports_without_examples.Rd b/man/find_exports_without_examples.Rd index 9c0f3f5..a02af95 100644 --- a/man/find_exports_without_examples.Rd +++ b/man/find_exports_without_examples.Rd @@ -3,14 +3,18 @@ \alias{find_exports_without_examples} \title{Find Exported Functions Without Examples} \usage{ -find_exports_without_examples(lines) +find_exports_without_examples(blocks) } \arguments{ -\item{lines}{Character vector of file lines} +\item{blocks}{List of documentation blocks from parse_package()} } \value{ Character vector of function names missing examples } \description{ -Parses R file content to find @export tags without @examples. +Examples belong to an Rd page, and blocks sharing an @rdname share one +page, so the check runs per topic: an exported function is reported only +when no block on its page carries examples. A block that generates no +page (a bare @export above an S3 method, or @noRd) is skipped, since +there is nowhere for an example to live. } diff --git a/man/find_markdown_lines.Rd b/man/find_markdown_lines.Rd new file mode 100644 index 0000000..98633a1 --- /dev/null +++ b/man/find_markdown_lines.Rd @@ -0,0 +1,20 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{find_markdown_lines} +\alias{find_markdown_lines} +\title{Find Documentation Lines Containing Markdown} +\usage{ +find_markdown_lines(lines) +} +\arguments{ +\item{lines}{Character vector of file lines} +} +\value{ +Integer vector of line numbers +} +\description{ +Flags backtick pairs and bracketed function links in documentation +prose. Example code is skipped (backticks are R syntax there), as are +blocks tagged noRd (they ship nowhere) and text already inside an Rd +code macro. +} +\keyword{internal} diff --git a/man/order_params.Rd b/man/order_params.Rd new file mode 100644 index 0000000..cbe0293 --- /dev/null +++ b/man/order_params.Rd @@ -0,0 +1,20 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{order_params} +\alias{order_params} +\title{Order @param Names by the Function's Formals} +\usage{ +order_params(param_names, formal_names) +} +\arguments{ +\item{param_names}{Character vector of @param names.} + +\item{formal_names}{Character vector of formal argument names.} +} +\value{ +param_names, reordered. +} +\description{ +Formals order first, then anything matching no formal in the order +written. A joined name ("a,b") sorts at its earliest formal. +} +\keyword{internal} diff --git a/man/rd_filename.Rd b/man/rd_filename.Rd index 2cd5c6e..2e74679 100644 --- a/man/rd_filename.Rd +++ b/man/rd_filename.Rd @@ -12,17 +12,17 @@ rd_filename(name) A file name stem, with no .Rd extension. } \description{ -A topic can be called anything R can name, and `$.foo`, `[[.foo` -and `$<-.foo` are ordinary S3 methods. Those characters are not -legal in a file name on every platform R runs on, and `R CMD build` +A topic can be called anything R can name, and \code{$.foo}, \code{[[.foo} +and \code{$<-.foo} are ordinary S3 methods. Those characters are not +legal in a file name on every platform R runs on, and \code{R CMD build} does not warn and carry on: it prints "excluding invalid files" and **drops them from the tarball**. The package then installs with no -documentation for those methods, and `R CMD check` reports nothing, +documentation for those methods, and \code{R CMD check} reports nothing, because it checks the tarball the files are no longer in. Green, and missing its docs. } \details{ -The `\\name{}` inside the file keeps the real topic, so help still +The \code{\\name{}} inside the file keeps the real topic, so help still resolves under it; only the file on disk is renamed. } diff --git a/man/render_sections.Rd b/man/render_sections.Rd index 1fd5e62..f06c17c 100644 --- a/man/render_sections.Rd +++ b/man/render_sections.Rd @@ -6,7 +6,7 @@ render_sections(sections) } \arguments{ -\item{sections}{List of `list(title=, content=)` from parse_tags().} +\item{sections}{List of \code{list(title=, content=)} from parse_tags().} } \value{ Character vector of Rd lines (empty when there are no sections). diff --git a/man/resolve_external_params.Rd b/man/resolve_external_params.Rd index c5b72f9..8207acf 100644 --- a/man/resolve_external_params.Rd +++ b/man/resolve_external_params.Rd @@ -13,6 +13,6 @@ Named list of parameter descriptions, or empty list on failure. } \description{ Reads an installed package's Rd file to extract parameter documentation -for use with `@inheritParams pkg::function`. +for use with \code{@inheritParams pkg::function}. } \keyword{internal} From e5da53dd10db5d75df3fa48e03e7d02308aa4bde Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 24 Nov 2026 07:05:16 -0600 Subject: [PATCH 2/4] Remove fyi.md: the format is retired --- .Rbuildignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.Rbuildignore b/.Rbuildignore index 309dcd4..89f9cc0 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -3,6 +3,5 @@ ^\.github$ ^CLAUDE\.md$ ^\.claude$ -^fyi\.md$ ^cran-comments\.md$ ^tools$ From c94fe8f7db678a9a31606d2be44aa0c4c92ce4e3 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 24 Nov 2026 07:05:16 -0600 Subject: [PATCH 3/4] Bump version to 0.4.1.5 --- DESCRIPTION | 4 ++-- NEWS.md | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 572f576..0ccd5b9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: tinyrox Title: Minimal R Documentation Generator -Version: 0.4.1.3 -Date: 2026-10-05 +Version: 0.4.1.5 +Date: 2026-11-24 Authors@R: c( person("Troy", "Hernandez", email = "troy@cornball.ai", role = c("aut", "cre"), comment = c(ORCID = "0009-0005-4248-604X")), diff --git a/NEWS.md b/NEWS.md index fb1ec6d..c15f498 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# tinyrox 0.4.1.5 + +* A bare `@exportS3Method` registers the method (#37). With no argument the tag was dropped silently: no `S3method()` line and no export, so dispatch failed at runtime. Generic and class now come from the object name, a known or package-defined generic first (`as.data.frame.foo`), and a name with no dot to split on warns. +* `@exportS3Method pkg::generic` writes `S3method(pkg::generic,class)`, R's delayed registration for a generic the package does not import. It used to drop the `pkg::` prefix, and the two-argument form quoted it; either way the method was never registered. The class is the object name with `generic.` removed, and a name that does not start that way warns. +* `@examplesIf cond` is supported (#39). It was an unknown tag, so its examples were skipped with a warning and the page shipped with none. The output matches roxygen2: the guard sits in `\dontshow{}` and the help page shows only the example code. Several example tags on one block now all reach the page, in the order written; a second `@examples` used to replace the first. +* `@param a,b desc` documents both formals for the undocumented-parameter check (#40). The Rd was already right (`\item{a,b}{...}`); the check compared the joined name against each formal and reported both as undocumented. A joined name also sorts at its earliest formal in `\arguments`. +* The missing-examples check runs per Rd page (#41). A function merged into a page through `@rdname` is no longer reported when another block on the page carries the examples, and a bare `@export` above an S3 method, which generates no page, is skipped. +* `check_cran()` warns when roxygen text contains markdown that ships verbatim (#42): backticked terms and `[fn()]` links, reported once per file. Example code, `@noRd` blocks, and text already inside `\code{}` are ignored. Nothing is converted. Silence it with `options(tinyrox.markdown_check = FALSE)`. The result list gains a `markdown` element. + # tinyrox 0.4.1.3 * A replacement S3 method (`$<-.myclass`, `[[<-.myclass`, `dim<-.myclass`) was given the usage `\method{$<-}{myclass}(x, name, value)`, which `R CMD check` reports as a bad `\usage` line: the `<-` sits on the generic, not at the end of the name, so the replacement test never fired. It is `\method{$}{myclass}(x, name) <- value` now, as for a plain replacement function. Found in glinty's two output-registration methods. From 26fb80c1c39331df45816da591471ad038ef4375 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 24 Nov 2026 07:11:28 -0600 Subject: [PATCH 4/4] Split NEWS into 0.4.1.4 and 0.4.1.5 sections --- NEWS.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index c15f498..6e88d54 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,11 @@ # tinyrox 0.4.1.5 -* A bare `@exportS3Method` registers the method (#37). With no argument the tag was dropped silently: no `S3method()` line and no export, so dispatch failed at runtime. Generic and class now come from the object name, a known or package-defined generic first (`as.data.frame.foo`), and a name with no dot to split on warns. * `@exportS3Method pkg::generic` writes `S3method(pkg::generic,class)`, R's delayed registration for a generic the package does not import. It used to drop the `pkg::` prefix, and the two-argument form quoted it; either way the method was never registered. The class is the object name with `generic.` removed, and a name that does not start that way warns. * `@examplesIf cond` is supported (#39). It was an unknown tag, so its examples were skipped with a warning and the page shipped with none. The output matches roxygen2: the guard sits in `\dontshow{}` and the help page shows only the example code. Several example tags on one block now all reach the page, in the order written; a second `@examples` used to replace the first. + +# tinyrox 0.4.1.4 + +* A bare `@exportS3Method` registers the method (#37). With no argument the tag was dropped silently: no `S3method()` line and no export, so dispatch failed at runtime. Generic and class now come from the object name, a known or package-defined generic first (`as.data.frame.foo`), and a name with no dot to split on warns. * `@param a,b desc` documents both formals for the undocumented-parameter check (#40). The Rd was already right (`\item{a,b}{...}`); the check compared the joined name against each formal and reported both as undocumented. A joined name also sorts at its earliest formal in `\arguments`. * The missing-examples check runs per Rd page (#41). A function merged into a page through `@rdname` is no longer reported when another block on the page carries the examples, and a bare `@export` above an S3 method, which generates no page, is skipped. * `check_cran()` warns when roxygen text contains markdown that ships verbatim (#42): backticked terms and `[fn()]` links, reported once per file. Example code, `@noRd` blocks, and text already inside `\code{}` are ignored. Nothing is converted. Silence it with `options(tinyrox.markdown_check = FALSE)`. The result list gains a `markdown` element.