Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(semantic): add IdMapping to transform checker #5079

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions crates/oxc_semantic/src/post_transform_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
//!
//! See also: <https://github.com/oxc-project/oxc/issues/4790>
use std::{cell::Cell, fmt::Debug};
use std::{cell::Cell, fmt::Debug, hash::Hash};

use oxc_allocator::{Allocator, CloneIn};
#[allow(clippy::wildcard_imports)]
Expand Down Expand Up @@ -142,9 +142,9 @@ pub fn check_semantic_after_transform(
let mut checker = PostTransformChecker {
after_transform: data_after_transform,
rebuilt: data_rebuilt,
scope_ids_map: FxHashMap::default(),
symbol_ids_map: FxHashMap::default(),
reference_ids_map: FxHashMap::default(),
scope_ids_map: IdMapping::default(),
symbol_ids_map: IdMapping::default(),
reference_ids_map: IdMapping::default(),
errors: Errors::default(),
};
checker.create_mappings();
Expand All @@ -159,9 +159,9 @@ struct PostTransformChecker<'s> {
after_transform: SemanticData<'s>,
rebuilt: SemanticData<'s>,
// Mappings from after transform ID to rebuilt ID
scope_ids_map: FxHashMap<ScopeId, ScopeId>,
symbol_ids_map: FxHashMap<SymbolId, SymbolId>,
reference_ids_map: FxHashMap<ReferenceId, ReferenceId>,
scope_ids_map: IdMapping<ScopeId>,
symbol_ids_map: IdMapping<SymbolId>,
reference_ids_map: IdMapping<ReferenceId>,
errors: Errors,
}

Expand All @@ -171,6 +171,25 @@ struct SemanticData<'s> {
ids: SemanticIds,
}

/// Mapping from "after transform" ID to "rebuilt" ID
struct IdMapping<Id>(FxHashMap<Id, Id>);

impl<Id: Copy + Eq + Hash> IdMapping<Id> {
fn insert(&mut self, after_transform_id: Id, rebuilt_id: Id) {
self.0.insert(after_transform_id, rebuilt_id);
}

fn get(&self, after_transform_id: Id) -> Option<Id> {
self.0.get(&after_transform_id).copied()
}
}

impl<Id> Default for IdMapping<Id> {
fn default() -> Self {
Self(FxHashMap::default())
}
}

/// Pair of values from after transform and rebuilt
struct Pair<T> {
after_transform: T,
Expand Down Expand Up @@ -339,7 +358,7 @@ impl<'s> PostTransformChecker<'s> {
let mut symbol_ids_after_transform = symbol_ids
.after_transform
.iter()
.map(|symbol_id| self.symbol_ids_map.get(symbol_id).copied())
.map(|&symbol_id| self.symbol_ids_map.get(symbol_id))
.collect::<Vec<_>>();
symbol_ids_after_transform.sort_unstable();
let mut symbol_ids_rebuilt = symbol_ids
Expand Down Expand Up @@ -406,7 +425,7 @@ impl<'s> PostTransformChecker<'s> {
let mut child_ids_after_transform = child_ids
.after_transform
.iter()
.map(|child_id| self.scope_ids_map.get(child_id).copied())
.map(|&child_id| self.scope_ids_map.get(child_id))
.collect::<Vec<_>>();
child_ids_after_transform.sort_unstable();
let mut child_ids_rebuilt =
Expand Down Expand Up @@ -521,10 +540,7 @@ impl<'s> PostTransformChecker<'s> {

/// Map `after_transform` scope ID to `rebuilt` scope ID
fn remap_scope_ids(&self, scope_ids: Pair<ScopeId>) -> Pair<Option<ScopeId>> {
Pair::new(
self.scope_ids_map.get(&scope_ids.after_transform).copied(),
Some(scope_ids.rebuilt),
)
Pair::new(self.scope_ids_map.get(scope_ids.after_transform), Some(scope_ids.rebuilt))
}
}

Expand Down