Skip to content

Commit

Permalink
refactor(syntax): remove ModuleRecord::export_default_duplicated be…
Browse files Browse the repository at this point in the history
…cause it is a syntax error (#7576)
  • Loading branch information
Boshen committed Dec 2, 2024
1 parent 275d625 commit 17663f5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 54 deletions.
18 changes: 0 additions & 18 deletions crates/oxc_linter/src/module_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ pub struct ModuleRecord {
/// `export default name`
/// ^^^^^^^ span
pub export_default: Option<Span>,

/// Duplicated span of `export default` for diagnostics
pub export_default_duplicated: Vec<Span>,
}

impl fmt::Debug for ModuleRecord {
Expand All @@ -117,7 +114,6 @@ impl fmt::Debug for ModuleRecord {
.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)
.field("export_default_duplicated", &self.export_default_duplicated)
.finish()
}
}
Expand Down Expand Up @@ -477,21 +473,7 @@ impl ModuleRecord {
.iter()
.map(NameSpan::from)
.collect(),
exported_bindings_from_star_export: other
.exported_bindings_from_star_export
.iter()
.map(|(name, values)| {
(
PathBuf::from(name.as_str()),
values
.into_iter()
.map(|v| CompactStr::from(v.as_str()))
.collect::<Vec<_>>(),
)
})
.collect(),
export_default: other.export_default,
export_default_duplicated: other.export_default_duplicated.iter().copied().collect(),
..ModuleRecord::default()
}
}
Expand Down
11 changes: 0 additions & 11 deletions crates/oxc_linter/src/rules/import/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,6 @@ impl Rule for Export {
);
}
}

if !module_record.export_default_duplicated.is_empty() {
let mut spans = module_record.export_default_duplicated.clone();
if let Some(span) = module_record.export_default {
spans.push(span);
let labels = spans.into_iter().map(LabeledSpan::underline).collect::<Vec<_>>();
ctx.diagnostic(
OxcDiagnostic::warn("Multiple default exports.").with_labels(labels),
);
}
}
}
}

Expand Down
19 changes: 6 additions & 13 deletions crates/oxc_linter/src/rules/import/no_default_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,12 @@ declare_oxc_lint!(
impl Rule for NoDefaultExport {
fn run_once(&self, ctx: &LintContext<'_>) {
let module_record = ctx.module_record();
write_diagnostic_optional(ctx, module_record.export_default);
module_record.export_default_duplicated.iter().for_each(|it| write_diagnostic(ctx, *it));
write_diagnostic_optional(ctx, module_record.exported_bindings.get("default").copied());
}
}

fn write_diagnostic(ctx: &LintContext<'_>, span: Span) {
ctx.diagnostic(no_default_export_diagnostic(span));
}

fn write_diagnostic_optional(ctx: &LintContext<'_>, span_option: Option<Span>) {
if let Some(span) = span_option {
write_diagnostic(ctx, span);
if let Some(span) = module_record.export_default {
ctx.diagnostic(no_default_export_diagnostic(span));
}
if let Some(span) = module_record.exported_bindings.get("default") {
ctx.diagnostic(no_default_export_diagnostic(*span));
}
}
}

Expand Down
12 changes: 9 additions & 3 deletions crates/oxc_parser/src/module_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ pub struct ModuleRecordBuilder<'a> {
allocator: &'a Allocator,
module_record: ModuleRecord<'a>,
export_entries: Vec<ExportEntry<'a>>,
export_default_duplicated: Vec<Span>,
}

impl<'a> ModuleRecordBuilder<'a> {
pub fn new(allocator: &'a Allocator) -> Self {
Self { allocator, module_record: ModuleRecord::new(allocator), export_entries: vec![] }
Self {
allocator,
module_record: ModuleRecord::new(allocator),
export_entries: vec![],
export_default_duplicated: vec![],
}
}

pub fn build(mut self) -> ModuleRecord<'a> {
Expand All @@ -36,7 +42,7 @@ impl<'a> ModuleRecordBuilder<'a> {
errors.push(diagnostics::duplicate_export(&name_span.name, name_span.span, old_span));
}

for span in &module_record.export_default_duplicated {
for span in &self.export_default_duplicated {
let old_span = module_record.export_default.unwrap();
errors.push(diagnostics::duplicate_export("default", *span, old_span));
}
Expand Down Expand Up @@ -88,7 +94,7 @@ impl<'a> ModuleRecordBuilder<'a> {

fn add_default_export(&mut self, span: Span) {
if let Some(old_node) = self.module_record.export_default.replace(span) {
self.module_record.export_default_duplicated.push(old_node);
self.export_default_duplicated.push(old_node);
}
}

Expand Down
9 changes: 0 additions & 9 deletions crates/oxc_syntax/src/module_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,9 @@ pub struct ModuleRecord<'a> {
/// Local duplicated exported bindings, for diagnostics
pub exported_bindings_duplicated: Vec<'a, NameSpan<'a>>,

/// Reexported bindings from `export * from 'specifier'`
/// Keyed by resolved path
pub exported_bindings_from_star_export: FxHashMap<Atom<'a>, Vec<'a, Atom<'a>>>,

/// `export default name`
/// ^^^^^^^ span
pub export_default: Option<Span>,

/// Duplicated span of `export default` for diagnostics
pub export_default_duplicated: Vec<'a, Span>,
}

impl<'a> ModuleRecord<'a> {
Expand All @@ -83,9 +76,7 @@ impl<'a> ModuleRecord<'a> {
star_export_entries: Vec::new_in(allocator),
exported_bindings: FxHashMap::default(),
exported_bindings_duplicated: Vec::new_in(allocator),
exported_bindings_from_star_export: FxHashMap::default(),
export_default: None,
export_default_duplicated: Vec::new_in(allocator),
}
}
}
Expand Down

0 comments on commit 17663f5

Please sign in to comment.