use oxc_ast::ast::{
Declaration, Expression, Function, ModuleExportName, Program, Statement, VariableDeclaration,
};
use oxc_span::GetSpan;
use crate::emit::Insertion;
use crate::RECORD_FUNCTION;
pub struct RecordInput<'a> {
pub path: &'a str,
pub mutants: &'a [(u32, u32)],
pub sites: &'a [(u32, u32)],
}
const fn holds(outer: (u32, u32), inner: (u32, u32)) -> bool {
outer.0 <= inner.0 && inner.1 <= outer.1
}
impl RecordInput<'_> {
fn has_mutant(&self, span: (u32, u32)) -> bool {
self.mutants.iter().any(|&mutant| holds(span, mutant))
}
fn inside_site(&self, offset: u32) -> bool {
self.sites
.iter()
.any(|&(start, end)| start < offset && offset < end)
}
fn key(&self, name: &str) -> String {
format!("{}#{name}", self.path)
}
}
fn is_function_value(expression: &Expression<'_>) -> bool {
matches!(
expression.get_inner_expression(),
Expression::ArrowFunctionExpression(_) | Expression::FunctionExpression(_)
)
}
const fn has_body(function: &Function<'_>) -> bool {
function.body.is_some() && !function.declare
}
fn span_of(node: &impl GetSpan) -> (u32, u32) {
let span = node.span();
(span.start, span.end)
}
fn reassign(input: &RecordInput<'_>, local: &str, name: &str, end: u32) -> Vec<Insertion> {
if input.inside_site(end) {
return Vec::new();
}
vec![Insertion {
offset: end,
text: format!(
" ;{local} = {RECORD_FUNCTION}(\"{}\", {local});",
input.key(name)
),
}]
}
fn wrap_initializers(
input: &RecordInput<'_>,
variables: &VariableDeclaration<'_>,
name_of: impl Fn(&str) -> Option<String>,
) -> Vec<Insertion> {
let mut insertions = Vec::new();
for declarator in &variables.declarations {
let (Some(id), Some(init)) = (declarator.id.get_binding_identifier(), &declarator.init)
else {
continue;
};
let Some(name) = name_of(id.name.as_str()) else {
continue;
};
let span = span_of(init);
if !is_function_value(init)
|| !input.has_mutant(span)
|| input.inside_site(span.0)
|| input.inside_site(span.1)
{
continue;
}
insertions.push(Insertion {
offset: span.0,
text: format!("{RECORD_FUNCTION}(\"{}\", ", input.key(&name)),
});
insertions.push(Insertion {
offset: span.1,
text: ")".to_owned(),
});
}
insertions
}
fn listed_exports(program: &Program<'_>) -> Vec<(String, String)> {
let name = |name: &ModuleExportName<'_>| name.name().to_string();
program
.body
.iter()
.filter_map(|statement| match statement {
Statement::ExportNamedDeclaration(export) if !export.export_kind.is_type() => {
Some(export)
}
_ => None,
})
.flat_map(|export| {
export
.specifiers
.iter()
.filter(|specifier| !specifier.export_kind.is_type())
.map(|specifier| (name(&specifier.local), name(&specifier.exported)))
})
.collect()
}
fn function_wrap(
input: &RecordInput<'_>,
function: &Function<'_>,
exported: impl Fn(&str) -> Option<String>,
) -> Vec<Insertion> {
let Some(id) = &function.id else {
return Vec::new();
};
let local = id.name.as_str();
let span = span_of(function);
match exported(local) {
Some(name) if has_body(function) && input.has_mutant(span) => {
reassign(input, local, &name, span.1)
}
_ => Vec::new(),
}
}
pub fn record_insertions(program: &Program<'_>, input: &RecordInput<'_>) -> Vec<Insertion> {
let listed = listed_exports(program);
let listed_name = |local: &str| {
listed
.iter()
.find(|(candidate, _)| candidate == local)
.map(|(_, exported)| exported.clone())
};
let mut insertions = Vec::new();
for statement in &program.body {
match statement {
Statement::ExportDeclaration(export) => match &export.declaration {
Declaration::FunctionDeclaration(function) => {
insertions.extend(function_wrap(input, function, |local| {
Some(local.to_owned())
}));
}
Declaration::VariableDeclaration(variables) => {
insertions.extend(wrap_initializers(input, variables, |local| {
Some(local.to_owned())
}));
}
_ => {}
},
Statement::FunctionDeclaration(function) => {
insertions.extend(function_wrap(input, function, listed_name));
}
Statement::VariableDeclaration(variables) => {
insertions.extend(wrap_initializers(input, variables, listed_name));
}
_ => {}
}
}
insertions.sort_by_key(|insertion| insertion.offset);
insertions
}