From a43472dbe2cee5596f0f8b584fd412714ea321c9 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sun, 26 Jan 2025 14:27:15 +0800 Subject: [PATCH] refactor(semantic): simplify ScopeTree::iter_bindings --- crates/oxc_semantic/src/lib.rs | 8 ++----- crates/oxc_semantic/src/scope.rs | 6 ++--- .../tests/integration/util/symbol_tester.rs | 24 ++++++++++--------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index 175a191ce83f74..c63f235821e881 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -287,13 +287,9 @@ mod tests { let source = "function Fn() {}"; let allocator = Allocator::default(); let semantic = get_semantic(&allocator, source, SourceType::default()); + let scopes = semantic.scopes(); - let top_level_a = semantic - .scopes() - .iter_bindings() - .find(|(_scope_id, _symbol_id, name)| *name == "Fn") - .unwrap(); - assert_eq!(semantic.symbols.get_scope_id(top_level_a.1), top_level_a.0); + assert!(scopes.get_binding(scopes.root_scope_id(), "Fn").is_some()); } #[test] diff --git a/crates/oxc_semantic/src/scope.rs b/crates/oxc_semantic/src/scope.rs index a2bdffb519e74a..19fe9e1c0420fa 100644 --- a/crates/oxc_semantic/src/scope.rs +++ b/crates/oxc_semantic/src/scope.rs @@ -310,10 +310,8 @@ impl ScopeTree { /// If you only want bindings in a specific scope, use [`iter_bindings_in`]. /// /// [`iter_bindings_in`]: ScopeTree::iter_bindings_in - pub fn iter_bindings(&self) -> impl Iterator + '_ { - self.cell.borrow_dependent().bindings.iter_enumerated().flat_map(|(scope_id, bindings)| { - bindings.iter().map(move |(&name, &symbol_id)| (scope_id, symbol_id, name)) - }) + pub fn iter_bindings(&self) -> impl Iterator + '_ { + self.cell.borrow_dependent().bindings.iter_enumerated() } /// Iterate over bindings declared inside a scope. diff --git a/crates/oxc_semantic/tests/integration/util/symbol_tester.rs b/crates/oxc_semantic/tests/integration/util/symbol_tester.rs index 78672bed0855fc..9dcd72403485c7 100644 --- a/crates/oxc_semantic/tests/integration/util/symbol_tester.rs +++ b/crates/oxc_semantic/tests/integration/util/symbol_tester.rs @@ -1,7 +1,7 @@ use std::rc::Rc; use oxc_diagnostics::{Error, OxcDiagnostic}; -use oxc_semantic::{Reference, ScopeFlags, ScopeId, Semantic, SymbolFlags, SymbolId}; +use oxc_semantic::{Reference, ScopeFlags, Semantic, SymbolFlags, SymbolId}; use super::{Expect, SemanticTester}; @@ -65,15 +65,15 @@ impl<'a> SymbolTester<'a> { semantic: Semantic<'a>, target: &str, ) -> Self { - let symbols_with_target_name: Vec<_> = - semantic.scopes().iter_bindings().filter(|(_, _, name)| *name == target).collect(); + let mut symbols_with_target_name: Vec = semantic + .scopes() + .iter_bindings() + .filter_map(|(_, bindings)| bindings.get(target).copied()) + .collect(); + let data = match symbols_with_target_name.len() { 0 => Err(OxcDiagnostic::error(format!("Could not find declaration for {target}"))), - 1 => Ok(symbols_with_target_name - .iter() - .map(|&(_, symbol_id, _)| symbol_id) - .next() - .unwrap()), + 1 => Ok(symbols_with_target_name.pop().unwrap()), n if n > 1 => Err(OxcDiagnostic::error(format!( "Couldn't uniquely resolve symbol id for target {target}; {n} symbols with that name are declared in the source." ))), @@ -98,11 +98,13 @@ impl<'a> SymbolTester<'a> { semantic: Semantic<'a>, target: &str, ) -> Self { - let symbols_with_target_name: Option<(ScopeId, SymbolId, &str)> = - semantic.scopes().iter_bindings().find(|(_, _, name)| *name == target); + let symbols_with_target_name: Option = semantic + .scopes() + .iter_bindings() + .find_map(|(_, bindings)| bindings.get(target).copied()); let data = match symbols_with_target_name { - Some((_, symbol_id, _)) => Ok(symbol_id), + Some(symbol_id) => Ok(symbol_id), None => Err(OxcDiagnostic::error(format!("Could not find declaration for {target}"))), };