Skip to content

PRL-PRG/impuresrcref

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

impuresrcref

impuresrcref imputes transparent srcref metadata for injected brace calls ({) in R function ASTs.

srcref primer

In R, a srcref is source-location metadata (line/column ranges) attached to parsed objects when source retention is enabled (for example with options(keep.source = TRUE)). This package solves a finer-grained mapping problem for control-flow expressions written without braces (for example if (x) f() else g()). It adds transparent wrappers and imputes srcrefs from parse data so individual control-flow components can be mapped to source precisely. It uses the parser token tables getParseData for it.

What it does

Given code like:

if (x && y) f() else g()

impute_srcrefs() rewrites control-flow positions to:

if ({x} && {y}) {f()} else {g()}

and assigns srcrefs to injected { calls using parse-data-derived source spans.

Exported API

  • impute_srcrefs(fn)
    • Impute missing control-flow braces and transparent srcrefs for one function.
  • source_impute_srcrefs(file, envir = parent.frame(), ...)
    • Source an R file and patch all changed/new functions in the target environment.
  • check_package_parse_data(package, include_internal = TRUE)
    • Check whether installed package functions carry parse data.
  • impute_package_srcrefs(package, include_internal = TRUE, ...)
    • Patch package functions if parse data is available

Important: package installs and parse data

For installed packages, parse data is often missing unless the package was installed from source with package source options enabled.

Recommended installation pattern:

local({
  options(keep.source.pkgs = TRUE, keep.parse.data.pkgs = TRUE)
  install.packages("MASS", type = "source")
})

Usage

Patch functions

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)
g

Source a file and patch all loaded functions

env <- new.env(parent = baseenv())
res <- source_impute_srcrefs("path/to/file.R", envir = env)
res$functions

Check and patch an installed package

check_package_parse_data("MASS")
res <- impute_package_srcrefs("MASS")
res$patched_count

If parse data is missing, impute_package_srcrefs() prints an install command and returns without patching.

Functions without srcref metadata

impute_srcrefs() requires function srcref metadata by default. For functions created without srcrefs, opt into deparse-based fallback explicitly:

options(impuresrcref.allow_deparse_fallback = TRUE)

Tests

Snapshot tests compare generated output against committed .out golden files. Run tests in compare mode:

Rscript tests/test-srcref-imputation.R

Refresh snapshots:

UPDATE_SNAPSHOTS=1 Rscript tests/test-srcref-imputation.R

By default, mismatches fail. With UPDATE_SNAPSHOTS=1, the snapshot file is rewritten. Legacy --update is still accepted for compatibility.

Acknowledgements

This package was inspired in part by covr's parse-data handling approach.

About

No description, website, or topics provided.

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published