Skip to content

Commit a0fc823

Browse files
committed
Fix conditional style issue
1 parent b6997df commit a0fc823

File tree

6 files changed

+119
-21
lines changed

6 files changed

+119
-21
lines changed

.changeset/sixty-wombats-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/wasm": patch
3+
---
4+
5+
Fix conditional style issue

libs/extractor/src/gen_style.rs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,59 @@ fn gen_style<'a>(
7777
(None, None) => {
7878
return vec![];
7979
}
80-
(Some(c), None) | (None, Some(c)) => {
81-
gen_style(ast_builder, c)
82-
.into_iter()
83-
.for_each(|p| properties.push(p));
80+
(None, Some(c)) => {
81+
gen_style(ast_builder, c).into_iter().for_each(|p| {
82+
if let ObjectPropertyKind::ObjectProperty(p) = p {
83+
properties.push(ObjectPropertyKind::ObjectProperty(
84+
ast_builder.alloc_object_property(
85+
SPAN,
86+
PropertyKind::Init,
87+
p.key.clone_in(ast_builder.allocator),
88+
Expression::ConditionalExpression(
89+
ast_builder.alloc_conditional_expression(
90+
SPAN,
91+
condition.clone_in(ast_builder.allocator),
92+
Expression::Identifier(
93+
ast_builder
94+
.alloc_identifier_reference(SPAN, "undefined"),
95+
),
96+
p.value.clone_in(ast_builder.allocator),
97+
),
98+
),
99+
false,
100+
false,
101+
false,
102+
),
103+
))
104+
}
105+
});
106+
}
107+
(Some(c), None) => {
108+
gen_style(ast_builder, c).into_iter().for_each(|p| {
109+
if let ObjectPropertyKind::ObjectProperty(p) = p {
110+
properties.push(ObjectPropertyKind::ObjectProperty(
111+
ast_builder.alloc_object_property(
112+
SPAN,
113+
PropertyKind::Init,
114+
p.key.clone_in(ast_builder.allocator),
115+
Expression::ConditionalExpression(
116+
ast_builder.alloc_conditional_expression(
117+
SPAN,
118+
condition.clone_in(ast_builder.allocator),
119+
p.value.clone_in(ast_builder.allocator),
120+
Expression::Identifier(
121+
ast_builder
122+
.alloc_identifier_reference(SPAN, "undefined"),
123+
),
124+
),
125+
),
126+
false,
127+
false,
128+
false,
129+
),
130+
))
131+
}
132+
});
84133
}
85134
(Some(c), Some(a)) => {
86135
let collect_c = gen_style(ast_builder, c);

libs/extractor/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,32 @@ mod tests {
655655
"test.tsx",
656656
r#"import { Box } from "@devup-ui/core";
657657
<Box margin={a === b ? undefined : 2} />;
658+
"#,
659+
ExtractOption {
660+
package: "@devup-ui/core".to_string(),
661+
css_file: None
662+
}
663+
)
664+
.unwrap());
665+
666+
reset_class_map();
667+
assert_debug_snapshot!(extract(
668+
"test.tsx",
669+
r#"import { Box } from "@devup-ui/core";
670+
<Box margin={a === b ? `${a}px` : undefined} />;
671+
"#,
672+
ExtractOption {
673+
package: "@devup-ui/core".to_string(),
674+
css_file: None
675+
}
676+
)
677+
.unwrap());
678+
679+
reset_class_map();
680+
assert_debug_snapshot!(extract(
681+
"test.tsx",
682+
r#"import { Box } from "@devup-ui/core";
683+
<Box margin={a === b ? null : `${b}px`} />;
658684
"#,
659685
ExtractOption {
660686
package: "@devup-ui/core".to_string(),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "extract(\"test.tsx\",\nr#\"import { Box } from \"@devup-ui/core\";\n<Box margin={a === b ? `${a}px` : undefined} />;\n\"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
4+
---
5+
ExtractOutput {
6+
styles: [
7+
Dynamic(
8+
ExtractDynamicStyle {
9+
property: "margin",
10+
level: 0,
11+
identifier: "`${a}px`",
12+
selector: None,
13+
},
14+
),
15+
],
16+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={a === b ? \"d0\" : \"\"} style={{ \"--d1\": a === b ? `${a}px` : undefined }} />;\n",
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "extract(\"test.tsx\",\nr#\"import { Box } from \"@devup-ui/core\";\n<Box margin={a === b ? null : `${b}px`} />;\n\"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
4+
---
5+
ExtractOutput {
6+
styles: [
7+
Dynamic(
8+
ExtractDynamicStyle {
9+
property: "margin",
10+
level: 0,
11+
identifier: "`${b}px`",
12+
selector: None,
13+
},
14+
),
15+
],
16+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={a === b ? \"\" : \"d0\"} style={{ \"--d1\": a === b ? undefined : `${b}px` }} />;\n",
17+
}

libs/extractor/src/style_extractor.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ pub fn extract_style_from_expression<'a>(
7474
level: u8,
7575
selector: Option<&str>,
7676
) -> ExtractResult<'a> {
77-
println!(
78-
"extract_style_from_expression: {:?} {:?} {:?}",
79-
selector, name, expression
80-
);
8177
let mut typo = false;
8278

8379
if name.is_none() && selector.is_none() {
@@ -262,19 +258,7 @@ pub fn extract_style_from_expression<'a>(
262258
}
263259
typo = name == "typography";
264260
}
265-
if let Some(value) = get_number_by_literal_expression(expression) {
266-
name.map(|name| {
267-
ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(Static(
268-
ExtractStaticStyle::new(
269-
name,
270-
&value.to_string(),
271-
level,
272-
selector.map(|s| s.into()),
273-
),
274-
))])
275-
})
276-
.unwrap_or(ExtractResult::Maintain)
277-
} else if let Some(value) = get_string_by_literal_expression(expression) {
261+
if let Some(value) = get_string_by_literal_expression(expression) {
278262
name.map(|name| {
279263
ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(if typo {
280264
Typography(value.as_str().to_string())

0 commit comments

Comments
 (0)