From f3035f1bbe5bb4d6ac9b6dcae97b3dbd818ea31e Mon Sep 17 00:00:00 2001 From: Dunqing Date: Mon, 5 Feb 2024 13:16:05 +0800 Subject: [PATCH] feat(semantic): apply ImportSpecifier's binder and remove ModuleDeclaration's binder (#2307) Added in #2230, But i forgot to call. --- crates/oxc_linter/src/utils/jest.rs | 10 +++------- crates/oxc_semantic/src/binder.rs | 16 +--------------- crates/oxc_semantic/src/builder.rs | 26 +++++++++++++++----------- 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/crates/oxc_linter/src/utils/jest.rs b/crates/oxc_linter/src/utils/jest.rs index d23819bb5..cf043f815 100644 --- a/crates/oxc_linter/src/utils/jest.rs +++ b/crates/oxc_linter/src/utils/jest.rs @@ -2,8 +2,7 @@ use std::borrow::Cow; use oxc_ast::{ ast::{ - CallExpression, Expression, ImportDeclaration, ImportDeclarationSpecifier, - ModuleDeclaration, TemplateLiteral, + CallExpression, Expression, ImportDeclaration, ImportDeclarationSpecifier, TemplateLiteral, }, AstKind, }; @@ -201,11 +200,8 @@ fn collect_ids_referenced_to_import<'a, 'b>( .filter_map(|(symbol_id, reference_ids)| { if ctx.symbols().get_flag(symbol_id).is_import_binding() { let id = ctx.symbols().get_declaration(symbol_id); - let node = ctx.nodes().get_node(id); - let AstKind::ModuleDeclaration(module_decl) = node.kind() else { - return None; - }; - let ModuleDeclaration::ImportDeclaration(import_decl) = module_decl else { + let Some(AstKind::ImportDeclaration(import_decl)) = ctx.nodes().parent_kind(id) + else { return None; }; let name = ctx.symbols().get_name(symbol_id); diff --git a/crates/oxc_semantic/src/binder.rs b/crates/oxc_semantic/src/binder.rs index 8d64f4a95..fa639a29b 100644 --- a/crates/oxc_semantic/src/binder.rs +++ b/crates/oxc_semantic/src/binder.rs @@ -274,25 +274,11 @@ impl<'a> Binder for CatchClause<'a> { } } -impl<'a> Binder for ModuleDeclaration<'a> { - fn bind(&self, builder: &mut SemanticBuilder) { - self.bound_names(&mut |ident| { - let symbol_id = builder.declare_symbol( - ident.span, - &ident.name, - SymbolFlags::ImportBinding, - SymbolFlags::ImportBindingExcludes, - ); - ident.symbol_id.set(Some(symbol_id)); - }); - } -} - fn declare_symbol_for_import_specifier(ident: &BindingIdentifier, builder: &mut SemanticBuilder) { let symbol_id = builder.declare_symbol( ident.span, &ident.name, - SymbolFlags::ImportBinding, + SymbolFlags::Import | SymbolFlags::ImportBinding, SymbolFlags::ImportBindingExcludes, ); ident.symbol_id.set(Some(symbol_id)); diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 4ff910f98..b524dfd6b 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -1642,8 +1642,18 @@ impl<'a> SemanticBuilder<'a> { fn enter_kind(&mut self, kind: AstKind<'a>) { match kind { AstKind::ModuleDeclaration(decl) => { - self.current_symbol_flags |= Self::symbol_flag_from_module_declaration(decl); - decl.bind(self); + if !matches!(decl, ModuleDeclaration::ImportDeclaration(_)) { + self.current_symbol_flags |= SymbolFlags::Export; + } + } + AstKind::ImportSpecifier(specifier) => { + specifier.bind(self); + } + AstKind::ImportDefaultSpecifier(specifier) => { + specifier.bind(self); + } + AstKind::ImportNamespaceSpecifier(specifier) => { + specifier.bind(self); } AstKind::VariableDeclarator(decl) => { decl.bind(self); @@ -1750,7 +1760,9 @@ impl<'a> SemanticBuilder<'a> { self.class_table_builder.pop_class(); } AstKind::ModuleDeclaration(decl) => { - self.current_symbol_flags -= Self::symbol_flag_from_module_declaration(decl); + if !matches!(decl, ModuleDeclaration::ImportDeclaration(_)) { + self.current_symbol_flags -= SymbolFlags::Export; + } } AstKind::LabeledStatement(_) => self.label_builder.leave(), AstKind::StaticBlock(_) | AstKind::Function(_) => { @@ -1886,12 +1898,4 @@ impl<'a> SemanticBuilder<'a> { } } } - - fn symbol_flag_from_module_declaration(module: &ModuleDeclaration) -> SymbolFlags { - if matches!(module, ModuleDeclaration::ImportDeclaration(_)) { - SymbolFlags::Import - } else { - SymbolFlags::Export - } - } }