use std::collections::{HashMap, HashSet};
use crate::types::ZigType;
pub struct FnContext {
pub name: String,
pub is_export: bool,
pub return_type: Option<ZigType>,
pub seen_return: bool,
pub fn_has_throw: bool,
pub has_bigint_div: bool,
pub has_catchable_error: bool,
pub js_const_reassigned: HashSet<String>,
pub in_return_expr: bool,
pub in_expr_stmt: bool,
pub call_generated_catch: bool,
pub inside_try_block: Option<String>,
pub current_class: Option<String>,
pub nested_fn_names: HashSet<String>,
pub current_nested_fn_name: Option<String>,
pub fn_scope_vars: HashSet<String>,
pub fn_local_types: HashMap<String, ZigType>,
pub typedarray_vars: HashMap<String, String>,
pub regexp_vars: HashSet<String>,
pub rest_param_name: Option<String>,
pub compile_time_referenced_idents: HashSet<String>,
pub saved_this_rewrite_fields: Option<Vec<String>>,
}
impl FnContext {
pub fn new(name: &str, is_export: bool, return_type: Option<ZigType>) -> Self {
Self {
name: name.to_string(),
is_export,
return_type,
seen_return: false,
fn_has_throw: false,
has_bigint_div: false,
has_catchable_error: false,
js_const_reassigned: HashSet::new(),
in_return_expr: false,
in_expr_stmt: false,
call_generated_catch: false,
inside_try_block: None,
current_class: None,
nested_fn_names: HashSet::new(),
current_nested_fn_name: None,
fn_scope_vars: HashSet::new(),
fn_local_types: HashMap::new(),
typedarray_vars: HashMap::new(),
regexp_vars: HashSet::new(),
rest_param_name: None,
compile_time_referenced_idents: HashSet::new(),
saved_this_rewrite_fields: None,
}
}
pub fn add_scope_var(&mut self, name: &str) {
self.fn_scope_vars.insert(name.to_string());
}
pub fn add_nested_fn(&mut self, name: &str) {
self.nested_fn_names.insert(name.to_string());
}
pub fn is_nested_fn(&self, name: &str) -> bool {
self.nested_fn_names.contains(name)
}
pub fn add_typedarray_var(&mut self, name: &str, suffix: &str) {
self.typedarray_vars
.insert(name.to_string(), suffix.to_string());
}
pub fn add_regexp_var(&mut self, name: &str) {
self.regexp_vars.insert(name.to_string());
}
}
pub struct SetFlagGuard<'a> {
field: &'a mut bool,
old_value: bool,
}
impl<'a> SetFlagGuard<'a> {
pub fn new(field: &'a mut bool, value: bool) -> Self {
let old_value = *field;
*field = value;
Self { field, old_value }
}
}
impl Drop for SetFlagGuard<'_> {
fn drop(&mut self) {
*self.field = self.old_value;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fn_context_new() {
let ctx = FnContext::new("add", true, Some(ZigType::I64));
assert_eq!(ctx.name, "add");
assert!(ctx.is_export);
assert_eq!(ctx.return_type, Some(ZigType::I64));
assert!(!ctx.seen_return);
assert!(!ctx.fn_has_throw);
assert!(ctx.nested_fn_names.is_empty());
}
#[test]
fn test_fn_context_register_vars() {
let mut ctx = FnContext::new("test", false, None);
ctx.add_scope_var("x");
ctx.add_scope_var("y");
assert!(ctx.fn_scope_vars.contains("x"));
assert!(ctx.fn_scope_vars.contains("y"));
ctx.add_nested_fn("helper");
assert!(ctx.is_nested_fn("helper"));
assert!(!ctx.is_nested_fn("other"));
ctx.add_typedarray_var("buf", "U8");
assert_eq!(ctx.typedarray_vars.get("buf"), Some(&"U8".to_string()));
ctx.add_regexp_var("re");
assert!(ctx.regexp_vars.contains("re"));
}
#[test]
fn test_set_flag_guard() {
let mut ctx = FnContext::new("test", false, None);
assert!(!ctx.in_return_expr);
{
let _guard = SetFlagGuard::new(&mut ctx.in_return_expr, true);
}
assert!(!ctx.in_return_expr); }
}
pub(crate) fn stmt_has_throw(stmt: &oxc_ast::ast::Statement) -> bool {
use oxc_ast::ast::Statement;
match stmt {
Statement::ThrowStatement(_) => true,
Statement::BlockStatement(bs) => bs.body.iter().any(|s| stmt_has_throw(s)),
Statement::IfStatement(is) => {
stmt_has_throw(&is.consequent)
|| is.alternate.as_ref().is_some_and(|a| stmt_has_throw(a))
}
Statement::WhileStatement(ws) => stmt_has_throw(&ws.body),
Statement::DoWhileStatement(dws) => stmt_has_throw(&dws.body),
Statement::ForStatement(fs) => stmt_has_throw(&fs.body),
Statement::ForOfStatement(fos) => stmt_has_throw(&fos.body),
Statement::ForInStatement(fis) => stmt_has_throw(&fis.body),
Statement::LabeledStatement(ls) => stmt_has_throw(&ls.body),
Statement::SwitchStatement(ss) => ss
.cases
.iter()
.any(|c| c.consequent.iter().any(|s| stmt_has_throw(s))),
Statement::TryStatement(_) => true,
_ => false,
}
}
pub(crate) fn format_specifier_for_type(ty: &ZigType) -> &'static str {
match ty {
ZigType::Str => "{s}",
ZigType::I64 => "{d}",
ZigType::F64 => "{}",
ZigType::Bool => "{}",
ZigType::JsAny => "{f}",
_ => "{any}",
}
}