From 1cf7b834316fc938f0a1ea1132237a1cdf17e96b Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Wed, 18 Dec 2024 07:56:13 +0000 Subject: [PATCH] refactor(semantic): simplify handling namespace stack (#7987) We had a BindingIdentifier to `TSModuleDeclarationName` so that we don't need to get binding from name. --- crates/oxc_semantic/src/builder.rs | 13 ++++++------- tasks/coverage/snapshots/semantic_typescript.snap | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 21a894330c0d1..0c236d678ce93 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -1989,11 +1989,10 @@ impl<'a> SemanticBuilder<'a> { } AstKind::TSModuleDeclaration(module_declaration) => { module_declaration.bind(self); - let symbol_id = self - .scope - .get_bindings(self.current_scope_id) - .get(module_declaration.id.name().as_str()) - .copied(); + let symbol_id = match &module_declaration.id { + TSModuleDeclarationName::Identifier(ident) => ident.symbol_id.get(), + TSModuleDeclarationName::StringLiteral(_) => None, + }; self.namespace_stack.push(symbol_id); } AstKind::TSTypeAliasDeclaration(type_alias_declaration) => { @@ -2086,7 +2085,7 @@ impl<'a> SemanticBuilder<'a> { AstKind::CatchParameter(_) => { self.resolve_references_for_current_scope(); } - AstKind::TSModuleBlock(_) => { + AstKind::TSModuleDeclaration(_) => { self.namespace_stack.pop(); } AstKind::TSTypeName(_) => { @@ -2103,7 +2102,7 @@ impl<'a> SemanticBuilder<'a> { } fn make_all_namespaces_valuelike(&mut self) { - for &symbol_id in &self.namespace_stack { + for symbol_id in self.namespace_stack.iter().copied() { let Some(symbol_id) = symbol_id else { continue; }; diff --git a/tasks/coverage/snapshots/semantic_typescript.snap b/tasks/coverage/snapshots/semantic_typescript.snap index d09a00ddc71c5..7798098d799c0 100644 --- a/tasks/coverage/snapshots/semantic_typescript.snap +++ b/tasks/coverage/snapshots/semantic_typescript.snap @@ -35516,7 +35516,7 @@ Scope children mismatch: after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(4), ScopeId(7)] rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)] Symbol flags mismatch for "createElement": -after transform: SymbolId(2): SymbolFlags(BlockScopedVariable | Function | NameSpaceModule | ValueModule) +after transform: SymbolId(2): SymbolFlags(BlockScopedVariable | Function | NameSpaceModule) rebuilt : SymbolId(1): SymbolFlags(BlockScopedVariable | Function) Symbol redeclarations mismatch for "createElement": after transform: SymbolId(2): [Span { start: 243, end: 256 }]