feat(semantic): apply ImportSpecifier's binder and remove ModuleDeclaration's binder (#2307)

Added in #2230, But i forgot to call.
This commit is contained in:
Dunqing 2024-02-05 13:16:05 +08:00 committed by GitHub
parent bb9cf484e9
commit f3035f1bbe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 33 deletions

View file

@ -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);

View file

@ -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));

View file

@ -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
}
}
}