oxc_coverage_instrument
Istanbul-compatible JavaScript/TypeScript coverage instrumentation, built on the Oxc parser. 5-33x faster than existing tools.
Why
swc-coverage-instrument fills this role for SWC. There is no equivalent for the Oxc ecosystem. Any tool built on oxc_parser that needs coverage instrumentation currently has to pull in SWC or Babel.
This crate fills that gap. AST-level instrumentation via oxc_traverse + oxc_codegen produces correct Istanbul-compatible output, verified against the canonical istanbul-lib-instrument on 25 shared fixtures.
Install
Rust
[]
= "0.2"
Node.js
CLI
Usage
Rust
use ;
let source = "function add(a, b) { return a + b; }";
let result = instrument.unwrap;
// Istanbul-compatible coverage map
assert_eq!;
// Instrumented source with counters injected
println!;
Node.js
import from 'oxc-coverage-instrument';
const result = ;
result.; // instrumented source
const coverageMap = JSON.; // Istanbul format
result.; // source map JSON (if enabled)
CLI
# Print instrumented code to stdout
# Write to file
# Print coverage map only
# With source map
Vitest integration
import { defineConfig } from 'vitest/config'
import { createOxcInstrumenter } from 'oxc-coverage-instrument/vitest'
export default defineConfig({
test: {
coverage: {
provider: 'istanbul',
instrumenter: () => createOxcInstrumenter(),
}
}
})
Note: Requires Vitest with custom instrumenter support (see vitest#10119).
Vite plugin example
import from 'oxc-coverage-instrument';
export
Reading existing coverage data
use parse_coverage_map;
// Parse a coverage-final.json file
let json = read_to_string.unwrap;
let map = parse_coverage_map.unwrap;
for in &map
What it tracks
| Dimension | What gets a counter |
|---|---|
| Statements | Every executable statement |
| Functions | Declarations, expressions, arrows, class methods |
| Branches | if/else, ternary, switch, &&/||/??, ??=/||=/&&=, default-arg |
| Pragmas | istanbul ignore next/if/else/file, v8 ignore, c8 ignore |
Istanbul conformance
Verified against istanbul-lib-instrument on 25 shared fixtures covering all branch types, function forms, and edge cases. 175 automated conformance checks validate:
- Function counts and names match exactly
- Branch counts, types, and location counts match exactly
- Statement counts within tolerance
- JSON structure matches Istanbul's field set
- Instrumented output re-parses as valid JS
Performance
Benchmarked on real-world JavaScript libraries, all running in the same Node.js process for a fair comparison. Reproduce with ./scripts/benchmark-comparison.sh.
| File | Size | oxc (napi) | babel-plugin-istanbul | swc-plugin (wasm) | istanbul-lib |
|---|---|---|---|---|---|
| react.development.js | 107 KB | 1.9 ms | 23.9 ms | 29.9 ms | 91.9 ms |
| lodash.js | 531 KB | 7.3 ms | 64.1 ms | 93.7 ms | 225.3 ms |
| vue.global.js | 462 KB | 13.3 ms | 123.7 ms | 206.3 ms | 565.9 ms |
| d3.js | 573 KB | 23.7 ms | 195.8 ms | 299.1 ms | 782.8 ms |
| three.js | 1.2 MB | 31.4 ms | 319.2 ms | 417.8 ms | 1109.3 ms |
8-13x faster than babel-plugin-istanbul, 10-16x faster than swc-plugin-coverage-instrument (Rust/WASM), 30-48x faster than istanbul-lib-instrument.
Note: swc-plugin-coverage-instrument is written in Rust but runs as a WASM module inside SWC's sandbox, adding serialisation overhead at every AST boundary. The comparison measures end-to-end instrumentation time as users experience it.
Architecture
source code (JS/TS)
|
v
oxc_parser -- parse to AST
|
v
SemanticBuilder -- build scope tree
|
v
CoverageTransform -- traverse AST, inject ++cov().s[N] counters
|
v
oxc_codegen -- emit instrumented code + source map
|
v
instrumented code + coverage map
Related projects
| Project | AST | Notes |
|---|---|---|
istanbul-lib-instrument |
Babel | The canonical Istanbul instrumenter |
babel-plugin-istanbul |
Babel | Babel plugin wrapper around istanbul-lib-instrument |
swc-plugin-coverage-instrument |
SWC | SWC WASM plugin |
| this crate | Oxc | Native Rust, 5-33x faster |
Compatibility
- Rust: 1.92+ (2024 edition)
- Oxc: 0.124.x
- Istanbul:
coverage-final.jsonv3+ format - Node.js: 18+ (via napi-rs)
License
MIT