Skip to content

Commit

Permalink
feat(isolated-declarations): create unique name for _default (#3730)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jun 18, 2024
1 parent 38a75e5 commit ee627c3
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions crates/oxc_isolated_declarations/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use oxc_ast::ast::*;

use oxc_allocator::Box;
use oxc_ast::Visit;
use oxc_span::{GetSpan, SPAN};
use oxc_span::{Atom, GetSpan, SPAN};

use crate::{diagnostics::default_export_inferred, IsolatedDeclarations};

Expand All @@ -24,6 +24,16 @@ impl<'a> IsolatedDeclarations<'a> {
})
}

pub fn create_unique_name(&mut self, name: &str) -> Atom<'a> {
let mut binding = self.ast.new_atom(name);
let mut i = 1;
while self.scope.has_reference(&binding) {
binding = self.ast.new_atom(format!("{name}_{i}").as_str());
i += 1;
}
binding
}

pub fn transform_export_default_declaration(
&mut self,
decl: &ExportDefaultDeclaration<'a>,
Expand All @@ -46,8 +56,7 @@ impl<'a> IsolatedDeclarations<'a> {
} else {
// declare const _default: Type
let kind = VariableDeclarationKind::Const;
// TODO: create unique name for this
let name = self.ast.new_atom("_default");
let name = self.create_unique_name("_default");
let id = self
.ast
.binding_pattern_identifier(BindingIdentifier::new(SPAN, name.clone()));
Expand Down

0 comments on commit ee627c3

Please sign in to comment.