From ee627c355c919d1b0ce424d5c46c6882159f3f6e Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Tue, 18 Jun 2024 03:40:45 +0000 Subject: [PATCH] feat(isolated-declarations): create unique name for `_default` (#3730) --- crates/oxc_isolated_declarations/src/module.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/oxc_isolated_declarations/src/module.rs b/crates/oxc_isolated_declarations/src/module.rs index e45db55c1087e..6898eabc748e1 100644 --- a/crates/oxc_isolated_declarations/src/module.rs +++ b/crates/oxc_isolated_declarations/src/module.rs @@ -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}; @@ -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>, @@ -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()));