@@ -19,41 +19,28 @@ use oxc_span::SPAN;
1919
2020fn extract_base_tag_and_class_name < ' a > (
2121 input : & Expression < ' a > ,
22- styled_name : & str ,
2322 imports : & HashMap < String , ExportVariableKind > ,
2423) -> ( Option < String > , Option < Vec < ExtractStyleValue > > ) {
2524 match input {
2625 Expression :: StaticMemberExpression ( member) => {
27- if let Expression :: Identifier ( ident) = & member. object {
28- if ident. name . as_str ( ) == styled_name {
29- ( Some ( member. property . name . to_string ( ) ) , None )
30- } else {
31- ( None , None )
32- }
33- } else {
34- ( None , None )
35- }
26+ ( Some ( member. property . name . to_string ( ) ) , None )
3627 }
3728 Expression :: CallExpression ( call) => {
38- if let Expression :: Identifier ( ident) = & call. callee {
39- if ident. name . as_str ( ) == styled_name && call. arguments . len ( ) == 1 {
40- // styled("div") or styled(Component)
41- if let Argument :: StringLiteral ( lit) = & call. arguments [ 0 ] {
42- ( Some ( lit. value . to_string ( ) ) , None )
43- } else if let Argument :: Identifier ( ident) = & call. arguments [ 0 ] {
44- if let Some ( export_variable_kind) = imports. get ( ident. name . as_str ( ) ) {
45- (
46- Some ( export_variable_kind. to_tag ( ) . to_string ( ) ) ,
47- Some ( export_variable_kind. extract ( ) ) ,
48- )
49- } else {
50- ( Some ( ident. name . to_string ( ) ) , None )
51- }
29+ if call. arguments . len ( ) == 1 {
30+ // styled("div") or styled(Component)
31+ if let Argument :: StringLiteral ( lit) = & call. arguments [ 0 ] {
32+ ( Some ( lit. value . to_string ( ) ) , None )
33+ } else if let Argument :: Identifier ( ident) = & call. arguments [ 0 ] {
34+ if let Some ( export_variable_kind) = imports. get ( ident. name . as_str ( ) ) {
35+ (
36+ Some ( export_variable_kind. to_tag ( ) . to_string ( ) ) ,
37+ Some ( export_variable_kind. extract ( ) ) ,
38+ )
5239 } else {
53- // Component reference - we'll handle this later
54- ( None , None )
40+ ( Some ( ident. name . to_string ( ) ) , None )
5541 }
5642 } else {
43+ // Component reference - we'll handle this later
5744 ( None , None )
5845 }
5946 } else {
@@ -74,16 +61,14 @@ fn extract_base_tag_and_class_name<'a>(
7461pub fn extract_style_from_styled < ' a > (
7562 ast_builder : & AstBuilder < ' a > ,
7663 expression : & mut Expression < ' a > ,
77- styled_name : & str ,
7864 split_filename : Option < & str > ,
7965 imports : & HashMap < String , ExportVariableKind > ,
8066) -> ( ExtractResult < ' a > , Expression < ' a > ) {
8167 let ( result, new_expr) = match expression {
8268 // Case 1: styled.div`css` or styled("div")`css`
8369 Expression :: TaggedTemplateExpression ( tag) => {
8470 // Check if tag is styled.div or styled(...)
85- let ( tag_name, default_class_name) =
86- extract_base_tag_and_class_name ( & tag. tag , styled_name, imports) ;
71+ let ( tag_name, default_class_name) = extract_base_tag_and_class_name ( & tag. tag , imports) ;
8772
8873 if let Some ( tag_name) = tag_name {
8974 // Extract CSS from template literal
@@ -101,13 +86,13 @@ pub fn extract_style_from_styled<'a>(
10186
10287 let class_name =
10388 gen_class_names ( ast_builder, & mut props_styles, None , split_filename) ;
104-
105- let new_expr = create_styled_component (
89+ let styled_component = create_styled_component (
10690 ast_builder,
10791 & tag_name,
10892 & class_name,
10993 & gen_styles ( ast_builder, & props_styles, None ) ,
11094 ) ;
95+
11196 let result = ExtractResult {
11297 styles : props_styles,
11398 tag : Some ( ast_builder. expression_string_literal (
@@ -120,7 +105,7 @@ pub fn extract_style_from_styled<'a>(
120105 props : None ,
121106 } ;
122107
123- ( Some ( result) , Some ( new_expr ) )
108+ ( Some ( result) , Some ( styled_component ) )
124109 } else {
125110 ( None , None )
126111 }
@@ -129,7 +114,7 @@ pub fn extract_style_from_styled<'a>(
129114 Expression :: CallExpression ( call) => {
130115 // Check if this is a call to styled.div or styled("div")
131116 let ( tag_name, default_class_name) =
132- extract_base_tag_and_class_name ( & call. callee , styled_name , imports) ;
117+ extract_base_tag_and_class_name ( & call. callee , imports) ;
133118
134119 if let Some ( tag_name) = tag_name
135120 && call. arguments . len ( ) == 1
@@ -196,14 +181,15 @@ fn create_styled_component<'a>(
196181 SPAN ,
197182 FormalParameterKind :: ArrowFormalParameters ,
198183 oxc_allocator:: Vec :: from_iter_in (
199- vec ! [ ast_builder. formal_parameter(
200- SPAN ,
201- oxc_allocator:: Vec :: from_iter_in( vec![ ] , ast_builder. allocator) ,
202- ast_builder. binding_pattern(
203- ast_builder. binding_pattern_kind_object_pattern(
204- SPAN ,
205- oxc_allocator:: Vec :: from_iter_in(
206- vec![
184+ vec ! [
185+ ast_builder. formal_parameter(
186+ SPAN ,
187+ oxc_allocator:: Vec :: from_iter_in( vec![ ] , ast_builder. allocator) ,
188+ ast_builder. binding_pattern(
189+ ast_builder. binding_pattern_kind_object_pattern(
190+ SPAN ,
191+ oxc_allocator:: Vec :: from_iter_in(
192+ vec![
207193 ast_builder. binding_property(
208194 SPAN ,
209195 ast_builder. property_key_static_identifier( SPAN , "style" ) ,
@@ -241,27 +227,28 @@ fn create_styled_component<'a>(
241227 false ,
242228 ) ,
243229 ] ,
244- ast_builder. allocator,
245- ) ,
246- Some ( ast_builder. binding_rest_element(
247- SPAN ,
248- ast_builder. binding_pattern(
249- ast_builder. binding_pattern_kind_binding_identifier(
250- SPAN ,
251- ast_builder. atom( "rest" ) ,
252- ) ,
253- None :: <oxc_allocator:: Box <oxc_ast:: ast:: TSTypeAnnotation <' a>>>,
254- false ,
230+ ast_builder. allocator,
255231 ) ,
256- ) ) ,
232+ Some ( ast_builder. binding_rest_element(
233+ SPAN ,
234+ ast_builder. binding_pattern(
235+ ast_builder. binding_pattern_kind_binding_identifier(
236+ SPAN ,
237+ ast_builder. atom( "rest" ) ,
238+ ) ,
239+ None :: <oxc_allocator:: Box <oxc_ast:: ast:: TSTypeAnnotation <' a>>>,
240+ false ,
241+ ) ,
242+ ) ) ,
243+ ) ,
244+ None :: <oxc_allocator:: Box <oxc_ast:: ast:: TSTypeAnnotation <' a>>>,
245+ false ,
257246 ) ,
258- None :: <oxc_allocator:: Box <oxc_ast:: ast:: TSTypeAnnotation <' a>>>,
247+ None ,
248+ false ,
259249 false ,
260250 ) ,
261- None ,
262- false ,
263- false ,
264- ) ] ,
251+ ] ,
265252 ast_builder. allocator ,
266253 ) ,
267254 None :: < oxc_allocator:: Box < oxc_ast:: ast:: BindingRestElement < ' a > > > ,
@@ -270,16 +257,20 @@ fn create_styled_component<'a>(
270257 SPAN ,
271258 oxc_allocator:: Vec :: from_iter_in ( vec ! [ ] , ast_builder. allocator ) ,
272259 oxc_allocator:: Vec :: from_iter_in (
273- vec ! [ ast_builder. statement_expression(
274- SPAN ,
275- ast_builder. expression_jsx_element(
260+ vec ! [
261+ ast_builder. statement_expression(
276262 SPAN ,
277- ast_builder. alloc_jsx_opening_element (
263+ ast_builder. expression_jsx_element (
278264 SPAN ,
279- ast_builder. jsx_element_name_identifier( SPAN , ast_builder. atom( tag_name) ) ,
280- None :: <oxc_allocator:: Box <oxc_ast:: ast:: TSTypeParameterInstantiation <' a>>>,
281- oxc_allocator:: Vec :: from_iter_in(
282- vec![
265+ ast_builder. alloc_jsx_opening_element(
266+ SPAN ,
267+ ast_builder
268+ . jsx_element_name_identifier( SPAN , ast_builder. atom( tag_name) ) ,
269+ None :: <
270+ oxc_allocator:: Box <oxc_ast:: ast:: TSTypeParameterInstantiation <' a>>,
271+ >,
272+ oxc_allocator:: Vec :: from_iter_in(
273+ vec![
283274 ast_builder. jsx_attribute_item_spread_attribute(
284275 SPAN ,
285276 ast_builder
@@ -358,13 +349,14 @@ fn create_styled_component<'a>(
358349 ) ,
359350 ) ,
360351 ] ,
361- ast_builder. allocator,
352+ ast_builder. allocator,
353+ ) ,
362354 ) ,
355+ oxc_allocator:: Vec :: from_iter_in( vec![ ] , ast_builder. allocator) ,
356+ None :: <oxc_allocator:: Box <oxc_ast:: ast:: JSXClosingElement <' a>>>,
363357 ) ,
364- oxc_allocator:: Vec :: from_iter_in( vec![ ] , ast_builder. allocator) ,
365- None :: <oxc_allocator:: Box <oxc_ast:: ast:: JSXClosingElement <' a>>>,
366358 ) ,
367- ) ] ,
359+ ] ,
368360 ast_builder. allocator ,
369361 ) ,
370362 ) ;
0 commit comments