docs(r): document replacement-function calls, package-less R/ directories and unparseable files

Pins the rename behaviour with two fixtures (an R/ directory without a package is
refused; a syntax error in an unrelated indexed file refuses with parse_error) and
adds the replacement-function CALLS limitation to the Limits section.
This commit is contained in:
Mick Cooney 2026-10-18 15:58:43 +01:00
parent e41f81b36e
commit 244a23f2e3
10 changed files with 36 additions and 2 deletions

View file

@ -71,6 +71,7 @@ Not edges or nodes:
- R6 `super$method()` and `private$method()` calls, S4 slot access (`obj@slot`), and `UseMethod()` / `NextMethod()` create no edge.
- Anonymous functions (`function(x)`, `\(x)`) passed as arguments are not indexed as definitions; calls inside are attributed to the enclosing function, or to the File at top level.
- Infix operators (`a %op% b`) are not linked to their definitions.
- A replacement-function call, `foo(x) <- value`, is captured as a call of `foo`: the `CALLS` edge goes to a function named `foo` (the getter), although R runs `` `foo<-` ``. A function defined as `` `foo<-` <- function(x, value) ... `` therefore has no callers in the graph, and `impact` on it reports none. `rename` refuses both halves of such a pair (see Rename).
- Constants and other top-level non-function assignments, and environments (`new.env()`, `local()`, `assign()` / `get()`), are not nodes; the only value-like nodes are R6, RefClass and S4 fields and slots. `setValidity()`, `setClassUnion()`, `setReplaceMethod()` and `Class$methods(...)` create no definition or edge.
- No framework or entry-point detection (Shiny, plumber, targets, testthat). Plumber `#*` annotations are not read and no route nodes are created, and no route or fetch edges link R to other languages.
@ -156,10 +157,11 @@ Not modelled:
- a new name that the sources already read as a value or call (`name_collision`): `sapply(x, paste)`, `Reduce(max, x)`, `x <- letters`, a default `f = assist`, a formula term, `assist[1]`, `assist(1)`. After the rename the function is the global of that name and would capture the read. A parameter of an enclosing function of that name covers a read, and covers a call only when it has no default. Not reads: bound names, argument names (`f(assist = 1)`), members (`x$assist`, `x@assist`) and qualified names (`pkg::assist`);
- a call of the name under a same-named parameter that may hold a function (`ambiguous_reference`): a function, `NULL` or computed default, or an assignment to the parameter in the body, because the call may reach the parameter or the function being renamed;
- a function that is also assigned with `<<-` or `->>` anywhere in the indexed R sources (`ambiguous_reference`, as the top-level form already was): the global re-assignment is a second definition that a rename would leave behind;
- a package function under `R/` that is not proven private, because `NAMESPACE` is not an indexed source and its entry would not be renamed. Two checks must both pass. The persisted `isExported` flag must be false, which it is only for a function that a `NAMESPACE` file beside the package leaves out of `export()`, `exportPattern()` and `S3method()` (the third argument of `S3method(generic, class, method)` counts): a package with no `NAMESPACE` file, or an `R/` folder that belongs to no package, keeps the default public flag and is refused. The flag is not enough alone, because an edit of `NAMESPACE` with no `R/` file touched leaves it stale until a full analyse. So every rename plan also reads the package's `NAMESPACE` from disk again, through the parsing the indexer uses, and refuses (`unsupported_occurrence`) when it now exports the name by `export()`, `exportPattern()` or `S3method()`. It fails closed with `incomplete_semantics` when that `NAMESPACE` was deleted or cannot be read, when an `exportPattern()` of it was dropped or ran out of work budget (a name such a pattern was meant to cover is then not provably private), or when the reader skipped or could not read something that may hide an export: a conditional `if (...) export(x)`, a raw-string or multi-line argument, an argument that is an expression, an `S3method()` it cannot read, or a malformed directive that swallowed an export. It also refuses (`unsupported_occurrence`) a function whose roxygen2 block (the `#'` lines directly above the definition) carries `@export`, `@exportMethod`, `@exportS3Method` or another `@export*` tag, because the next `document()` would export the new name; the tag is found by text, other roxygen2 tags (`@rdname`, `@inheritParams`, `@seealso`) are not read. A private function in a package whose `NAMESPACE` is unchanged is renamed; a script outside `R/` is not affected. The check does not need `--force`; re-run `analyze` to bring the flag itself up to date.
- a package function under `R/` that is not proven private, because `NAMESPACE` is not an indexed source and its entry would not be renamed. Two checks must both pass. The persisted `isExported` flag must be false, which it is only for a function that a `NAMESPACE` file beside the package leaves out of `export()`, `exportPattern()` and `S3method()` (the third argument of `S3method(generic, class, method)` counts): a package with no `NAMESPACE` file, or an `R/` folder that belongs to no package, keeps the default public flag and is refused. That includes projects that keep their functions in `R/` without a `DESCRIPTION` (targets, drake and analysis repositories): every rename of a function under `R/` is refused there, and a function outside `R/` is not. The flag is not enough alone, because an edit of `NAMESPACE` with no `R/` file touched leaves it stale until a full analyse. So every rename plan also reads the package's `NAMESPACE` from disk again, through the parsing the indexer uses, and refuses (`unsupported_occurrence`) when it now exports the name by `export()`, `exportPattern()` or `S3method()`. It fails closed with `incomplete_semantics` when that `NAMESPACE` was deleted or cannot be read, when an `exportPattern()` of it was dropped or ran out of work budget (a name such a pattern was meant to cover is then not provably private), or when the reader skipped or could not read something that may hide an export: a conditional `if (...) export(x)`, a raw-string or multi-line argument, an argument that is an expression, an `S3method()` it cannot read, or a malformed directive that swallowed an export. It also refuses (`unsupported_occurrence`) a function whose roxygen2 block (the `#'` lines directly above the definition) carries `@export`, `@exportMethod`, `@exportS3Method` or another `@export*` tag, because the next `document()` would export the new name; the tag is found by text, other roxygen2 tags (`@rdname`, `@inheritParams`, `@seealso`) are not read. A private function in a package whose `NAMESPACE` is unchanged is renamed; a script outside `R/` is not affected. The check does not need `--force`; re-run `analyze` to bring the flag itself up to date.
- **Not updated by a successful rename:** files that are not indexed R sources. R Markdown and Quarto documents (`.Rmd`, `.qmd`, vignettes, `README.Rmd`), `man/*.Rd` (`\alias`, `\usage`), roxygen2 tags that name the function (`@rdname`, `@inheritParams`, `@seealso`), `_pkgdown.yml`, `pkg:::name` uses in `inst/`, and C++ or other code that looks it up by string keep the old name. The result does not list them; search for the old name in those files after a rename.
- **Refused by the shared engine (fail-closed, not provider code):** S3 methods and any function name with a dot after its first character (`area.circle` reads as an owner-qualified member, so `stale_graph`). A name whose only dot is the leading one (`.helper`, the usual internal-function convention) is renamed like any other function, with one exception: R's hook functions `.onLoad`, `.onAttach`, `.onUnload`, `.onDetach`, `.First` and `.Last` are refused (`unsupported_occurrence`), because R calls them by name; S4 class names (`unsupported_occurrence`, no exact source-name span).
- **Invalid names (`invalid_name`):** anything that is not a nonreserved R identifier: the reserved words, `...` and `..1`, `..2`, ..., and any name with a non-ASCII character, because new names are limited to ASCII letters, digits, `.` and `_` (conservative by design; the message says so). An old name made only of non-ASCII characters (`λ`) is refused as not a supported identifier. Backtick-quoting a new name is never offered: a name that needs it is refused rather than quoted at every use.
- **Refused when any indexed R file does not parse (`parse_error`):** the rename plan parses every indexed R file, and a syntax error in one of them, related to the target or not, refuses the rename and names that file. Fix the file (or exclude it) and plan again.
- **Not proven:** unqualified calls to a function defined in another file (including the package `R/` layout) fail with `ambiguous_reference`, because R's global-name fallback is not a provable binding. Renaming across files works only where the call is qualified or otherwise resolved.
## Type annotations

View file

@ -0,0 +1,3 @@
helper <- function(x) x * 2
run <- function() helper(2)

View file

@ -0,0 +1,3 @@
helper <- function(x) x * 2
run <- function() helper(2)

View file

@ -0,0 +1,9 @@
{
"language": "r",
"description": "Refuse a function in an R/ directory that belongs to no package (no DESCRIPTION or NAMESPACE, as in a targets or drake project): nothing proves it private.",
"categories": ["namespace", "package-export", "negative", "unchanged-on-error"],
"target": { "name": "helper", "kind": "Function", "file": "R/utils.R", "startLine": 0 },
"newName": "secret",
"expect": { "status": "error", "code": "unsupported_occurrence", "planningStatus": "unsupported" },
"edges": []
}

View file

@ -0,0 +1,3 @@
helper <- function(x) x * 2
run <- function() helper(2)

View file

@ -0,0 +1 @@
broken <- function( {

View file

@ -0,0 +1,3 @@
helper <- function(x) x * 2
run <- function() helper(2)

View file

@ -0,0 +1 @@
broken <- function( {

View file

@ -0,0 +1,9 @@
{
"language": "r",
"description": "A syntax error in an unrelated indexed R file refuses the rename: the file cannot be proven free of the name.",
"categories": ["parse-error", "negative", "unchanged-on-error"],
"target": { "name": "helper", "kind": "Function", "file": "a.R", "startLine": 0 },
"newName": "secret",
"expect": { "status": "error", "code": "parse_error", "planningStatus": "blocked" },
"edges": []
}

View file

@ -5,7 +5,7 @@ import { defineRenameFixtureTests, renameCases } from '../helpers/rename-integra
// would define no test and stay green. Pin the number of R cases: adding one means raising it.
describe('R rename fixtures', () => {
it('are all discovered', () => {
expect(renameCases.filter((fixture) => fixture.language === 'r')).toHaveLength(56);
expect(renameCases.filter((fixture) => fixture.language === 'r')).toHaveLength(58);
});
});