use std::path::{Path, PathBuf};
use oxc_allocator::Allocator;
use oxc_ast::ast::Program;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_parser::{Parser, ParserReturn};
use oxc_semantic::{Scoping, SemanticBuilder};
use oxc_span::SourceType;
use oxc_transformer::{
DecoratorOptions, JsxOptions, TransformOptions, Transformer, TypeScriptOptions,
};
use oxc_traverse::traverse_mut;
use std::collections::BTreeMap;
use crate::coverage_builder::{CoverageMaps, build_file_coverage, build_function_identity_map};
use crate::pragma::PragmaMap;
use crate::transform::{
CoverageState, CoverageTransform, PreambleInputs, TransformInit, djb31_hex,
generate_cov_fn_name, generate_preamble_source,
};
use oxc_coverage_types::{FileCoverage, UnhandledPragma};
#[derive(Debug, Clone)]
pub struct InstrumentOptions {
pub coverage_variable: String,
pub source_map: bool,
pub input_source_map: Option<String>,
pub report_logic: bool,
pub ignore_class_methods: Vec<String>,
pub strip_typescript: bool,
pub decorator_mode: DecoratorMode,
pub function_identity_overlay: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DecoratorMode {
#[default]
PassThrough,
Experimental,
ExperimentalWithMetadata,
}
impl DecoratorMode {
#[must_use]
pub const fn legacy(self) -> bool {
matches!(self, Self::Experimental | Self::ExperimentalWithMetadata)
}
#[must_use]
pub const fn emit_metadata(self) -> bool {
matches!(self, Self::ExperimentalWithMetadata)
}
}
impl Default for InstrumentOptions {
fn default() -> Self {
Self {
coverage_variable: "__coverage__".to_string(),
source_map: false,
input_source_map: None,
report_logic: false,
ignore_class_methods: Vec::new(),
strip_typescript: false,
decorator_mode: DecoratorMode::PassThrough,
function_identity_overlay: false,
}
}
}
#[derive(Debug)]
pub struct InstrumentResult {
pub code: String,
pub coverage_map: FileCoverage,
pub coverage_map_json: String,
pub source_map: Option<String>,
pub unhandled_pragmas: Vec<UnhandledPragma>,
}
fn is_valid_js_identifier(s: &str) -> bool {
!s.is_empty()
&& s.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_' || c == '$')
&& s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
}
fn serialize_coverage_map(coverage_map: &FileCoverage) -> String {
serde_json::to_string(coverage_map).expect("FileCoverage serializes to JSON infallibly")
}
pub fn instrument(
source: &str,
filename: &str,
options: &InstrumentOptions,
) -> Result<InstrumentResult, InstrumentError> {
if !is_valid_js_identifier(&options.coverage_variable) {
return Err(InstrumentError::InvalidCoverageVariable(options.coverage_variable.clone()));
}
let allocator = Allocator::default();
let mut parsed = parse_program(&allocator, source, filename)?;
let (pragmas, unhandled_pragmas) = PragmaMap::from_program(&parsed.program, source);
if pragmas.ignore_file {
return Ok(empty_coverage_result(filename, source, unhandled_pragmas));
}
let mut scoping = SemanticBuilder::new().build(&parsed.program).semantic.into_scoping();
if options.strip_typescript {
scoping = strip_typescript_pass(
&allocator,
filename,
&mut parsed.program,
scoping,
options.decorator_mode,
)?;
}
let cov_fn_name = generate_cov_fn_name(filename);
let mut transform = CoverageTransform::new(TransformInit {
allocator: &allocator,
source,
cov_fn_name: &cov_fn_name,
report_logic: options.report_logic,
ignore_class_methods: options.ignore_class_methods.clone(),
});
let state = CoverageState { pragmas };
let scoping = traverse_mut(&mut transform, &allocator, &mut parsed.program, scoping, state);
let mut coverage_map =
build_coverage_map(filename, transform, options.input_source_map.as_deref());
if options.function_identity_overlay {
coverage_map.x_fallow_function_map =
Some(build_function_identity_map(&coverage_map.path, &coverage_map.fn_map));
}
let coverage_json = serialize_coverage_map(&coverage_map);
let coverage_hash = djb31_hex(&coverage_json);
let preamble = generate_preamble_source(&PreambleInputs {
coverage: &coverage_map,
coverage_json: &coverage_json,
coverage_hash: &coverage_hash,
coverage_var: &options.coverage_variable,
cov_fn_name: &cov_fn_name,
report_logic: options.report_logic,
});
let (code, raw_source_map) = emit_code(EmitInputs {
program: &parsed.program,
scoping,
source,
filename,
preamble: &preamble,
options,
});
let source_map = raw_source_map
.as_ref()
.map(|sm| finalize_source_map(sm, &preamble, options.input_source_map.as_deref()));
Ok(InstrumentResult {
code,
coverage_map,
coverage_map_json: coverage_json,
source_map,
unhandled_pragmas,
})
}
fn strip_typescript_pass<'a>(
allocator: &'a Allocator,
filename: &str,
program: &mut Program<'a>,
scoping: Scoping,
decorator_mode: DecoratorMode,
) -> Result<Scoping, InstrumentError> {
let options = TransformOptions {
typescript: TypeScriptOptions::default(),
jsx: JsxOptions::disable(),
decorator: DecoratorOptions {
legacy: decorator_mode.legacy(),
emit_decorator_metadata: decorator_mode.emit_metadata(),
},
..TransformOptions::default()
};
let transformer = Transformer::new(allocator, Path::new(filename), &options);
let ret = transformer.build_with_scoping(scoping, program);
if !ret.errors.is_empty() {
return Err(InstrumentError::TransformError(
ret.errors.iter().map(|e| format!("{e}")).collect::<Vec<_>>(),
));
}
Ok(ret.scoping)
}
fn parse_program<'a>(
allocator: &'a Allocator,
source: &'a str,
filename: &str,
) -> Result<ParserReturn<'a>, InstrumentError> {
let source_type = SourceType::from_path(filename).unwrap_or_default();
let parsed = Parser::new(allocator, source, source_type).parse();
if parsed.errors.is_empty() {
Ok(parsed)
} else {
Err(InstrumentError::ParseError(
parsed.errors.iter().map(|e| format!("{e}")).collect::<Vec<_>>().join("; "),
))
}
}
fn empty_coverage_result(
filename: &str,
source: &str,
unhandled_pragmas: Vec<UnhandledPragma>,
) -> InstrumentResult {
let coverage_map = build_file_coverage(CoverageMaps {
path: filename.to_string(),
statement_locs: Vec::new(),
fn_entries: Vec::new(),
branch_entries: Vec::new(),
logical_branch_ids: Vec::new(),
});
let coverage_map_json = serialize_coverage_map(&coverage_map);
InstrumentResult {
code: source.to_string(),
coverage_map,
coverage_map_json,
source_map: None,
unhandled_pragmas,
}
}
fn build_coverage_map(
filename: &str,
transform: CoverageTransform<'_, '_>,
input_source_map: Option<&str>,
) -> FileCoverage {
let mut coverage_map = build_file_coverage(CoverageMaps {
path: filename.to_string(),
statement_locs: transform.statement_map,
fn_entries: transform.fn_map,
branch_entries: transform.branch_map,
logical_branch_ids: transform.logical_branch_ids,
});
if let Some(input_sm) = input_source_map {
coverage_map.input_source_map = serde_json::from_str(input_sm).ok();
}
coverage_map
}
#[expect(
clippy::redundant_pub_crate,
reason = "crate-internal type intentionally; the explicit pub(crate) documents that this is not part of the public API even though the parent module is already private"
)]
pub(crate) struct V8CollectResult {
pub(crate) coverage_map: FileCoverage,
pub(crate) arm_body_byte_spans: BTreeMap<String, Vec<(u32, u32)>>,
}
#[expect(
clippy::redundant_pub_crate,
reason = "crate-internal function intentionally; the explicit pub(crate) documents that this is not part of the public API even though the parent module is already private"
)]
pub(crate) fn collect_for_v8_to_istanbul(
source: &str,
filename: &str,
) -> Result<V8CollectResult, InstrumentError> {
let allocator = Allocator::default();
let mut parsed = parse_program(&allocator, source, filename)?;
let (pragmas, _unhandled_pragmas) = PragmaMap::from_program(&parsed.program, source);
if pragmas.ignore_file {
let coverage_map = build_file_coverage(CoverageMaps {
path: filename.to_string(),
statement_locs: Vec::new(),
fn_entries: Vec::new(),
branch_entries: Vec::new(),
logical_branch_ids: Vec::new(),
});
return Ok(V8CollectResult { coverage_map, arm_body_byte_spans: BTreeMap::new() });
}
let scoping = SemanticBuilder::new().build(&parsed.program).semantic.into_scoping();
let cov_fn_name = generate_cov_fn_name(filename);
let mut transform = CoverageTransform::new(TransformInit {
allocator: &allocator,
source,
cov_fn_name: &cov_fn_name,
report_logic: false,
ignore_class_methods: Vec::new(),
});
let state = CoverageState { pragmas };
let _scoping = traverse_mut(&mut transform, &allocator, &mut parsed.program, scoping, state);
let mut arm_body_byte_spans: BTreeMap<String, Vec<(u32, u32)>> = BTreeMap::new();
for (idx, body_spans) in transform.branch_arm_body_byte_spans.iter().enumerate() {
let surviving =
transform.branch_map.get(idx).is_some_and(|entry| !entry.locations.is_empty());
if surviving {
arm_body_byte_spans.insert(idx.to_string(), body_spans.clone());
}
}
let coverage_map = build_coverage_map(filename, transform, None);
Ok(V8CollectResult { coverage_map, arm_body_byte_spans })
}
struct EmitInputs<'a, 'arena> {
program: &'a Program<'arena>,
scoping: Scoping,
source: &'a str,
filename: &'a str,
preamble: &'a str,
options: &'a InstrumentOptions,
}
fn emit_code(inputs: EmitInputs<'_, '_>) -> (String, Option<oxc_sourcemap::SourceMap>) {
let EmitInputs { program, scoping, source, filename, preamble, options } = inputs;
let codegen_options = CodegenOptions {
source_map_path: if options.source_map { Some(PathBuf::from(filename)) } else { None },
..CodegenOptions::default()
};
let codegen_ret = Codegen::new()
.with_options(codegen_options)
.with_source_text(source)
.with_scoping(Some(scoping))
.build(program);
let code = format!("{preamble}{}", codegen_ret.code);
(code, codegen_ret.map)
}
fn finalize_source_map(
sm: &oxc_sourcemap::SourceMap,
preamble: &str,
input_source_map: Option<&str>,
) -> String {
let preamble_lines =
u32::try_from(preamble.chars().filter(|&c| c == '\n').count()).unwrap_or(u32::MAX);
let output_json = sm.to_json_string();
let Ok(output_sm) = srcmap_sourcemap::SourceMap::from_json(&output_json) else {
return output_json;
};
let offset_sm = if preamble_lines > 0 {
let mut builder = srcmap_remapping::ConcatBuilder::new(None);
builder.add_map(&output_sm, preamble_lines);
builder.build()
} else {
output_sm
};
if let Some(input_sm_json) = input_source_map
&& let Ok(input_sm) = srcmap_sourcemap::SourceMap::from_json(input_sm_json)
{
let composed = srcmap_remapping::remap(&offset_sm, |_name: &str| Some(input_sm.clone()));
return composed.to_json();
}
offset_sm.to_json()
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum InstrumentError {
ParseError(String),
InvalidCoverageVariable(String),
SerializationError(String),
TransformError(Vec<String>),
}
impl std::fmt::Display for InstrumentError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ParseError(msg) => write!(f, "parse error: {msg}"),
Self::SerializationError(msg) => write!(f, "serialization error: {msg}"),
Self::TransformError(msgs) => write!(f, "transform error: {}", msgs.join("; ")),
Self::InvalidCoverageVariable(name) => {
write!(
f,
"invalid coverage variable: {name:?} is not a valid JavaScript identifier"
)
}
}
}
}
impl std::error::Error for InstrumentError {}