From d93c9107e3d64556392945b83416807d04f0b462 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 2 Dec 2024 16:30:01 +0800 Subject: [PATCH] refactor(syntax): remove `ModuleRecord::exported_bindings_duplicated` because it is a syntax error --- crates/oxc_linter/src/module_record.rs | 9 ------- crates/oxc_linter/src/rules/import/export.rs | 25 ++++++------------- .../src/snapshots/import_export.snap | 11 -------- crates/oxc_parser/src/module_record.rs | 6 +++-- crates/oxc_syntax/src/module_record.rs | 4 --- 5 files changed, 12 insertions(+), 43 deletions(-) diff --git a/crates/oxc_linter/src/module_record.rs b/crates/oxc_linter/src/module_record.rs index d0352aa2404290..05343546f2d929 100644 --- a/crates/oxc_linter/src/module_record.rs +++ b/crates/oxc_linter/src/module_record.rs @@ -79,9 +79,6 @@ pub struct ModuleRecord { /// Local exported bindings pub exported_bindings: FxHashMap, - /// Local duplicated exported bindings, for diagnostics - pub exported_bindings_duplicated: Vec, - /// Reexported bindings from `export * from 'specifier'` /// Keyed by resolved path pub exported_bindings_from_star_export: FxDashMap>, @@ -111,7 +108,6 @@ impl fmt::Debug for ModuleRecord { .field("indirect_export_entries", &self.indirect_export_entries) .field("star_export_entries", &self.star_export_entries) .field("exported_bindings", &self.exported_bindings) - .field("exported_bindings_duplicated", &self.exported_bindings_duplicated) .field("exported_bindings_from_star_export", &self.exported_bindings_from_star_export) .field("export_default", &self.export_default) .finish() @@ -468,11 +464,6 @@ impl ModuleRecord { .iter() .map(|(name, span)| (CompactStr::from(name.as_str()), *span)) .collect(), - exported_bindings_duplicated: other - .exported_bindings_duplicated - .iter() - .map(NameSpan::from) - .collect(), export_default: other.export_default, ..ModuleRecord::default() } diff --git a/crates/oxc_linter/src/rules/import/export.rs b/crates/oxc_linter/src/rules/import/export.rs index fc6cc7a75045fe..04bc88f8114180 100644 --- a/crates/oxc_linter/src/rules/import/export.rs +++ b/crates/oxc_linter/src/rules/import/export.rs @@ -92,12 +92,6 @@ impl Rule for Export { }) .collect::>(); - for name_span in &module_record.exported_bindings_duplicated { - if name == name_span.name() { - spans.push(name_span.span()); - } - } - if !spans.is_empty() { spans.push(*span); let labels = spans.into_iter().map(LabeledSpan::underline).collect::>(); @@ -344,16 +338,6 @@ fn test() { // export const Foo = 'bar'; // export namespace Foo { } // "), - (r#" - // declare module "a" { - // const Foo = 1; - // export {Foo as default}; - // } - const Bar = 2; - export {Bar as default}; - const Baz = 3; - export {Baz as default}; - "#), ]; Tester::new(Export::NAME, Export::CATEGORY, pass, fail) @@ -364,7 +348,14 @@ fn test() { { let pass = vec!["export * from './module'"]; - let fail = vec![]; + let fail = vec![ + (" + const Bar = 2; + export {Bar as default}; + const Baz = 3; + export {Baz as default}; + "), + ]; Tester::new(Export::NAME, Export::CATEGORY, pass, fail) .with_import_plugin(true) .change_rule_path("export-star-4/index.js") diff --git a/crates/oxc_linter/src/snapshots/import_export.snap b/crates/oxc_linter/src/snapshots/import_export.snap index 6df1bdc54aa423..f81057363cddd6 100644 --- a/crates/oxc_linter/src/snapshots/import_export.snap +++ b/crates/oxc_linter/src/snapshots/import_export.snap @@ -97,14 +97,3 @@ snapshot_kind: text · ╰── It can not be redeclared here 4 │ export namespace Foo { } ╰──── - - ⚠ eslint-plugin-import(export): Multiple exports of name 'default'. - ╭─[index.ts:7:32] - 6 │ const Bar = 2; - 7 │ export {Bar as default}; - · ─────── - 8 │ const Baz = 3; - 9 │ export {Baz as default}; - · ─────── - 10 │ - ╰──── diff --git a/crates/oxc_parser/src/module_record.rs b/crates/oxc_parser/src/module_record.rs index da8ee01604798e..a3ae47078322f6 100644 --- a/crates/oxc_parser/src/module_record.rs +++ b/crates/oxc_parser/src/module_record.rs @@ -12,6 +12,7 @@ pub struct ModuleRecordBuilder<'a> { module_record: ModuleRecord<'a>, export_entries: Vec>, export_default_duplicated: Vec, + exported_bindings_duplicated: Vec>, } impl<'a> ModuleRecordBuilder<'a> { @@ -21,6 +22,7 @@ impl<'a> ModuleRecordBuilder<'a> { module_record: ModuleRecord::new(allocator), export_entries: vec![], export_default_duplicated: vec![], + exported_bindings_duplicated: vec![], } } @@ -37,7 +39,7 @@ impl<'a> ModuleRecordBuilder<'a> { let module_record = &self.module_record; // It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries. - for name_span in &module_record.exported_bindings_duplicated { + for name_span in &self.exported_bindings_duplicated { let old_span = module_record.exported_bindings[&name_span.name]; errors.push(diagnostics::duplicate_export(&name_span.name, name_span.span, old_span)); } @@ -88,7 +90,7 @@ impl<'a> ModuleRecordBuilder<'a> { fn add_export_binding(&mut self, name: Atom<'a>, span: Span) { if let Some(old_node) = self.module_record.exported_bindings.insert(name.clone(), span) { - self.module_record.exported_bindings_duplicated.push(NameSpan::new(name, old_node)); + self.exported_bindings_duplicated.push(NameSpan::new(name, old_node)); } } diff --git a/crates/oxc_syntax/src/module_record.rs b/crates/oxc_syntax/src/module_record.rs index 030ad2df5cdfbe..c81c6d3aca23af 100644 --- a/crates/oxc_syntax/src/module_record.rs +++ b/crates/oxc_syntax/src/module_record.rs @@ -56,9 +56,6 @@ pub struct ModuleRecord<'a> { /// Local exported bindings pub exported_bindings: FxHashMap, Span>, - /// Local duplicated exported bindings, for diagnostics - pub exported_bindings_duplicated: Vec<'a, NameSpan<'a>>, - /// `export default name` /// ^^^^^^^ span pub export_default: Option, @@ -75,7 +72,6 @@ impl<'a> ModuleRecord<'a> { indirect_export_entries: Vec::new_in(allocator), star_export_entries: Vec::new_in(allocator), exported_bindings: FxHashMap::default(), - exported_bindings_duplicated: Vec::new_in(allocator), export_default: None, } }