Skip to content

Commit

Permalink
fix(linter/import): ignore export declaration in no-duplicates (#2863)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Mar 30, 2024
1 parent c452897 commit df62828
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules/import/no_duplicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl Rule for NoDuplicates {
let import_entries_maps = group
.into_iter()
.flat_map(|(_path, requested_modules)| requested_modules)
.filter(|requested_module| requested_module.is_import())
.into_group_map_by(|requested_module| {
// We should early return if there is no type import
if !has_type_import {
Expand Down Expand Up @@ -136,6 +137,7 @@ fn test() {
(r"import { type x } from './foo'; import y from './foo'", None),
(r"import { type x } from './foo'; import { y } from './foo'", None),
(r"import { type x } from './foo'; import type y from 'foo'", None),
(r"import { x } from './foo'; export { x } from './foo'", None),
];

let fail = vec![
Expand Down
22 changes: 17 additions & 5 deletions crates/oxc_semantic/src/module_record/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ impl ModuleRecordBuilder {
self.module_record
}

fn add_module_request(&mut self, name_span: &NameSpan, is_type: bool) {
fn add_module_request(&mut self, name_span: &NameSpan, is_type: bool, is_import: bool) {
self.module_record
.requested_modules
.entry(name_span.name().clone())
.or_default()
.push(RequestedModule::new(name_span.span(), is_type));
.push(RequestedModule::new(name_span.span(), is_type, is_import));
}

fn add_import_entry(&mut self, entry: ImportEntry) {
Expand Down Expand Up @@ -202,7 +202,11 @@ impl ModuleRecordBuilder {
});
}
}
self.add_module_request(&module_request, decl.import_kind.is_type());
self.add_module_request(
&module_request,
decl.import_kind.is_type(),
/* is_import */ true,
);
}

fn visit_export_all_declaration(&mut self, decl: &ExportAllDeclaration) {
Expand All @@ -226,7 +230,11 @@ impl ModuleRecordBuilder {
if let Some(exported_name) = &decl.exported {
self.add_export_binding(exported_name.name().to_compact_str(), exported_name.span());
}
self.add_module_request(&module_request, decl.export_kind.is_type());
self.add_module_request(
&module_request,
decl.export_kind.is_type(),
/* is_import */ false,
);
}

fn visit_export_default_declaration(&mut self, decl: &ExportDefaultDeclaration) {
Expand Down Expand Up @@ -273,7 +281,11 @@ impl ModuleRecordBuilder {
.map(|source| NameSpan::new(source.value.to_compact_str(), source.span));

if let Some(module_request) = &module_request {
self.add_module_request(module_request, decl.export_kind.is_type());
self.add_module_request(
module_request,
decl.export_kind.is_type(),
/* is_import */ false,
);
}

if let Some(decl) = &decl.declaration {
Expand Down
10 changes: 8 additions & 2 deletions crates/oxc_syntax/src/module_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,13 @@ pub struct FunctionMeta {
pub struct RequestedModule {
span: Span,
is_type: bool,
/// is_import is true if the module is requested by an import statement.
is_import: bool,
}

impl RequestedModule {
pub fn new(span: Span, is_type: bool) -> Self {
Self { span, is_type }
pub fn new(span: Span, is_type: bool, is_import: bool) -> Self {
Self { span, is_type, is_import }
}

pub fn span(&self) -> Span {
Expand All @@ -285,6 +287,10 @@ impl RequestedModule {
pub fn is_type(&self) -> bool {
self.is_type
}

pub fn is_import(&self) -> bool {
self.is_import
}
}

#[cfg(test)]
Expand Down

0 comments on commit df62828

Please sign in to comment.