feat(semantic): distinguish type imports in ModuleRecord (#2785)

I am not sure moving `ImportOrExportKind` to `oxc-syntax` is a good
solution.
This commit is contained in:
Dunqing 2024-03-23 21:38:17 +08:00 committed by GitHub
parent 4a42c5fd7d
commit 712b3d2a11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 4 deletions

View file

@ -170,33 +170,35 @@ impl ModuleRecordBuilder {
}
fn visit_import_declaration(&mut self, decl: &ImportDeclaration) {
if decl.import_kind.is_type() {
return;
}
let module_request = NameSpan::new(decl.source.value.to_compact_str(), decl.source.span);
if let Some(specifiers) = &decl.specifiers {
for specifier in specifiers {
let (import_name, local_name) = match specifier {
let (import_name, local_name, is_type) = match specifier {
ImportDeclarationSpecifier::ImportSpecifier(specifier) => (
ImportImportName::Name(NameSpan::new(
specifier.imported.name().to_compact_str(),
specifier.imported.span(),
)),
NameSpan::new(specifier.local.name.to_compact_str(), specifier.local.span),
decl.import_kind.is_type() || specifier.import_kind.is_type(),
),
ImportDeclarationSpecifier::ImportNamespaceSpecifier(specifier) => (
ImportImportName::NamespaceObject,
NameSpan::new(specifier.local.name.to_compact_str(), specifier.local.span),
decl.import_kind.is_type(),
),
ImportDeclarationSpecifier::ImportDefaultSpecifier(specifier) => (
ImportImportName::Default(specifier.span),
NameSpan::new(specifier.local.name.to_compact_str(), specifier.local.span),
decl.import_kind.is_type(),
),
};
self.add_import_entry(ImportEntry {
module_request: module_request.clone(),
import_name,
local_name,
is_type,
});
}
}

View file

@ -35,6 +35,7 @@ mod module_record_tests {
module_request: NameSpan::new("mod".into(), Span::new(14, 19)),
import_name: ImportImportName::Default(Span::new(7, 8)),
local_name: NameSpan::new("v".into(), Span::new(7, 8)),
is_type: false,
};
assert_eq!(module_record.import_entries.len(), 1);
assert_eq!(module_record.import_entries[0], import_entry);
@ -47,6 +48,7 @@ mod module_record_tests {
module_request: NameSpan::new("mod".into(), Span::new(20, 25)),
import_name: ImportImportName::NamespaceObject,
local_name: NameSpan::new("ns".into(), Span::new(12, 14)),
is_type: false,
};
assert_eq!(module_record.import_entries.len(), 1);
assert_eq!(module_record.import_entries[0], import_entry);
@ -59,6 +61,7 @@ mod module_record_tests {
module_request: NameSpan::new("mod".into(), Span::new(18, 23)),
import_name: ImportImportName::Name(NameSpan::new("x".into(), Span::new(9, 10))),
local_name: NameSpan::new("x".into(), Span::new(9, 10)),
is_type: false,
};
assert_eq!(module_record.import_entries.len(), 1);
assert_eq!(module_record.import_entries[0], import_entry);
@ -71,6 +74,7 @@ mod module_record_tests {
module_request: NameSpan::new("mod".into(), Span::new(23, 28)),
import_name: ImportImportName::Name(NameSpan::new("x".into(), Span::new(9, 10))),
local_name: NameSpan::new("v".into(), Span::new(14, 15)),
is_type: false,
};
assert_eq!(module_record.import_entries.len(), 1);
assert_eq!(module_record.import_entries[0], import_entry);

View file

@ -148,6 +148,8 @@ pub struct ImportEntry {
/// The name that is used to locally access the imported value from within the importing module.
pub local_name: NameSpan,
pub is_type: bool,
}
/// `ImportName` For `ImportEntry`