use react_compiler_hir::environment::Environment;
use react_compiler_hir::{is_props_type, HirFunction, InstructionValue};
pub fn optimize_props_method_calls(func: &mut HirFunction, env: &Environment) {
for (_block_id, block) in &func.body.blocks {
let instruction_ids: Vec<_> = block.instructions.clone();
for instr_id in instruction_ids {
let instr = &mut func.instructions[instr_id.0 as usize];
let should_replace = matches!(
&instr.value,
InstructionValue::MethodCall { receiver, .. }
if {
let identifier = &env.identifiers[receiver.identifier.0 as usize];
let ty = &env.types[identifier.type_.0 as usize];
is_props_type(ty)
}
);
if should_replace {
let old = std::mem::replace(
&mut instr.value,
InstructionValue::Debugger { loc: None },
);
match old {
InstructionValue::MethodCall {
property,
args,
loc,
..
} => {
instr.value = InstructionValue::CallExpression {
callee: property,
args,
loc,
};
}
_ => unreachable!(),
}
}
}
}
}