Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.
Rust agent-skillsclaude-codeclicode-migrationcodexffigolangjavajavascriptmcpmigrationnapiperformanceportingpyo3pythonrewriterubyrusttypescript
//! How many times each name is bound in the file (declaration, parameter, `catch`, import,
//! function or class name), and which names are parameters.
//!
//! Scopes are not followed: a name bound only once stands for the same thing everywhere, so its
//! origin is proven; a name bound several times can stand for something else depending on the
//! place, so its origin is unknown.
use std::collections::{HashMap, HashSet};
use oxc_ast::ast::{BindingIdentifier, FormalParameter, FormalParameterRest, Program};
use oxc_ast_visit::{Visit, walk};
#[derive(Default)]
pub(super) struct Bindings {
counts: HashMap<String, u32>,
/// The names bound by a function parameter: their value comes from the caller.
params: HashSet<String>,
/// Depth inside a parameter (the names of a `{ a, b }` pattern are parameters too).
in_param: u32,
}
impl Bindings {
pub(super) fn read(program: &Program<'_>) -> Self {
let mut reader = Self::default();
reader.visit_program(program);
reader
}
/// Is the name bound more than once in the file?
pub(super) fn is_ambiguous(&self, name: &str) -> bool {
self.counts.get(name).is_some_and(|n| *n > 1)
}
/// How many times the name is bound in the file.
pub(super) fn count(&self, name: &str) -> u32 {
self.counts.get(name).copied().unwrap_or(0)
}
/// Is the name bound at least once in the file?
pub(super) fn is_bound(&self, name: &str) -> bool {
self.counts.contains_key(name)
}
/// Is the name (at least once) a function parameter?
pub(super) fn is_param(&self, name: &str) -> bool {
self.params.contains(name)
}
}
impl<'a> Visit<'a> for Bindings {
fn visit_binding_identifier(&mut self, it: &BindingIdentifier<'a>) {
let name = it.name.as_str().to_string();
if self.in_param > 0 {
self.params.insert(name.clone());
}
*self.counts.entry(name).or_insert(0) += 1;
walk::walk_binding_identifier(self, it);
}
fn visit_formal_parameter(&mut self, it: &FormalParameter<'a>) {
self.in_param += 1;
walk::walk_formal_parameter(self, it);
self.in_param -= 1;
}
fn visit_formal_parameter_rest(&mut self, it: &FormalParameterRest<'a>) {
self.in_param += 1;
walk::walk_formal_parameter_rest(self, it);
self.in_param -= 1;
}
}