@@ -85,39 +85,51 @@ fn generate_transformed_import(
8585 match alias {
8686 ImportAlias :: DefaultToNamed ( named_export) => {
8787 // Transform: `import foo from 'pkg'` → `import { named as foo } from 'target'`
88+ // Check for namespace import first (early return)
89+ if let Some ( ns_spec) = specifiers. iter ( ) . find_map ( |s| {
90+ if let ImportDeclarationSpecifier :: ImportNamespaceSpecifier ( ns) = s {
91+ Some ( ns)
92+ } else {
93+ None
94+ }
95+ } ) {
96+ return format ! (
97+ "import * as {} from '{}';" ,
98+ ns_spec. local. name. as_str( ) ,
99+ package
100+ ) ;
101+ }
102+
88103 let mut parts = String :: new ( ) ;
89104
105+ // Handle default specifier (always first and at most one in valid JS)
106+ if let Some ( default_spec) = specifiers. iter ( ) . find_map ( |s| {
107+ if let ImportDeclarationSpecifier :: ImportDefaultSpecifier ( ds) = s {
108+ Some ( ds)
109+ } else {
110+ None
111+ }
112+ } ) {
113+ let local_name = default_spec. local . name . as_str ( ) ;
114+ if local_name == named_export {
115+ parts. push_str ( named_export) ;
116+ } else {
117+ write ! ( parts, "{} as {}" , named_export, local_name) . unwrap ( ) ;
118+ }
119+ }
120+
121+ // Handle named specifiers
90122 for specifier in specifiers {
91- match specifier {
92- ImportDeclarationSpecifier :: ImportDefaultSpecifier ( default_spec) => {
93- let local_name = default_spec. local . name . as_str ( ) ;
94- if !parts. is_empty ( ) {
95- parts. push_str ( ", " ) ;
96- }
97- if local_name == named_export {
98- parts. push_str ( named_export) ;
99- } else {
100- write ! ( parts, "{} as {}" , named_export, local_name) . unwrap ( ) ;
101- }
102- }
103- ImportDeclarationSpecifier :: ImportSpecifier ( spec) => {
104- let imported = spec. imported . to_string ( ) ;
105- let local = spec. local . name . as_str ( ) ;
106- if !parts. is_empty ( ) {
107- parts. push_str ( ", " ) ;
108- }
109- if imported == local {
110- parts. push_str ( & imported) ;
111- } else {
112- write ! ( parts, "{} as {}" , imported, local) . unwrap ( ) ;
113- }
123+ if let ImportDeclarationSpecifier :: ImportSpecifier ( spec) = specifier {
124+ if !parts. is_empty ( ) {
125+ parts. push_str ( ", " ) ;
114126 }
115- ImportDeclarationSpecifier :: ImportNamespaceSpecifier ( ns_spec ) => {
116- return format ! (
117- "import * as {} from '{}';" ,
118- ns_spec . local . name . as_str ( ) ,
119- package
120- ) ;
127+ let imported = spec . imported . to_string ( ) ;
128+ let local = spec . local . name . as_str ( ) ;
129+ if imported == local {
130+ parts . push_str ( & imported ) ;
131+ } else {
132+ write ! ( parts , "{} as {}" , imported , local ) . unwrap ( ) ;
121133 }
122134 }
123135 }
@@ -127,34 +139,46 @@ fn generate_transformed_import(
127139 ImportAlias :: NamedToNamed => {
128140 // Just change the source, keep specifiers as-is
129141 // `import { style } from 'pkg'` → `import { style } from 'target'`
142+ // Check for namespace import first (early return)
143+ if let Some ( ns_spec) = specifiers. iter ( ) . find_map ( |s| {
144+ if let ImportDeclarationSpecifier :: ImportNamespaceSpecifier ( ns) = s {
145+ Some ( ns)
146+ } else {
147+ None
148+ }
149+ } ) {
150+ return format ! (
151+ "import * as {} from '{}';" ,
152+ ns_spec. local. name. as_str( ) ,
153+ package
154+ ) ;
155+ }
156+
130157 let mut parts = String :: new ( ) ;
131158
159+ // Handle default specifier first (becomes `default as localName`)
160+ if let Some ( default_spec) = specifiers. iter ( ) . find_map ( |s| {
161+ if let ImportDeclarationSpecifier :: ImportDefaultSpecifier ( ds) = s {
162+ Some ( ds)
163+ } else {
164+ None
165+ }
166+ } ) {
167+ write ! ( parts, "default as {}" , default_spec. local. name. as_str( ) ) . unwrap ( ) ;
168+ }
169+
170+ // Handle named specifiers
132171 for specifier in specifiers {
133- match specifier {
134- ImportDeclarationSpecifier :: ImportDefaultSpecifier ( default_spec) => {
135- if !parts. is_empty ( ) {
136- parts. push_str ( ", " ) ;
137- }
138- write ! ( parts, "default as {}" , default_spec. local. name. as_str( ) ) . unwrap ( ) ;
139- }
140- ImportDeclarationSpecifier :: ImportSpecifier ( spec) => {
141- let imported = spec. imported . to_string ( ) ;
142- let local = spec. local . name . as_str ( ) ;
143- if !parts. is_empty ( ) {
144- parts. push_str ( ", " ) ;
145- }
146- if imported == local {
147- parts. push_str ( & imported) ;
148- } else {
149- write ! ( parts, "{} as {}" , imported, local) . unwrap ( ) ;
150- }
172+ if let ImportDeclarationSpecifier :: ImportSpecifier ( spec) = specifier {
173+ if !parts. is_empty ( ) {
174+ parts. push_str ( ", " ) ;
151175 }
152- ImportDeclarationSpecifier :: ImportNamespaceSpecifier ( ns_spec ) => {
153- return format ! (
154- "import * as {} from '{}';" ,
155- ns_spec . local . name . as_str ( ) ,
156- package
157- ) ;
176+ let imported = spec . imported . to_string ( ) ;
177+ let local = spec . local . name . as_str ( ) ;
178+ if imported == local {
179+ parts . push_str ( & imported ) ;
180+ } else {
181+ write ! ( parts , "{} as {}" , imported , local ) . unwrap ( ) ;
158182 }
159183 }
160184 }
0 commit comments