use oxc_ast::ast::*;
use std::collections::{HashMap, HashSet};
use crate::types::JSDocData;
use crate::types::ZigType;
pub mod ast_walk;
pub mod expr;
pub mod fn_types;
pub mod helpers;
pub mod passes;
pub use helpers::binding_name;
#[derive(Debug, Clone, PartialEq)]
pub enum InferResult {
Definite(ZigType),
Indeterminate,
}
#[derive(Debug, Clone)]
pub struct TypeCheckResult {
pub var_types: HashMap<String, ZigType>,
pub array_element_types: HashMap<String, ZigType>,
pub fn_return_types: HashMap<String, ZigType>,
pub fn_param_types: HashMap<String, Vec<(String, ZigType)>>,
pub mutated_vars: HashSet<String>,
pub reassigned_vars: HashSet<String>,
pub used_names: HashSet<String>,
pub has_json_parse_types: HashSet<String>,
pub errors: Vec<String>,
pub is_async: HashMap<String, bool>,
pub class_field_types: HashMap<String, HashMap<String, ZigType>>,
pub host_return_types: HashMap<String, ZigType>,
pub functions_needing_synthetic_rest: HashSet<String>,
}
pub struct TypeInferrer {
pub(crate) var_types: HashMap<String, ZigType>,
pub(crate) array_element_types: HashMap<String, ZigType>,
pub(crate) fn_return_types: HashMap<String, ZigType>,
pub(crate) fn_param_types: HashMap<String, Vec<(String, ZigType)>>,
pub(crate) mutated_vars: HashSet<String>,
pub(crate) reassigned_vars: HashSet<String>,
pub(crate) used_names: HashSet<String>,
pub(crate) has_json_parse_types: HashSet<String>,
pub(crate) is_async: HashMap<String, bool>,
pub errors: Vec<String>,
pub(crate) jsdoc_data: Option<JSDocData>,
pub(crate) exported_functions: Option<HashSet<String>>,
pub(crate) host_return_types: HashMap<String, ZigType>,
pub(crate) functions_needing_synthetic_rest: HashSet<String>,
pub(crate) host_struct_fields: HashMap<String, HashMap<String, ZigType>>,
pub(crate) current_fn: Option<String>,
pub(crate) class_names: HashSet<String>,
pub(crate) class_field_types: HashMap<String, HashMap<String, ZigType>>,
pub(crate) current_class: Option<String>,
}
impl TypeInferrer {
pub fn new() -> Self {
Self {
var_types: HashMap::new(),
array_element_types: HashMap::new(),
fn_return_types: HashMap::new(),
fn_param_types: HashMap::new(),
mutated_vars: HashSet::new(),
reassigned_vars: HashSet::new(),
used_names: HashSet::new(),
has_json_parse_types: HashSet::new(),
is_async: HashMap::new(),
errors: Vec::new(),
jsdoc_data: None,
exported_functions: None,
host_return_types: HashMap::new(),
host_struct_fields: HashMap::new(),
functions_needing_synthetic_rest: HashSet::new(),
current_fn: None,
class_names: HashSet::new(),
class_field_types: HashMap::new(),
current_class: None,
}
}
pub fn set_jsdoc_data(&mut self, data: JSDocData) {
self.jsdoc_data = Some(data);
}
pub fn set_host_fn_types(&mut self, host_fns: &crate::host::HostFnRegistry) {
for def in host_fns.iter() {
self.host_return_types
.insert(def.name.clone(), def.ret_type.clone());
if let ZigType::NamedStruct(ref struct_name) = def.ret_type
&& let Some(fields) = host_fns.struct_fields_map().get(struct_name)
{
let field_map: HashMap<String, ZigType> =
fields.iter().map(|(n, t)| (n.clone(), t.clone())).collect();
self.host_struct_fields
.insert(struct_name.clone(), field_map);
}
}
}
pub fn infer_all(
&mut self,
program: &Program,
exported_functions: Option<HashSet<String>>,
) -> TypeCheckResult {
self.exported_functions = exported_functions;
self.analyze_objects(program);
self.collect_used_names(program);
self.walk_toplevel_for_types(program);
TypeCheckResult {
var_types: std::mem::take(&mut self.var_types),
array_element_types: std::mem::take(&mut self.array_element_types),
fn_return_types: std::mem::take(&mut self.fn_return_types),
fn_param_types: std::mem::take(&mut self.fn_param_types),
mutated_vars: std::mem::take(&mut self.mutated_vars),
reassigned_vars: std::mem::take(&mut self.reassigned_vars),
used_names: std::mem::take(&mut self.used_names),
has_json_parse_types: std::mem::take(&mut self.has_json_parse_types),
errors: std::mem::take(&mut self.errors),
is_async: std::mem::take(&mut self.is_async),
class_field_types: std::mem::take(&mut self.class_field_types),
host_return_types: std::mem::take(&mut self.host_return_types),
functions_needing_synthetic_rest: std::mem::take(
&mut self.functions_needing_synthetic_rest,
),
}
}
}