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.
- Unhandled
Pragma - A coverage pragma comment that was found but not handled.
Enums§
- Instrument
Error - Error type for instrumentation failures.
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.