Skip to content

Commit 679ec14

Browse files
committed
Refactor code
1 parent 919f997 commit 679ec14

File tree

4 files changed

+87
-25
lines changed

4 files changed

+87
-25
lines changed

libs/extractor/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,6 +3672,9 @@ e(o, { className: "a", bg: variable, style: { color: "blue" }, ...props })
36723672

36733673
reset_class_map();
36743674
assert_debug_snapshot!(ToBTreeSet::from(extract("test.js", r#""use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),r=require("@devup-ui/react");function t(){return e.jsxs("div",{children:[e.jsx(r.Box,{_hover:{bg:"blue"},bg:"$text",color:"red",children:"hello"}),e.jsx(r.Text,{typography:`header`,children:"typo"}),e.jsx(r.Flex,{as:"section",mt:2,children:"section"})]})}exports.Lib=t;"#, ExtractOption { package: "@devup-ui/react".to_string(), css_dir: "@devup-ui/react".to_string(), single_css: true, import_main_css: false }).unwrap()));
3675+
3676+
reset_class_map();
3677+
assert_debug_snapshot!(ToBTreeSet::from(extract("test.js", r#""use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),{Box,Text,Flex}=require("@devup-ui/react");function t(){return e.jsxs("div",{children:[e.jsx(Box,{_hover:{bg:"blue"},bg:"$text",color:"red",children:"hello"}),e.jsx(Text,{typography:`header`,children:"typo"}),e.jsx(Flex,{as:"section",mt:2,children:"section"})]})}exports.Lib=t;"#, ExtractOption { package: "@devup-ui/react".to_string(), css_dir: "@devup-ui/react".to_string(), single_css: true, import_main_css: false }).unwrap()));
36753678
}
36763679

36773680
#[test]

libs/extractor/src/prop_modify_utils.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ pub fn modify_prop_object<'a>(
3131
let name = ident.name.as_str();
3232
if name == "className" {
3333
class_name_prop = Some(attr.value.clone_in(ast_builder.allocator));
34-
continue;
35-
}
36-
if name == "style" {
34+
} else if name == "style" {
3735
style_prop = Some(attr.value.clone_in(ast_builder.allocator));
38-
continue;
36+
} else {
37+
props.insert(idx, ObjectPropertyKind::ObjectProperty(attr));
3938
}
39+
} else {
40+
props.insert(idx, ObjectPropertyKind::ObjectProperty(attr));
4041
}
41-
props.insert(idx, ObjectPropertyKind::ObjectProperty(attr));
4242
}
4343
ObjectPropertyKind::SpreadProperty(spread) => {
4444
spread_props.push(spread.argument.clone_in(ast_builder.allocator));
@@ -111,26 +111,26 @@ pub fn modify_props<'a>(
111111
&& let Some(value) = &attr.value
112112
&& (ident.name == "className" || ident.name == "style")
113113
{
114-
let value = match &value {
115-
JSXAttributeValue::ExpressionContainer(container) => container
114+
let mut res = None;
115+
116+
if let JSXAttributeValue::ExpressionContainer(container) = &value {
117+
res = container
116118
.expression
117119
.as_expression()
118-
.map(|expression| expression.clone_in(ast_builder.allocator)),
119-
JSXAttributeValue::StringLiteral(literal) => {
120-
Some(ast_builder.expression_string_literal(SPAN, literal.value, None))
121-
}
122-
_ => None,
123-
};
120+
.map(|expression| expression.clone_in(ast_builder.allocator));
121+
} else if let JSXAttributeValue::StringLiteral(literal) = &value {
122+
res =
123+
Some(ast_builder.expression_string_literal(SPAN, literal.value, None));
124+
}
124125
let name = ident.name.as_str();
125126
if name == "className" {
126-
class_name_prop = value;
127+
class_name_prop = res;
127128
} else if name == "style" {
128-
style_prop = value;
129+
style_prop = res;
129130
}
130-
131-
continue;
131+
} else {
132+
props.insert(idx, JSXAttributeItem::Attribute(attr));
132133
}
133-
props.insert(idx, JSXAttributeItem::Attribute(attr));
134134
}
135135
JSXAttributeItem::SpreadAttribute(spread) => {
136136
spread_props.push(spread.argument.clone_in(ast_builder.allocator));
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.js\",\nr#\"\"use strict\";Object.defineProperty(exports,Symbol.toStringTag,{value:\"Module\"});const e=require(\"react/jsx-runtime\"),{Box,Text,Flex}=require(\"@devup-ui/react\");function t(){return e.jsxs(\"div\",{children:[e.jsx(Box,{_hover:{bg:\"blue\"},bg:\"$text\",color:\"red\",children:\"hello\"}),e.jsx(Text,{typography:`header`,children:\"typo\"}),e.jsx(Flex,{as:\"section\",mt:2,children:\"section\"})]})}exports.Lib=t;\"#,\nExtractOption\n{\n package: \"@devup-ui/react\".to_string(), css_dir:\n \"@devup-ui/react\".to_string(), single_css: true, import_main_css: false\n}).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "background",
10+
value: "$text",
11+
level: 0,
12+
selector: None,
13+
style_order: None,
14+
},
15+
),
16+
Static(
17+
ExtractStaticStyle {
18+
property: "background",
19+
value: "blue",
20+
level: 0,
21+
selector: Some(
22+
Selector(
23+
"&:hover",
24+
),
25+
),
26+
style_order: None,
27+
},
28+
),
29+
Static(
30+
ExtractStaticStyle {
31+
property: "color",
32+
value: "red",
33+
level: 0,
34+
selector: None,
35+
style_order: None,
36+
},
37+
),
38+
Static(
39+
ExtractStaticStyle {
40+
property: "display",
41+
value: "flex",
42+
level: 0,
43+
selector: None,
44+
style_order: Some(
45+
0,
46+
),
47+
},
48+
),
49+
Static(
50+
ExtractStaticStyle {
51+
property: "margin-top",
52+
value: "8px",
53+
level: 0,
54+
selector: None,
55+
style_order: None,
56+
},
57+
),
58+
Typography(
59+
"header",
60+
),
61+
},
62+
code: "\"use strict\";\nimport \"@devup-ui/react/devup-ui.css\";\nObject.defineProperty(exports, Symbol.toStringTag, { value: \"Module\" });\nconst e = require(\"react/jsx-runtime\"), { Box, Text, Flex } = require(\"@devup-ui/react\");\nfunction t() {\n\treturn e.jsxs(\"div\", { children: [\n\t\te.jsx(\"div\", {\n\t\t\tchildren: \"hello\",\n\t\t\tclassName: \"a b c\"\n\t\t}),\n\t\te.jsx(\"span\", {\n\t\t\tchildren: \"typo\",\n\t\t\tclassName: \"typo-header\"\n\t\t}),\n\t\te.jsx(\"section\", {\n\t\t\tchildren: \"section\",\n\t\t\tclassName: \"d e\"\n\t\t})\n\t] });\n}\nexports.Lib = t;\n",
63+
}

libs/extractor/src/visit.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,7 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> {
448448
.insert(import.local.to_string(), import.imported.to_string());
449449
}
450450
}
451-
return;
452-
}
453-
454-
if it.source.value == self.package
451+
} else if it.source.value == self.package
455452
&& let Some(specifiers) = &mut it.specifiers
456453
{
457454
for i in (0..specifiers.len()).rev() {
@@ -506,10 +503,9 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> {
506503
}
507504
}
508505
}
509-
return;
506+
} else {
507+
walk_import_declaration(self, it);
510508
}
511-
512-
walk_import_declaration(self, it);
513509
}
514510
fn visit_jsx_element(&mut self, elem: &mut JSXElement<'a>) {
515511
walk_jsx_element(self, elem);

0 commit comments

Comments
 (0)