use oxc_ast::ast::{
Argument, ArrayExpression, ArrowFunctionExpression, BlockStatement, CallExpression, Class,
Expression, Function, FunctionBody, LogicalExpression, ObjectExpression, Statement, SwitchCase,
ThrowStatement, VariableDeclaration, VariableDeclarationKind,
};
use oxc_ast_visit::{Visit, walk};
use oxc_span::{GetSpan, Span};
use oxc_syntax::scope::ScopeFlags;
use super::{Candidate, Collector, has_optional_link, is_skipped_call, pair};
use crate::{MutantChange, Operator};
const METHODS: [(&str, Option<&str>); 35] = [
("charAt", None),
("filter", None),
("reverse", None),
("slice", None),
("sort", None),
("substr", None),
("substring", None),
("trim", None),
("endsWith", Some("startsWith")),
("startsWith", Some("endsWith")),
("every", Some("some")),
("some", Some("every")),
("toLocaleLowerCase", Some("toLocaleUpperCase")),
("toLocaleUpperCase", Some("toLocaleLowerCase")),
("toLowerCase", Some("toUpperCase")),
("toUpperCase", Some("toLowerCase")),
("trimEnd", Some("trimStart")),
("trimStart", Some("trimEnd")),
("min", Some("max")),
("max", Some("min")),
("setDate", Some("setTime")),
("setTime", Some("setDate")),
("setFullYear", Some("setMonth")),
("setMonth", Some("setFullYear")),
("setHours", Some("setMinutes")),
("setMinutes", Some("setHours")),
("setSeconds", Some("setMilliseconds")),
("setMilliseconds", Some("setSeconds")),
("setUTCDate", Some("setTime")),
("setUTCFullYear", Some("setUTCMonth")),
("setUTCMonth", Some("setUTCFullYear")),
("setUTCHours", Some("setUTCMinutes")),
("setUTCMinutes", Some("setUTCHours")),
("setUTCSeconds", Some("setUTCMilliseconds")),
("setUTCMilliseconds", Some("setUTCSeconds")),
];
fn method_replacement(name: &str) -> Option<(&'static str, Option<&'static str>)> {
METHODS.iter().find(|(method, _)| *method == name).copied()
}
fn is_movable(statements: &[Statement<'_>]) -> bool {
if statements.iter().any(Statement::is_declaration) {
return false;
}
let mut finder = HoistedDeclaration::default();
for statement in statements {
finder.visit_statement(statement);
}
!finder.found
}
#[derive(Default)]
struct HoistedDeclaration {
found: bool,
}
impl<'a> Visit<'a> for HoistedDeclaration {
fn visit_variable_declaration(&mut self, it: &VariableDeclaration<'a>) {
self.found |= it.kind == VariableDeclarationKind::Var;
walk::walk_variable_declaration(self, it);
}
fn visit_function(&mut self, it: &Function<'a>, _flags: ScopeFlags) {
self.found |= it.is_declaration();
}
fn visit_arrow_function_expression(&mut self, _it: &ArrowFunctionExpression<'a>) {}
fn visit_class(&mut self, _it: &Class<'a>) {}
}
fn calls_super(visit: impl FnOnce(&mut SuperCall)) -> bool {
let mut finder = SuperCall::default();
visit(&mut finder);
finder.found
}
#[derive(Default)]
struct SuperCall {
found: bool,
}
impl<'a> Visit<'a> for SuperCall {
fn visit_call_expression(&mut self, it: &CallExpression<'a>) {
self.found |= matches!(it.callee, Expression::Super(_));
walk::walk_call_expression(self, it);
}
}
fn quote_string(value: &str, quote: char) -> String {
let mut text = String::with_capacity(value.len() + 2);
text.push(quote);
for ch in value.chars() {
match ch {
'\\' => text.push_str("\\\\"),
'\n' => text.push_str("\\n"),
'\r' => text.push_str("\\r"),
'\u{2028}' => text.push_str("\\u2028"),
'\u{2029}' => text.push_str("\\u2029"),
_ if ch == quote => {
text.push('\\');
text.push(ch);
}
_ => text.push(ch),
}
}
text.push(quote);
text
}
impl Collector<'_> {
pub(super) fn empty_function_body(&mut self, body: &FunctionBody<'_>) {
let Some(first) = body.statements.first() else {
return;
};
if !is_movable(&body.statements)
|| (self.in_constructor() && calls_super(|finder| finder.visit_function_body(body)))
{
return;
}
let start = if body.directives.is_empty() {
body.span.start + 1
} else {
first.span().start
};
self.empty_statements((start, body.span.end - 1));
}
pub(super) fn empty_block(&mut self, it: &BlockStatement<'_>) {
if it.body.is_empty()
|| !is_movable(&it.body)
|| (self.in_constructor() && calls_super(|finder| finder.visit_block_statement(it)))
{
return;
}
self.empty_statements((it.span.start + 1, it.span.end - 1));
}
fn in_constructor(&self) -> bool {
self.frames
.iter()
.rev()
.find(|frame| !frame.arrow)
.is_some_and(|frame| frame.constructor)
}
fn empty_statements(&mut self, region: (u32, u32)) {
self.add_statement(
region,
Operator::BlockStatement,
"",
MutantChange::new(MutantChange::STATEMENTS, ""),
);
}
pub(super) fn empty_case(&mut self, it: &SwitchCase<'_>) {
let (Some(first), Some(last)) = (it.consequent.first(), it.consequent.last()) else {
return;
};
if !is_movable(&it.consequent) {
return;
}
self.add_statement(
(first.span().start, last.span().end),
Operator::SwitchCase,
"",
MutantChange::new(MutantChange::STATEMENTS, ""),
);
}
pub(super) fn remove_throw(&mut self, it: &ThrowStatement<'_>) {
if matches!(
it.argument.without_parentheses(),
Expression::NewExpression(_)
) {
self.add_statement(
pair(it.span),
Operator::ThrowStatement,
"{}",
MutantChange::new(MutantChange::STATEMENT, "{}"),
);
}
}
pub(super) fn nullish_fallback(&mut self, it: &LogicalExpression<'_>, token_start: u32) {
self.add(
it.span,
Operator::NullishFallback,
(token_start, it.right.span().end),
String::new(),
MutantChange::new(MutantChange::FALLBACK, ""),
);
}
pub(super) fn array_literal(&mut self, it: &ArrayExpression<'_>) {
let (replacement, change) = if it.elements.is_empty() {
("[\"fallow\"]", MutantChange::new("[]", "[\"fallow\"]"))
} else {
("[]", MutantChange::new(MutantChange::ARRAY, "[]"))
};
self.add(
it.span,
Operator::ArrayLiteral,
pair(it.span),
replacement.to_owned(),
change,
);
}
pub(super) fn object_literal(&mut self, it: &ObjectExpression<'_>) {
if !it.properties.is_empty() {
self.add(
it.span,
Operator::ObjectLiteral,
pair(it.span),
"{}".to_owned(),
MutantChange::new(MutantChange::OBJECT, "{}"),
);
}
}
pub(super) fn call_candidates(&mut self, it: &CallExpression<'_>) {
if it.optional || has_optional_link(&it.callee) {
return;
}
self.method_expression(it);
self.call_expression(it);
}
fn method_expression(&mut self, it: &CallExpression<'_>) {
let Expression::StaticMemberExpression(member) = &it.callee else {
return;
};
if matches!(member.object, Expression::Super(_)) {
return;
}
let Some((name, other)) = method_replacement(member.property.name.as_str()) else {
return;
};
if let Some(other) = other {
self.add(
it.span,
Operator::MethodExpression,
pair(member.property.span),
other.to_owned(),
MutantChange::new(name, other),
);
return;
}
let object_end = member.object.span().end;
let start = self
.find_token(object_end, member.property.span.start, ".")
.unwrap_or(object_end);
self.add(
it.span,
Operator::MethodExpression,
(start, it.span.end),
String::new(),
MutantChange::new(MutantChange::CALL, ""),
);
}
fn call_expression(&mut self, it: &CallExpression<'_>) {
let span = pair(it.span);
if is_skipped_call(it) || self.covered_calls.contains(&span) {
return;
}
self.add(
it.span,
Operator::CallExpression,
span,
"(void 0)".to_owned(),
MutantChange::new(MutantChange::CALL, "void 0"),
);
}
pub(super) fn regex_constructor(
&mut self,
callee: &Expression<'_>,
arguments: &[Argument<'_>],
) {
if !callee.is_specific_id("RegExp") {
return;
}
let Some(Expression::StringLiteral(pattern)) =
arguments.first().and_then(Argument::as_expression)
else {
return;
};
let flags = match arguments.get(1).map(|argument| argument.as_expression()) {
None => "",
Some(Some(Expression::StringLiteral(flags))) => match flags.value.as_str() {
Some(flags) => flags,
None => return,
},
Some(_) => return,
};
let Some(value) = pattern.value.as_str() else {
return;
};
let quote = match self.byte(pattern.span.start) {
Some(b'"') => '"',
Some(b'\'') => '\'',
_ => return,
};
for (mutated, change) in crate::regex::pattern_mutations(value, flags) {
self.add(
pattern.span,
Operator::RegexConstructor,
pair(pattern.span),
quote_string(&mutated, quote),
change,
);
}
}
fn add_statement(
&mut self,
region: (u32, u32),
operator: Operator,
replacement: &str,
change: MutantChange,
) {
self.add_site(
Span::new(region.0, region.1),
true,
Candidate {
operator,
region,
replacement: replacement.to_owned(),
change,
},
);
}
}