From 815db57a25dcf137a4291a40d7085492b443b8c4 Mon Sep 17 00:00:00 2001 From: Yunfei He Date: Sun, 3 Sep 2023 10:16:47 +0800 Subject: [PATCH] fix(semantic): symbol of identifier of top level function declaration should be in the root scope (#843) --------- Co-authored-by: Boshen --- crates/oxc_semantic/src/builder.rs | 3 +-- crates/oxc_semantic/src/lib.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 3423f1607..f21250e15 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -252,8 +252,7 @@ impl<'a> SemanticBuilder<'a> { } let includes = includes | self.current_symbol_flags; - let symbol_id = - self.symbols.create_symbol(span, name.clone(), includes, self.current_scope_id); + let symbol_id = self.symbols.create_symbol(span, name.clone(), includes, scope_id); self.symbols.add_declaration(self.current_node_id); self.scope.add_binding(scope_id, name.clone(), symbol_id); symbol_id diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index 00b414720..e57d21abc 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -169,6 +169,20 @@ mod tests { assert_eq!(references.count(), 1); } + #[test] + fn test_top_level_symbols() { + let source = "function Fn() {}"; + let allocator = Allocator::default(); + let semantic = get_semantic(&allocator, source, SourceType::default()); + + let top_level_a = semantic + .scopes() + .iter_bindings() + .find(|(_scope_id, _symbol_id, name)| name == &Atom::from("Fn")) + .unwrap(); + assert_eq!(semantic.symbols.get_scope_id(top_level_a.1), top_level_a.0); + } + #[test] fn test_is_global() { let source = "