Skip to content

Commit

Permalink
feat(semantic): transform checker check child scope IDs (#5410)
Browse files Browse the repository at this point in the history
Transform checker check child scope IDs. If we have to track child scope IDs, we should make sure they're correct!
  • Loading branch information
overlookmotel committed Sep 3, 2024
1 parent d594818 commit be4642f
Show file tree
Hide file tree
Showing 10 changed files with 8,357 additions and 186 deletions.
5 changes: 5 additions & 0 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub trait CompilerInterface {
true
}

fn semantic_child_scope_ids(&self) -> bool {
false
}

fn after_parse(&mut self, _parser_return: &mut ParserReturn) -> ControlFlow<()> {
ControlFlow::Continue(())
}
Expand Down Expand Up @@ -186,6 +190,7 @@ pub trait CompilerInterface {
) -> SemanticBuilderReturn<'a> {
SemanticBuilder::new(source_text, source_type)
.with_check_syntax_error(self.check_semantic_error())
.with_scope_tree_child_ids(self.semantic_child_scope_ids())
.build_module_record(source_path, program)
.build(program)
}
Expand Down
33 changes: 33 additions & 0 deletions crates/oxc_semantic/src/post_transform_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub fn check_semantic_after_transform(
let allocator = Allocator::default();
let program = program.clone_in(&allocator);
let (symbols_rebuilt, scopes_rebuilt) = SemanticBuilder::new("", program.source_type)
.with_scope_tree_child_ids(scopes_after_transform.has_child_ids())
.build(&program)
.semantic
.into_symbol_table_and_scope_tree();
Expand Down Expand Up @@ -366,6 +367,16 @@ impl<'s> PostTransformChecker<'s> {
self.errors.push_mismatch("Scope parent mismatch", scope_ids, parent_ids);
}

// Check children match
if self.scoping_after_transform.scopes.has_child_ids() {
let child_ids = self.get_pair(scope_ids, |scoping, scope_id| {
scoping.scopes.get_child_ids(scope_id).to_vec()
});
if self.remap_scope_ids_sets(&child_ids).is_mismatch() {
self.errors.push_mismatch("Scope children mismatch", scope_ids, child_ids);
}
}

// NB: Skip checking node IDs match - transformer does not set `AstNodeId`s
}
}
Expand Down Expand Up @@ -504,6 +515,28 @@ impl<'s> PostTransformChecker<'s> {
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<V: AsRef<Vec<ScopeId>>>(
&self,
scope_ids: &Pair<V>,
) -> Pair<Vec<Option<ScopeId>>> {
let mut after_transform = scope_ids
.after_transform
.as_ref()
.iter()
.map(|&scope_id| self.scope_ids_map.get(scope_id))
.collect::<Vec<_>>();
let mut rebuilt =
scope_ids.rebuilt.as_ref().iter().copied().map(Option::Some).collect::<Vec<_>>();

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.
Expand Down
Loading

0 comments on commit be4642f

Please sign in to comment.