From fb46eafab63c2ddd3b7b32881ec74cad0b3d3b11 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 22 Aug 2024 23:40:24 +0000 Subject: [PATCH] refactor(semantic): add remap functions to transform checker (#5082) Move remapping into separate functions. --- .../src/post_transform_checker.rs | 68 +++++++++++-------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/crates/oxc_semantic/src/post_transform_checker.rs b/crates/oxc_semantic/src/post_transform_checker.rs index c3ec793106e61..0f2ad9c90697c 100644 --- a/crates/oxc_semantic/src/post_transform_checker.rs +++ b/crates/oxc_semantic/src/post_transform_checker.rs @@ -354,22 +354,7 @@ impl<'s> PostTransformChecker<'s> { let symbol_ids = self.get_pair(scope_ids, |data, scope_id| { data.scopes.get_bindings(scope_id).values().copied().collect::>() }); - - let mut symbol_ids_after_transform = symbol_ids - .after_transform - .iter() - .map(|&symbol_id| self.symbol_ids_map.get(symbol_id)) - .collect::>(); - symbol_ids_after_transform.sort_unstable(); - let mut symbol_ids_rebuilt = symbol_ids - .rebuilt - .iter() - .copied() - .map(Option::Some) - .collect::>(); - symbol_ids_rebuilt.sort_unstable(); - - if symbol_ids_after_transform != symbol_ids_rebuilt { + if self.remap_symbol_ids_sets(&symbol_ids).is_mismatch() { self.errors.push_mismatch( "Binding symbols mismatch", scope_ids, @@ -421,19 +406,7 @@ impl<'s> PostTransformChecker<'s> { // Check children match let child_ids = self .get_pair(scope_ids, |data, scope_id| data.scopes.get_child_ids(scope_id).to_vec()); - let is_match = child_ids.after_transform.len() == child_ids.rebuilt.len() && { - let mut child_ids_after_transform = child_ids - .after_transform - .iter() - .map(|&child_id| self.scope_ids_map.get(child_id)) - .collect::>(); - child_ids_after_transform.sort_unstable(); - let mut child_ids_rebuilt = - child_ids.rebuilt.iter().copied().map(Option::Some).collect::>(); - child_ids_rebuilt.sort_unstable(); - child_ids_after_transform == child_ids_rebuilt - }; - if !is_match { + if self.remap_scope_ids_sets(&child_ids).is_mismatch() { self.errors.push_mismatch("Scope children mismatch", scope_ids, child_ids); } } @@ -542,6 +515,43 @@ impl<'s> PostTransformChecker<'s> { fn remap_scope_ids(&self, scope_ids: Pair) -> Pair> { Pair::new(self.scope_ids_map.get(scope_ids.after_transform), Some(scope_ids.rebuilt)) } + + /// Remap pair of arrays of `ScopeId`s. + /// Map `after_transform` IDs to `rebuilt` IDs. + /// Sort both sets. + fn remap_scope_ids_sets(&self, scope_ids: &Pair>) -> Pair>> { + let mut after_transform = scope_ids + .after_transform + .iter() + .map(|&scope_id| self.scope_ids_map.get(scope_id)) + .collect::>(); + let mut rebuilt = scope_ids.rebuilt.iter().copied().map(Option::Some).collect::>(); + + after_transform.sort_unstable(); + rebuilt.sort_unstable(); + + Pair::new(after_transform, rebuilt) + } + + /// Remap pair of arrays of `SymbolId`s. + /// Map `after_transform` IDs to `rebuilt` IDs. + /// Sort both sets. + fn remap_symbol_ids_sets( + &self, + symbol_ids: &Pair>, + ) -> Pair>> { + let mut after_transform = symbol_ids + .after_transform + .iter() + .map(|&symbol_id| self.symbol_ids_map.get(symbol_id)) + .collect::>(); + let mut rebuilt = symbol_ids.rebuilt.iter().copied().map(Option::Some).collect::>(); + + after_transform.sort_unstable(); + rebuilt.sort_unstable(); + + Pair::new(after_transform, rebuilt) + } } /// Collection of `ScopeId`s, `SymbolId`s and `ReferenceId`s from an AST.