| Title: | Impute Transparent Source References for Injected Braces |
|---|---|
| Description: | Rewrites R function ASTs by injecting transparent brace calls in control-flow positions and imputing source references ('srcref') from parse data. Supports individual functions, sourced files, and installed package namespace functions, including package-style 'srcref' line mappings. |
| Authors: | Filip Krikava [aut, cre, cph], Matej Kocourek [aut, cph], Pierre Donat-Bouillud [aut, cph] |
| Maintainer: | Filip Krikava <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.0.9000 |
| Built: | 2026-07-15 11:20:13 UTC |
| Source: | https://github.com/PRL-PRG/imputesrcref |
By default, impute_srcrefs() skips argument wrapping for primitive
SPECIALSXP calls discovered from builtins(). These functions inspect and
modify the user-configured portion of that blacklist.
get_impute_blacklist(include_default = TRUE) set_impute_blacklist(functions, append = TRUE) reset_impute_blacklist()get_impute_blacklist(include_default = TRUE) set_impute_blacklist(functions, append = TRUE) reset_impute_blacklist()
include_default |
If |
functions |
Character vector of call names. |
append |
If |
get_impute_blacklist() returns the blacklist, optionally including the
built-in defaults.
set_impute_blacklist() adds or replaces user entries, stored in
options(imputesrcref.wrap_arg_blacklist = ...).
reset_impute_blacklist() clears user entries.
get_impute_blacklist() returns a sorted unique character vector of call
names. set_impute_blacklist() invisibly returns the current
user-configured entries. reset_impute_blacklist() invisibly returns an
empty character vector.
head(get_impute_blacklist()) set_impute_blacklist(c("str_c", "paste")) get_impute_blacklist(include_default = FALSE) reset_impute_blacklist()head(get_impute_blacklist()) set_impute_blacklist(c("str_c", "paste")) get_impute_blacklist(include_default = FALSE) reset_impute_blacklist()
Check parse data for a package and impute srcrefs on all its functions.
impute_package_srcrefs(package, include_internal = TRUE, verbose = TRUE)impute_package_srcrefs(package, include_internal = TRUE, verbose = TRUE)
package |
Package name. |
include_internal |
If |
verbose |
If |
Invisibly returns a list with:
package: package name
fn_names: inspected function names
failed: failure messages (NA for successfully patched functions)
patched_count: number of patched functions
Traverses a function's AST and wraps unbraced expressions in targeted
positions with { ... } while attaching transparent srcrefs to the injected
brace calls. The srcref assigned to an injected brace matches the span of the
wrapped expression so that source mapping stays aligned with original code.
impute_srcrefs(fn, wrap_call_args = TRUE, quiet = FALSE)impute_srcrefs(fn, wrap_call_args = TRUE, quiet = FALSE)
fn |
A function. |
wrap_call_args |
If |
quiet |
If |
Covered constructs include:
if / else
for, while, repeat
switch
logical operators (&&, ||, &, |)
function defaults and function bodies
function call arguments (optional; call expressions only)
For functions without srcref metadata, deparse-based fallback is disabled by
default. To allow fallback, set
options(impuresrcref.allow_deparse_fallback = TRUE).
Generic call argument wrapping skips blacklisted callee names. By default the
blacklist includes primitive SPECIALSXP calls from builtins(). Use
set_impute_blacklist() / reset_impute_blacklist() to customize.
A function with transformed body/formals and preserved function-level attributes (including srcref/srcfile metadata when present).
options(keep.source = TRUE) f <- eval(parse(text = "function(x, y) if (x && y) f() else g()", keep.source = TRUE)[[1]]) g <- impute_srcrefs(f) goptions(keep.source = TRUE) f <- eval(parse(text = "function(x, y) if (x && y) f() else g()", keep.source = TRUE)[[1]]) g <- impute_srcrefs(f) g
Sources a file with base::sys.source() and then rewrites function bindings
that were created or changed by that source call via impute_srcrefs().
This is the recommended entry point for source files because it can enforce
both source retention and parse-data retention.
source_impute_srcrefs( file, envir = parent.frame(), chdir = FALSE, keep.source = TRUE, keep.parse.data = TRUE, toplevel.env = as.environment(envir), all.names = TRUE )source_impute_srcrefs( file, envir = parent.frame(), chdir = FALSE, keep.source = TRUE, keep.parse.data = TRUE, toplevel.env = as.environment(envir), all.names = TRUE )
file |
Path to an R source file. |
envir |
Environment where the file is sourced. |
chdir |
Passed to |
keep.source |
Passed to |
keep.parse.data |
Passed to |
toplevel.env |
Passed to |
all.names |
Include hidden bindings when scanning for functions. |
Invisibly returns a list with:
file: normalized file path
functions: names of patched functions
count: number of patched functions
keep.source / keep.parse.data: effective source flags
## Not run: tf <- tempfile(fileext = ".R") writeLines("f <- function(x, y) if (x && y) f() else g()", tf) env <- new.env(parent = baseenv()) source_impute_srcrefs(tf, envir = env) env$f ## End(Not run)## Not run: tf <- tempfile(fileext = ".R") writeLines("f <- function(x, y) if (x && y) f() else g()", tf) env <- new.env(parent = baseenv()) source_impute_srcrefs(tf, envir = env) env$f ## End(Not run)