Expand description
Istanbul-compatible JavaScript/TypeScript coverage instrumentation using the Oxc AST.
This crate parses JS/TS source with oxc_parser, identifies statements,
functions, and branches, injects coverage counter expressions, and emits
instrumented code. The coverage map output is compatible with Istanbul’s
coverage-final.json format (consumed by Jest, Vitest, c8, nyc, Codecov).
§Example
use oxc_coverage_instrument::{instrument, InstrumentOptions};
let source = "function add(a, b) { return a + b; }";
let result = instrument(source, "add.js", &InstrumentOptions::default()).unwrap();
println!("Instrumented code:\n{}", result.code);
println!("Functions found: {}", result.coverage_map.fn_map.len());§Coverage model
The coverage map tracks three dimensions:
- Statements: every executable statement gets a counter
- Functions: every function declaration, expression, arrow, and method
- Branches: if/else, ternary, switch cases, logical &&/||
Function names are derived from the same Oxc parser used by other Oxc-based tools, so they match consistently across the ecosystem.
Structs§
- Branch
Entry - Branch entry in the coverage map.
- File
Coverage - Coverage data for a single file. Serializes to Istanbul’s
coverage-final.jsonformat. - FnEntry
- Function entry in the coverage map.
- Instrument
Options - Options for the
instrumentfunction. - Instrument
Result - Result of instrumenting a source file.
- Location
- A source location span with start and end positions.
- Position
- A 1-based line, 0-based column position.
- Position
Remapper - A position-remap predicate over a parsed
inputSourceMap. - Remap
Options - Options for the remap helpers. The default value preserves the legacy
keep-generated-position behaviour for positions whose source-map lookup
returns
None, so existing callers that passRemapOptions::default(or rely on the parameterlessremap_coverage/remap_coverage_maphelpers) see no change. - Source
MapStore - Stateful map store for the Mode B “continuous remap during collection”
flow. Some runners (Jest with
transform) instrument files incrementally and want to record each file’s source map as it is produced, then rewriteFileCoverageobjects on the fly rather than once at report time. - Unhandled
Pragma - A coverage pragma comment that was found but not handled.
- V8Coverage
Range - A single V8 coverage range.
- V8Function
Coverage - A function’s coverage data as reported by the V8 inspector.
Enums§
- Decorator
Mode - How
strip_typescripthandles decorator syntax. - Instrument
Error - Error type for instrumentation failures.
- V8To
Istanbul Error - Errors produced by the V8-to-Istanbul conversion.
Functions§
- instrument
- Instrument a JavaScript/TypeScript source file for coverage collection.
- parse_
coverage_ map - Parse a
coverage-final.jsonstring into a map of file paths to coverage data. - remap_
coverage - Remap a single
FileCoveragethrough its embeddedinputSourceMap. - remap_
coverage_ map - Remap every
FileCoveragein a coverage map. Entries without aninputSourceMappass through unchanged under their original key. Entries with aninputSourceMapare rewritten and re-keyed by their resolved original source path. - remap_
coverage_ map_ with_ loader - Like
remap_coverage_map, but with a disk-read fallback for entries that lack an embeddedinputSourceMap. The loader is called with theFileCoverage.pathof each entry that needs an external map. - remap_
coverage_ map_ with_ loader_ and_ options - Like
remap_coverage_map_with_loader, with aRemapOptionsargument. SeeRemapOptions::drop_unmappedfor the pruning semantics. - remap_
coverage_ map_ with_ options - Like
remap_coverage_map, with aRemapOptionsargument. SeeRemapOptions::drop_unmappedfor the pruning semantics. - remap_
coverage_ with_ loader - Like
remap_coverage, but when the entry has no embeddedinputSourceMapthe suppliedloaderis consulted with theFileCoverage.pathto fetch the source map JSON from disk or another source. Matchesistanbul-lib-source-maps’ssourceStorecallback used by nyc when the map sits next to the instrumented file rather than embedded inside the coverage object. - remap_
coverage_ with_ loader_ and_ options - Like
remap_coverage_with_loader, with aRemapOptionsargument. SeeRemapOptions::drop_unmappedfor the pruning semantics. - remap_
coverage_ with_ options - Like
remap_coverage, but with aRemapOptionsargument. SeeRemapOptions::drop_unmappedfor the pruning semantics. - v8_
to_ istanbul - Convert V8 function coverage into Istanbul
FileCoverage. - v8_
to_ istanbul_ with_ loader - Like
v8_to_istanbul, but with a loader for externalsourceMappingURLreferences.