diff --git a/crates/oxc_ast/src/ast_impl/js.rs b/crates/oxc_ast/src/ast_impl/js.rs index 842f332611ddc..1a43cf3112fd1 100644 --- a/crates/oxc_ast/src/ast_impl/js.rs +++ b/crates/oxc_ast/src/ast_impl/js.rs @@ -4,7 +4,7 @@ use std::{ }; use oxc_span::{Atom, GetSpan, Ident, Span}; -use oxc_syntax::{operator::UnaryOperator, scope::ScopeFlags}; +use oxc_syntax::{operator::UnaryOperator, scope::ScopeFlags, symbol::SymbolId}; use crate::ast::*; @@ -1324,6 +1324,38 @@ impl<'a> BindingPattern<'a> { idents } + fn append_symbol_ids(&self, symbol_ids: &mut std::vec::Vec) { + match self { + Self::BindingIdentifier(ident) => { + symbol_ids.push(ident.symbol_id()); + } + Self::AssignmentPattern(assign) => assign.left.append_symbol_ids(symbol_ids), + Self::ArrayPattern(pattern) => { + pattern + .elements + .iter() + .filter_map(|item| item.as_ref()) + .for_each(|item| item.append_symbol_ids(symbol_ids)); + if let Some(rest) = &pattern.rest { + rest.argument.append_symbol_ids(symbol_ids); + } + } + Self::ObjectPattern(pattern) => { + pattern.properties.iter().for_each(|item| item.value.append_symbol_ids(symbol_ids)); + if let Some(rest) = &pattern.rest { + rest.argument.append_symbol_ids(symbol_ids); + } + } + } + } + + /// Returns the [`SymbolId`]s of the bound identifiers in this binding pattern. + pub fn get_symbol_ids(&self) -> std::vec::Vec { + let mut symbol_ids = vec![]; + self.append_symbol_ids(&mut symbol_ids); + symbol_ids + } + /// Returns `true` if all binding identifiers in this pattern satisfy the given predicate. /// /// This method is more efficient than [`BindingPattern::get_binding_identifiers`] followed by [`Iterator::all`]