From d6e4605c49714d15ebefa54f3c6fc93c2d88bf76 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Tue, 11 Feb 2025 11:00:57 +0900 Subject: [PATCH 1/6] Add call case --- .changeset/fluffy-cups-pay.md | 5 +++++ libs/css/src/lib.rs | 8 ++++++++ libs/extractor/src/lib.rs | 13 +++++++++++++ ...r__tests__extract_dynamic_style_props-2.snap | 17 +++++++++++++++++ libs/extractor/src/style_extractor.rs | 10 ++++++++++ 5 files changed, 53 insertions(+) create mode 100644 .changeset/fluffy-cups-pay.md create mode 100644 libs/extractor/src/snapshots/extractor__tests__extract_dynamic_style_props-2.snap diff --git a/.changeset/fluffy-cups-pay.md b/.changeset/fluffy-cups-pay.md new file mode 100644 index 00000000..1a7d78de --- /dev/null +++ b/.changeset/fluffy-cups-pay.md @@ -0,0 +1,5 @@ +--- +"@devup-ui/wasm": patch +--- + +Add call case diff --git a/libs/css/src/lib.rs b/libs/css/src/lib.rs index 1ac42ff6..2911c86d 100644 --- a/libs/css/src/lib.rs +++ b/libs/css/src/lib.rs @@ -731,4 +731,12 @@ mod tests { ":root[data-theme=dark] .cls:hover" ); } + + #[test] + fn test_set_class_map() { + let mut map = HashMap::new(); + map.insert("background-0-rgba(255,0,0,0.5)-".to_string(), 1); + set_class_map(map); + assert_eq!(get_class_map().len(), 1); + } } diff --git a/libs/extractor/src/lib.rs b/libs/extractor/src/lib.rs index f2400358..a0dd2c2c 100644 --- a/libs/extractor/src/lib.rs +++ b/libs/extractor/src/lib.rs @@ -492,6 +492,19 @@ mod tests { "test.tsx", r#"import { Box } from "@devup-ui/core"; ; +"#, + ExtractOption { + package: "@devup-ui/core".to_string(), + css_file: None + } + ) + .unwrap()); + + reset_class_map(); + assert_debug_snapshot!(extract( + "test.tsx", + r#"import { Box } from "@devup-ui/core"; +; "#, ExtractOption { package: "@devup-ui/core".to_string(), diff --git a/libs/extractor/src/snapshots/extractor__tests__extract_dynamic_style_props-2.snap b/libs/extractor/src/snapshots/extractor__tests__extract_dynamic_style_props-2.snap new file mode 100644 index 00000000..32727ff9 --- /dev/null +++ b/libs/extractor/src/snapshots/extractor__tests__extract_dynamic_style_props-2.snap @@ -0,0 +1,17 @@ +--- +source: libs/extractor/src/lib.rs +expression: "extract(\"test.tsx\",\nr#\"import { Box } from \"@devup-ui/core\";\n;\n\"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()" +--- +ExtractOutput { + styles: [ + Dynamic( + ExtractDynamicStyle { + property: "padding", + level: 0, + identifier: "Math.abs(5)", + selector: None, + }, + ), + ], + code: "import \"@devup-ui/core/devup-ui.css\";\n
;\n", +} diff --git a/libs/extractor/src/style_extractor.rs b/libs/extractor/src/style_extractor.rs index 92f8320f..88af430a 100644 --- a/libs/extractor/src/style_extractor.rs +++ b/libs/extractor/src/style_extractor.rs @@ -553,6 +553,16 @@ pub fn extract_style_from_expression<'a>( ExtractResult::ExtractStyle(props) } } + Expression::CallExpression(_) => { + ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(Dynamic( + ExtractDynamicStyle::new( + name.unwrap(), + level, + expression_to_code(expression).as_str(), + selector.map(|s| s.into()), + ), + ))]) + } // val if let Some(value) = get_number_by_literal_expression(val) => {} _ => ExtractResult::Maintain, } From bc6fc05397181fe76e805625417163fd3e630842 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Tue, 11 Feb 2025 11:04:33 +0900 Subject: [PATCH 2/6] Fix landing layout --- apps/landing/src/app/(detail)/team/layout.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/landing/src/app/(detail)/team/layout.tsx b/apps/landing/src/app/(detail)/team/layout.tsx index 0373ec20..9b8c5674 100644 --- a/apps/landing/src/app/(detail)/team/layout.tsx +++ b/apps/landing/src/app/(detail)/team/layout.tsx @@ -7,7 +7,7 @@ export default function TeamLayout({ }>) { return ( <> - + {children} From e2360610455283830060f7d207a9ce3613839422 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Tue, 11 Feb 2025 11:14:23 +0900 Subject: [PATCH 3/6] Add serial --- libs/css/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/css/src/lib.rs b/libs/css/src/lib.rs index 2911c86d..4458d6e3 100644 --- a/libs/css/src/lib.rs +++ b/libs/css/src/lib.rs @@ -733,6 +733,7 @@ mod tests { } #[test] + #[serial] fn test_set_class_map() { let mut map = HashMap::new(); map.insert("background-0-rgba(255,0,0,0.5)-".to_string(), 1); From ffd301ab5d0c80fcce8ed6e2ae7320982d773f67 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Tue, 11 Feb 2025 11:40:29 +0900 Subject: [PATCH 4/6] Fix test br --- libs/extractor/src/style_extractor.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/libs/extractor/src/style_extractor.rs b/libs/extractor/src/style_extractor.rs index 88af430a..f46a44c2 100644 --- a/libs/extractor/src/style_extractor.rs +++ b/libs/extractor/src/style_extractor.rs @@ -296,7 +296,9 @@ pub fn extract_style_from_expression<'a>( .unwrap_or(ExtractResult::Maintain) } else { match expression { - Expression::UnaryExpression(_) | Expression::BinaryExpression(_) => { + Expression::UnaryExpression(_) + | Expression::BinaryExpression(_) + | Expression::CallExpression(_) => { ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(Dynamic( ExtractDynamicStyle::new( name.unwrap(), @@ -553,16 +555,6 @@ pub fn extract_style_from_expression<'a>( ExtractResult::ExtractStyle(props) } } - Expression::CallExpression(_) => { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(Dynamic( - ExtractDynamicStyle::new( - name.unwrap(), - level, - expression_to_code(expression).as_str(), - selector.map(|s| s.into()), - ), - ))]) - } // val if let Some(value) = get_number_by_literal_expression(val) => {} _ => ExtractResult::Maintain, } From f29e6f6312283a27b9808861296b9afd6cd4555b Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Tue, 11 Feb 2025 11:56:09 +0900 Subject: [PATCH 5/6] Fix ignore --- .gitignore | 2 ++ libs/extractor/src/visit.rs | 38 ++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 8eef93ae..fe142dbd 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ tsconfig.tsbuildinfo codecov codecov.* codecov.*.* +lcov.info +tarpaulin-report.html \ No newline at end of file diff --git a/libs/extractor/src/visit.rs b/libs/extractor/src/visit.rs index 753bd049..4d14e96c 100644 --- a/libs/extractor/src/visit.rs +++ b/libs/extractor/src/visit.rs @@ -94,39 +94,37 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> { None, )); } else if call.arguments.len() == 1 { - match extract_style_from_expression( + if let ExtractResult::ExtractStyle(styles) = extract_style_from_expression( &self.ast, None, call.arguments[0].to_expression_mut(), 0, None, ) { - ExtractResult::ExtractStyle(styles) - | ExtractResult::ExtractStyleWithChangeTag(styles, _) => { - let class_name = gen_class_names(&self.ast, &styles); - let mut styles = styles - .into_iter() - .flat_map(|ex| ex.extract()) - .collect::>(); + // css can not reachable + // ExtractResult::ExtractStyleWithChangeTag(styles, _) + let class_name = gen_class_names(&self.ast, &styles); + let mut styles = styles + .into_iter() + .flat_map(|ex| ex.extract()) + .collect::>(); - self.styles.append(&mut styles); - if let Some(cls) = class_name { - *it = cls; - } else { - *it = Expression::StringLiteral(self.ast.alloc_string_literal( - SPAN, - "".to_string(), - None, - )); - } - } - _ => { + self.styles.append(&mut styles); + if let Some(cls) = class_name { + *it = cls; + } else { *it = Expression::StringLiteral(self.ast.alloc_string_literal( SPAN, "".to_string(), None, )); } + } else { + *it = Expression::StringLiteral(self.ast.alloc_string_literal( + SPAN, + "".to_string(), + None, + )); } } } From e708e4c0ccbac7e9003981d84acabd68c03a634f Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Tue, 11 Feb 2025 12:32:17 +0900 Subject: [PATCH 6/6] Refactor --- libs/extractor/src/style_extractor.rs | 535 +++++++++++++++----------- libs/extractor/src/visit.rs | 57 ++- 2 files changed, 326 insertions(+), 266 deletions(-) diff --git a/libs/extractor/src/style_extractor.rs b/libs/extractor/src/style_extractor.rs index f46a44c2..21c1474f 100644 --- a/libs/extractor/src/style_extractor.rs +++ b/libs/extractor/src/style_extractor.rs @@ -26,14 +26,10 @@ const IGNORED_IDENTIFIERS: [&str; 3] = ["undefined", "NaN", "Infinity"]; pub enum ExtractResult<'a> { // attribute will be maintained Maintain, - // attribute will be removed - Remove, - // attribute will be removed and the value will be extracted - ExtractStyle(Vec>), - // attribute will be removed and the tag will be changed - ChangeTag(Expression<'a>), - - ExtractStyleWithChangeTag(Vec>, Expression<'a>), + Extract { + styles: Option>>, + tag: Option>, + }, } pub fn extract_style_from_jsx_attr<'a>( @@ -83,12 +79,11 @@ pub fn extract_style_from_expression<'a>( let mut tag = None; for idx in (0..obj.properties.len()).rev() { let mut prop = obj.properties.remove(idx); - let mut rm = false; - match &mut prop { + if !match &mut prop { ObjectPropertyKind::ObjectProperty(prop) => { if let PropertyKey::StaticIdentifier(ident) = &prop.key { let name = ident.name.to_string(); - rm = match extract_style_from_expression( + match extract_style_from_expression( ast_builder, Some(&name), &mut prop.value, @@ -96,25 +91,22 @@ pub fn extract_style_from_expression<'a>( None, ) { ExtractResult::Maintain => false, - ExtractResult::Remove => true, - ExtractResult::ExtractStyle(mut styles) => { - props_styles.append(&mut styles); - true - } - ExtractResult::ChangeTag(t) => { - tag = Some(t); - true - } - ExtractResult::ExtractStyleWithChangeTag(mut styles, t) => { - props_styles.append(&mut styles); - tag = Some(t); + ExtractResult::Extract { styles, tag: _tag } => { + styles.into_iter().for_each(|mut styles| { + props_styles.append(&mut styles) + }); + if let Some(t) = _tag { + tag = Some(t); + } true } } + } else { + false } } ObjectPropertyKind::SpreadProperty(prop) => { - rm = match extract_style_from_expression( + match extract_style_from_expression( ast_builder, None, &mut prop.argument, @@ -122,64 +114,64 @@ pub fn extract_style_from_expression<'a>( None, ) { ExtractResult::Maintain => false, - ExtractResult::Remove => true, - ExtractResult::ExtractStyle(mut styles) => { - props_styles.append(&mut styles); - true - } - ExtractResult::ChangeTag(t) => { - tag = Some(t); - true - } - ExtractResult::ExtractStyleWithChangeTag(mut styles, t) => { - props_styles.append(&mut styles); - tag = Some(t); + ExtractResult::Extract { styles, tag: _tag } => { + styles + .into_iter() + .for_each(|mut styles| props_styles.append(&mut styles)); + if let Some(t) = _tag { + tag = Some(t); + } true } - }; + } } - } - if !rm { + } { obj.properties.insert(idx, prop); } } if props_styles.is_empty() { ExtractResult::Maintain - } else if let Some(tag) = tag { - ExtractResult::ExtractStyleWithChangeTag(props_styles, tag) } else { - ExtractResult::ExtractStyle(props_styles) + ExtractResult::Extract { + styles: Some(props_styles), + tag, + } } } - Expression::ConditionalExpression(ref mut conditional) => { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Conditional { + Expression::ConditionalExpression(ref mut conditional) => ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Conditional { condition: conditional.test.clone_in(ast_builder.allocator), - consequent: if let ExtractResult::ExtractStyle(styles) = - extract_style_from_expression( - ast_builder, - None, - &mut conditional.consequent, - level, - None, - ) { + consequent: if let ExtractResult::Extract { + styles: Some(styles), + .. + } = extract_style_from_expression( + ast_builder, + None, + &mut conditional.consequent, + level, + None, + ) { Some(Box::new(ExtractStyleProp::StaticArray(styles))) } else { None }, - alternate: if let ExtractResult::ExtractStyle(styles) = - extract_style_from_expression( - ast_builder, - None, - &mut conditional.alternate, - level, - selector, - ) { + alternate: if let ExtractResult::Extract { + styles: Some(styles), + .. + } = extract_style_from_expression( + ast_builder, + None, + &mut conditional.alternate, + level, + selector, + ) { Some(Box::new(ExtractStyleProp::StaticArray(styles))) } else { None }, - }]) - } + }]), + tag: None, + }, Expression::ParenthesizedExpression(parenthesized) => extract_style_from_expression( ast_builder, None, @@ -197,7 +189,10 @@ pub fn extract_style_from_expression<'a>( } if name == "as" { - return ExtractResult::ChangeTag(expression.clone_in(ast_builder.allocator)); + return ExtractResult::Extract { + styles: None, + tag: Some(expression.clone_in(ast_builder.allocator)), + }; // return match expression { // Expression::StringLiteral(ident) => ExtractResult::ChangeTag( @@ -245,20 +240,24 @@ pub fn extract_style_from_expression<'a>( for p in obj.properties.iter_mut() { if let ObjectPropertyKind::ObjectProperty(ref mut o) = p { let name = o.key.name().unwrap().to_string(); - if let ExtractResult::ExtractStyle(mut styles) = - extract_style_from_expression( - ast_builder, - None, - &mut o.value, - level, - Some(name.as_str()), - ) - { + if let ExtractResult::Extract { + styles: Some(mut styles), + .. + } = extract_style_from_expression( + ast_builder, + None, + &mut o.value, + level, + Some(name.as_str()), + ) { props.append(&mut styles); } } } - return ExtractResult::ExtractStyle(props); + return ExtractResult::Extract { + styles: Some(props), + tag: None, + }; } } @@ -281,8 +280,9 @@ pub fn extract_style_from_expression<'a>( typo = name == "typography"; } if let Some(value) = get_string_by_literal_expression(expression) { - name.map(|name| { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(if typo { + name.map(|name| ExtractResult::Extract { + tag: None, + styles: Some(vec![ExtractStyleProp::Static(if typo { Typography(value.as_str().to_string()) } else { Static(ExtractStaticStyle::new( @@ -291,75 +291,86 @@ pub fn extract_style_from_expression<'a>( level, selector.map(|s| s.into()), )) - })]) + })]), }) .unwrap_or(ExtractResult::Maintain) } else { match expression { Expression::UnaryExpression(_) | Expression::BinaryExpression(_) - | Expression::CallExpression(_) => { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(Dynamic( + | Expression::CallExpression(_) => ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Static(Dynamic( ExtractDynamicStyle::new( name.unwrap(), level, expression_to_code(expression).as_str(), selector.map(|s| s.into()), ), - ))]) - } + ))]), + tag: None, + }, Expression::ComputedMemberExpression(mem) => { extract_style_from_member_expression(ast_builder, name, mem, level, selector) } Expression::TemplateLiteral(tmp) => { if let Some(name) = name { if tmp.quasis.len() == 1 { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(if typo { - Typography(tmp.quasis[0].value.raw.as_str().to_string()) - } else { - Static(ExtractStaticStyle::new( - name, - tmp.quasis[0].value.raw.as_str(), - level, - selector.map(|s| s.into()), - )) - })]) + ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Static(if typo { + Typography(tmp.quasis[0].value.raw.as_str().to_string()) + } else { + Static(ExtractStaticStyle::new( + name, + tmp.quasis[0].value.raw.as_str(), + level, + selector.map(|s| s.into()), + )) + })]), + tag: None, + } } else if typo { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Expression { - expression: ast_builder.expression_template_literal( - SPAN, - ast_builder.vec_from_array([ - ast_builder.template_element( - SPAN, - false, - TemplateElementValue { - raw: ast_builder.atom("typo-"), - cooked: None, - }, - ), - ast_builder.template_element( - SPAN, - true, - TemplateElementValue { - raw: ast_builder.atom(""), - cooked: None, - }, - ), - ]), - ast_builder - .vec_from_array([expression.clone_in(ast_builder.allocator)]), - ), - styles: vec![], - }]) + ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Expression { + expression: ast_builder.expression_template_literal( + SPAN, + ast_builder.vec_from_array([ + ast_builder.template_element( + SPAN, + false, + TemplateElementValue { + raw: ast_builder.atom("typo-"), + cooked: None, + }, + ), + ast_builder.template_element( + SPAN, + true, + TemplateElementValue { + raw: ast_builder.atom(""), + cooked: None, + }, + ), + ]), + ast_builder.vec_from_array([ + expression.clone_in(ast_builder.allocator) + ]), + ), + styles: vec![], + }]), + tag: None, + } } else { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(Dynamic( - ExtractDynamicStyle::new( - name, - level, - expression_to_code(expression).as_str(), - selector.map(|s| s.into()), - ), - ))]) + ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Static(Dynamic( + ExtractDynamicStyle::new( + name, + level, + expression_to_code(expression).as_str(), + selector.map(|s| s.into()), + ), + ))]), + tag: None, + } } } else { ExtractResult::Maintain @@ -370,41 +381,48 @@ pub fn extract_style_from_expression<'a>( ExtractResult::Maintain } else if let Some(name) = name { if typo { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Expression { - expression: ast_builder.expression_template_literal( - SPAN, - ast_builder.vec_from_array([ - ast_builder.template_element( - SPAN, - false, - TemplateElementValue { - raw: ast_builder.atom("typo-"), - cooked: None, - }, - ), - ast_builder.template_element( - SPAN, - true, - TemplateElementValue { - raw: ast_builder.atom(""), - cooked: None, - }, - ), - ]), - ast_builder - .vec_from_array([expression.clone_in(ast_builder.allocator)]), - ), - styles: vec![], - }]) + ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Expression { + expression: ast_builder.expression_template_literal( + SPAN, + ast_builder.vec_from_array([ + ast_builder.template_element( + SPAN, + false, + TemplateElementValue { + raw: ast_builder.atom("typo-"), + cooked: None, + }, + ), + ast_builder.template_element( + SPAN, + true, + TemplateElementValue { + raw: ast_builder.atom(""), + cooked: None, + }, + ), + ]), + ast_builder.vec_from_array([ + expression.clone_in(ast_builder.allocator) + ]), + ), + styles: vec![], + }]), + tag: None, + } } else { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(Dynamic( - ExtractDynamicStyle::new( - name, - level, - identifier.name.as_str(), - selector.map(|s| s.into()), - ), - ))]) + ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Static(Dynamic( + ExtractDynamicStyle::new( + name, + level, + identifier.name.as_str(), + selector.map(|s| s.into()), + ), + ))]), + tag: None, + } } } else { ExtractResult::Maintain @@ -419,29 +437,32 @@ pub fn extract_style_from_expression<'a>( level, selector, ) { - ExtractResult::ExtractStyle(styles) => { - Some(Box::new(ExtractStyleProp::StaticArray(styles))) - } + ExtractResult::Extract { + styles: Some(styles), + .. + } => Some(Box::new(ExtractStyleProp::StaticArray(styles))), _ => None, } }); match logical.operator { - LogicalOperator::Or => { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Conditional { + LogicalOperator::Or => ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Conditional { condition: logical.left.clone_in(ast_builder.allocator), consequent: None, alternate: res, - }]) - } - LogicalOperator::And => { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Conditional { + }]), + tag: None, + }, + LogicalOperator::And => ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Conditional { condition: logical.left.clone_in(ast_builder.allocator), consequent: res, alternate: None, - }]) - } - LogicalOperator::Coalesce => { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Conditional { + }]), + tag: None, + }, + LogicalOperator::Coalesce => ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Conditional { condition: Expression::LogicalExpression( ast_builder.alloc_logical_expression( SPAN, @@ -471,8 +492,9 @@ pub fn extract_style_from_expression<'a>( ), consequent: None, alternate: res, - }]) - } + }]), + tag: None, + }, } } Expression::ParenthesizedExpression(parenthesized) => extract_style_from_expression( @@ -486,7 +508,10 @@ pub fn extract_style_from_expression<'a>( let mut props = vec![]; for (idx, element) in array.elements.iter_mut().enumerate() { - if let ExtractResult::ExtractStyle(mut styles) = extract_style_from_expression( + if let ExtractResult::Extract { + styles: Some(mut styles), + .. + } = extract_style_from_expression( ast_builder, name, element.to_expression_mut(), @@ -500,59 +525,67 @@ pub fn extract_style_from_expression<'a>( if props.is_empty() { ExtractResult::Maintain } else { - ExtractResult::ExtractStyle(props) + ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::StaticArray(props)]), + tag: None, + } } } - Expression::ConditionalExpression(ref mut conditional) => { - ExtractResult::ExtractStyle(vec![ExtractStyleProp::Conditional { + Expression::ConditionalExpression(ref mut conditional) => ExtractResult::Extract { + styles: Some(vec![ExtractStyleProp::Conditional { condition: conditional.test.clone_in(ast_builder.allocator), - consequent: if let ExtractResult::ExtractStyle(styles) = - extract_style_from_expression( - ast_builder, - name, - &mut conditional.consequent, - level, - selector, - ) { + consequent: if let ExtractResult::Extract { + styles: Some(styles), + .. + } = extract_style_from_expression( + ast_builder, + name, + &mut conditional.consequent, + level, + selector, + ) { Some(Box::new(ExtractStyleProp::StaticArray(styles))) } else { None }, - alternate: if let ExtractResult::ExtractStyle(styles) = - extract_style_from_expression( - ast_builder, - name, - &mut conditional.alternate, - level, - selector, - ) { + alternate: if let ExtractResult::Extract { + styles: Some(styles), + .. + } = extract_style_from_expression( + ast_builder, + name, + &mut conditional.alternate, + level, + selector, + ) { Some(Box::new(ExtractStyleProp::StaticArray(styles))) } else { None }, - }]) - } + }]), + tag: None, + }, Expression::ObjectExpression(obj) => { let mut props = vec![]; for p in obj.properties.iter_mut() { if let ObjectPropertyKind::ObjectProperty(ref mut o) = p { - if let ExtractResult::ExtractStyle(ref mut ret) = - extract_style_from_expression( - ast_builder, - Some(&o.key.name().unwrap()), - &mut o.value, - level, - selector, - ) - { - props.append(ret); + if let ExtractResult::Extract { + styles: Some(mut styles), + .. + } = extract_style_from_expression( + ast_builder, + Some(&o.key.name().unwrap()), + &mut o.value, + level, + selector, + ) { + props.append(&mut styles); } }; } - if props.is_empty() { - ExtractResult::Remove - } else { - ExtractResult::ExtractStyle(props) + ExtractResult::Extract { + styles: Some(props), + tag: None, } } // val if let Some(value) = get_number_by_literal_expression(val) => {} @@ -574,12 +607,18 @@ fn extract_style_from_member_expression<'a>( match &mut mem.object { Expression::ArrayExpression(array) => { if array.elements.is_empty() { - return ExtractResult::Remove; + return ExtractResult::Extract { + styles: None, + tag: None, + }; } if let Some(num) = get_number_by_literal_expression(mem_expression) { if num < 0f64 { - return ExtractResult::Remove; + return ExtractResult::Extract { + styles: None, + tag: None, + }; } let mut etc = None; for (idx, p) in array.elements.iter_mut().enumerate() { @@ -588,21 +627,26 @@ fn extract_style_from_member_expression<'a>( continue; } if idx as f64 == num { - if let ExtractResult::ExtractStyle(styles) = extract_style_from_expression( + if let ExtractResult::Extract { + styles: Some(styles), + .. + } = extract_style_from_expression( ast_builder, name, p.to_expression_mut(), level, selector, ) { - return ExtractResult::ExtractStyle(styles); + return ExtractResult::Extract { + styles: Some(styles), + tag: None, + }; } } } - return match etc { - None => ExtractResult::Remove, - Some(etc) => ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static( - Dynamic(ExtractDynamicStyle::new( + return ExtractResult::Extract { + styles: etc.map(|etc| { + vec![ExtractStyleProp::Static(Dynamic(ExtractDynamicStyle::new( name.unwrap(), level, expression_to_code(&Expression::ComputedMemberExpression( @@ -615,8 +659,9 @@ fn extract_style_from_member_expression<'a>( )) .as_str(), selector.map(|s| s.into()), - )), - )]), + )))] + }), + tag: None, }; } @@ -640,7 +685,10 @@ fn extract_style_from_member_expression<'a>( selector.map(|s| s.into()), )))), ); - } else if let ExtractResult::ExtractStyle(styles) = extract_style_from_expression( + } else if let ExtractResult::Extract { + styles: Some(styles), + .. + } = extract_style_from_expression( ast_builder, name, p.to_expression_mut(), @@ -661,7 +709,10 @@ fn extract_style_from_member_expression<'a>( } Expression::ObjectExpression(obj) => { if obj.properties.is_empty() { - return ExtractResult::Remove; + return ExtractResult::Extract { + styles: None, + tag: None, + }; } let mut map = BTreeMap::new(); @@ -671,16 +722,20 @@ fn extract_style_from_member_expression<'a>( if let ObjectPropertyKind::ObjectProperty(ref mut o) = p { if let PropertyKey::StaticIdentifier(ref pk) = o.key { if pk.name == k { - if let ExtractResult::ExtractStyle(styles) = - extract_style_from_expression( - ast_builder, - name, - &mut o.value, - level, - selector, - ) - { - return ExtractResult::ExtractStyle(styles); + if let ExtractResult::Extract { + styles: Some(styles), + .. + } = extract_style_from_expression( + ast_builder, + name, + &mut o.value, + level, + selector, + ) { + return ExtractResult::Extract { + styles: Some(styles), + tag: None, + }; } } } @@ -688,8 +743,14 @@ fn extract_style_from_member_expression<'a>( etc = Some(sp.argument.clone_in(ast_builder.allocator)); } } + match etc { - None => return ExtractResult::Remove, + None => { + return ExtractResult::Extract { + styles: None, + tag: None, + } + } Some(etc) => { ret.push(ExtractStyleProp::Static(Dynamic(ExtractDynamicStyle::new( name.unwrap(), @@ -715,7 +776,10 @@ fn extract_style_from_member_expression<'a>( | PropertyKey::NumericLiteral(_) | PropertyKey::StringLiteral(_) = o.key { - if let ExtractResult::ExtractStyle(styles) = extract_style_from_expression( + if let ExtractResult::Extract { + styles: Some(styles), + .. + } = extract_style_from_expression( ast_builder, name, &mut o.value, @@ -756,7 +820,10 @@ fn extract_style_from_member_expression<'a>( _ => {} }; - ExtractResult::ExtractStyle(ret) + ExtractResult::Extract { + styles: Some(ret), + tag: None, + } } fn get_number_by_literal_expression(expr: &Expression) -> Option { diff --git a/libs/extractor/src/visit.rs b/libs/extractor/src/visit.rs index 4d14e96c..83d5b899 100644 --- a/libs/extractor/src/visit.rs +++ b/libs/extractor/src/visit.rs @@ -94,7 +94,10 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> { None, )); } else if call.arguments.len() == 1 { - if let ExtractResult::ExtractStyle(styles) = extract_style_from_expression( + if let ExtractResult::Extract { + styles: Some(styles), + .. + } = extract_style_from_expression( &self.ast, None, call.arguments[0].to_expression_mut(), @@ -197,23 +200,19 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> { None, )); let mut props_styles = vec![]; - match extract_style_from_expression( - &self.ast, - None, - it.arguments[1].to_expression_mut(), - 0, - None, - ) { - ExtractResult::ExtractStyle(mut styles) => { - props_styles.append(&mut styles); - } - ExtractResult::ExtractStyleWithChangeTag(mut styles, t) => { - tag = t; - props_styles.append(&mut styles); - } - ExtractResult::Maintain => {} - ExtractResult::Remove => {} - ExtractResult::ChangeTag(t) => { + if let ExtractResult::Extract { styles, tag: _tag } = + extract_style_from_expression( + &self.ast, + None, + it.arguments[1].to_expression_mut(), + 0, + None, + ) + { + styles.into_iter().for_each(|mut ex| { + props_styles.append(&mut ex); + }); + if let Some(t) = _tag { tag = t; } } @@ -268,20 +267,14 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> { if let Some(at) = &mut attr.value { rm = match extract_style_from_jsx_attr(&self.ast, &name, at, None) { ExtractResult::Maintain => false, - ExtractResult::Remove => true, - ExtractResult::ExtractStyle(mut styles) => { - styles.reverse(); - props_styles.append(&mut styles); - true - } - ExtractResult::ChangeTag(tag) => { - tag_name = tag; - true - } - ExtractResult::ExtractStyleWithChangeTag(mut styles, tag) => { - styles.reverse(); - props_styles.append(&mut styles); - tag_name = tag; + ExtractResult::Extract { styles, tag } => { + styles.into_iter().for_each(|mut ex| { + ex.reverse(); + props_styles.append(&mut ex); + }); + if let Some(t) = tag { + tag_name = t; + } true } }