Skip to content

Commit

Permalink
feat(semantic): distinguish type imports in ModuleRecord (#2785)
Browse files Browse the repository at this point in the history
I am not sure moving `ImportOrExportKind` to `oxc-syntax` is a good
solution.
  • Loading branch information
Dunqing committed Mar 23, 2024
1 parent 4a42c5f commit 712b3d2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions crates/oxc_semantic/src/module_record/builder.rs
Expand Up @@ -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,
});
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_semantic/src/module_record/mod.rs
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_syntax/src/module_record.rs
Expand Up @@ -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`
Expand Down

0 comments on commit 712b3d2

Please sign in to comment.