Skip to content

Commit bdb4392

Browse files
committed
Fix classname
1 parent d7d7ef4 commit bdb4392

File tree

5 files changed

+281
-29
lines changed

5 files changed

+281
-29
lines changed

libs/extractor/src/lib.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,66 @@ e(o, { className: "a", bg: variable, style: { color: "blue" }, ...props })
26732673
));
26742674
}
26752675

2676+
// #[test]
2677+
// #[serial]
2678+
// fn props_multi_expression_1() {
2679+
// assert_debug_snapshot!(ToBTreeSet::from(
2680+
// extract(
2681+
// "test.tsx",
2682+
// r#"import {Box as C} from '@devup-ui/core'
2683+
// <C padding={1}
2684+
// _hover={{
2685+
// borderColor: true ? 'blue' : ``,
2686+
// }}
2687+
// className={variable} />
2688+
// "#,
2689+
// ExtractOption {
2690+
// package: "@devup-ui/core".to_string(),
2691+
// css_file: None
2692+
// }
2693+
// )
2694+
// .unwrap()
2695+
// ));
2696+
// }
2697+
#[test]
2698+
#[serial]
2699+
fn props_multi_expression() {
2700+
reset_class_map();
2701+
assert_debug_snapshot!(ToBTreeSet::from(
2702+
extract(
2703+
"test.jsx",
2704+
r#"import {
2705+
Box,
2706+
Button as DevupButton,
2707+
Center,
2708+
css,
2709+
} from '@devup-ui/core'
2710+
2711+
<DevupButton
2712+
border={
2713+
{
2714+
primary: 'none',
2715+
default: '1px solid var(--border, #E4E4E4)',
2716+
}[variant]
2717+
}
2718+
className={className}
2719+
px={
2720+
{
2721+
false: { sm: '12px', md: '16px', lg: '20px' }[size],
2722+
true: { sm: '24px', md: '28px', lg: '32px' }[size],
2723+
}[(!!icon).toString()]
2724+
}
2725+
/>
2726+
"#,
2727+
ExtractOption {
2728+
package: "@devup-ui/core".to_string(),
2729+
css_file: None
2730+
}
2731+
)
2732+
.unwrap()
2733+
));
2734+
}
2735+
26762736
#[test]
26772737
#[serial]
26782738
fn props_direct_object_select() {
@@ -3161,6 +3221,40 @@ import {Button} from '@devup/ui'
31613221
)
31623222
.unwrap()
31633223
));
3224+
3225+
reset_class_map();
3226+
assert_debug_snapshot!(ToBTreeSet::from(
3227+
extract(
3228+
"test.jsx",
3229+
r#"import {Box} from '@devup-ui/core'
3230+
<Box className={` ${1} ${2} `} />
3231+
"#,
3232+
ExtractOption {
3233+
package: "@devup-ui/core".to_string(),
3234+
css_file: None
3235+
}
3236+
)
3237+
.unwrap()
3238+
));
3239+
3240+
reset_class_map();
3241+
assert_debug_snapshot!(ToBTreeSet::from(
3242+
extract(
3243+
"test.jsx",
3244+
r#"import {Box} from '@devup-ui/core'
3245+
<Box className={` ${1} ${2} `}
3246+
_hover={{bg:"red"}}
3247+
_themeDark={{ _hover:{bg:"black"} }}
3248+
3249+
/>
3250+
"#,
3251+
ExtractOption {
3252+
package: "@devup-ui/core".to_string(),
3253+
css_file: None
3254+
}
3255+
)
3256+
.unwrap()
3257+
));
31643258
}
31653259

31663260
#[test]

libs/extractor/src/prop_modify_utils.rs

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -233,49 +233,83 @@ fn merge_string_expressions<'a>(
233233
if expressions.is_empty() {
234234
return None;
235235
}
236-
if expressions.len() == 1 {
236+
if expressions.len() == 1
237+
&& !matches!(
238+
expressions.first().unwrap(),
239+
Expression::StringLiteral(_) | Expression::TemplateLiteral(_)
240+
)
241+
{
237242
return Some(expressions.first().unwrap().clone_in(ast_builder.allocator));
238243
}
239244

240245
let mut string_literals: std::vec::Vec<String> = vec![];
241246
let mut other_expressions = vec![];
242247
let mut prev_str = String::new();
243248
for (idx, ex) in expressions.iter().enumerate() {
244-
if let Expression::StringLiteral(literal) = ex {
245-
if !prev_str.trim().is_empty() {
246-
prev_str.push(' ');
249+
match ex {
250+
Expression::StringLiteral(literal) => {
251+
prev_str.push_str(
252+
format!(
253+
"{}{}",
254+
if prev_str.trim().is_empty() && other_expressions.is_empty() {
255+
""
256+
} else {
257+
" "
258+
},
259+
literal.value.trim()
260+
)
261+
.as_str(),
262+
);
247263
}
248-
prev_str.push_str(literal.value.trim());
249-
} else if let Expression::TemplateLiteral(template) = ex {
250-
for (idx, q) in template.quasis.iter().enumerate() {
251-
if idx == 0 {
252-
if !prev_str.trim().is_empty() {
253-
prev_str.push(' ');
254-
}
255-
prev_str.push_str(&q.value.raw);
256-
} else {
257-
if !prev_str.trim().is_empty() {
258-
string_literals.push(prev_str.clone());
259-
}
260-
if q.tail {
261-
prev_str = format!("{} ", q.value.raw);
264+
Expression::TemplateLiteral(template) => {
265+
for (idx, q) in template.quasis.iter().enumerate() {
266+
if !prev_str.is_empty() {
267+
string_literals.push(format!(
268+
"{}{}{}{}",
269+
prev_str.trim(),
270+
if !prev_str.trim().is_empty() { " " } else { "" },
271+
q.value.raw.trim(),
272+
if idx == template.quasis.len() - 1 {
273+
""
274+
} else {
275+
" "
276+
}
277+
));
278+
prev_str = String::new();
279+
} else if q.tail {
280+
prev_str = q.value.raw.trim().to_string();
262281
} else {
263-
string_literals.push(q.value.raw.into());
282+
string_literals.push(format!(
283+
"{}{}{}",
284+
if idx == 0
285+
&& other_expressions.is_empty()
286+
&& string_literals.is_empty()
287+
{
288+
""
289+
} else {
290+
" "
291+
},
292+
q.value.raw.trim(),
293+
if q.value.raw.trim().is_empty() || !q.value.raw.ends_with(' ') {
294+
""
295+
} else {
296+
" "
297+
}
298+
));
264299
prev_str = String::new();
265300
}
266301
}
302+
other_expressions.extend(template.expressions.clone_in(ast_builder.allocator));
267303
}
268-
other_expressions.extend(template.expressions.clone_in(ast_builder.allocator));
269-
} else {
270-
if !prev_str.ends_with(' ') {
271-
string_literals.push(format!("{}{}", prev_str, if idx > 0 { " " } else { "" }));
272-
} else if idx > 0 {
273-
string_literals.push(" ".to_string());
274-
} else {
275-
string_literals.push("".to_string());
304+
ex => {
305+
string_literals.push(format!(
306+
"{}{}",
307+
prev_str.trim(),
308+
if idx > 0 { " " } else { "" }
309+
));
310+
other_expressions.push(ex.clone_in(ast_builder.allocator));
311+
prev_str = String::new();
276312
}
277-
other_expressions.push(ex.clone_in(ast_builder.allocator));
278-
prev_str = " ".to_string();
279313
}
280314
}
281315
if !prev_str.is_empty() {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.jsx\",\nr#\"import {\n Box,\n Button as DevupButton,\n Center,\n css,\n} from '@devup-ui/core'\n\n<DevupButton\n border={\n {\n primary: 'none',\n default: '1px solid var(--border, #E4E4E4)',\n }[variant]\n }\n className={className}\n px={\n {\n false: { sm: '12px', md: '16px', lg: '20px' }[size],\n true: { sm: '24px', md: '28px', lg: '32px' }[size],\n }[(!!icon).toString()]\n }\n/>\n\"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "border",
10+
value: "1px solid var(--border,#E4E4E4)",
11+
level: 0,
12+
selector: None,
13+
style_order: None,
14+
},
15+
),
16+
Static(
17+
ExtractStaticStyle {
18+
property: "border",
19+
value: "none",
20+
level: 0,
21+
selector: None,
22+
style_order: None,
23+
},
24+
),
25+
Static(
26+
ExtractStaticStyle {
27+
property: "px",
28+
value: "12px",
29+
level: 0,
30+
selector: None,
31+
style_order: None,
32+
},
33+
),
34+
Static(
35+
ExtractStaticStyle {
36+
property: "px",
37+
value: "16px",
38+
level: 0,
39+
selector: None,
40+
style_order: None,
41+
},
42+
),
43+
Static(
44+
ExtractStaticStyle {
45+
property: "px",
46+
value: "20px",
47+
level: 0,
48+
selector: None,
49+
style_order: None,
50+
},
51+
),
52+
Static(
53+
ExtractStaticStyle {
54+
property: "px",
55+
value: "24px",
56+
level: 0,
57+
selector: None,
58+
style_order: None,
59+
},
60+
),
61+
Static(
62+
ExtractStaticStyle {
63+
property: "px",
64+
value: "28px",
65+
level: 0,
66+
selector: None,
67+
style_order: None,
68+
},
69+
),
70+
Static(
71+
ExtractStaticStyle {
72+
property: "px",
73+
value: "32px",
74+
level: 0,
75+
selector: None,
76+
style_order: None,
77+
},
78+
),
79+
},
80+
code: "import \"@devup-ui/core/devup-ui.css\";\n<button className={`${className || \"\"} ${{\n\t\"default\": \"d0\",\n\t\"primary\": \"d1\"\n}[variant] || \"\"} ${{\n\t\"false\": {\n\t\t\"lg\": \"d2\",\n\t\t\"md\": \"d3\",\n\t\t\"sm\": \"d4\"\n\t}[size] || \"\",\n\t\"true\": {\n\t\t\"lg\": \"d5\",\n\t\t\"md\": \"d6\",\n\t\t\"sm\": \"d7\"\n\t}[size] || \"\"\n}[(!!icon).toString()]}`} />;\n",
81+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.jsx\",\nr#\"import {Box} from '@devup-ui/core'\n <Box className={` ${1} ${2} `} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {},
7+
code: "<div className={`${1} ${2}`} />;\n",
8+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.jsx\",\nr#\"import {Box} from '@devup-ui/core'\n <Box className={` ${1} ${2} `}\n _hover={{bg:\"red\"}}\n _themeDark={{ _hover:{bg:\"black\"} }}\n \n />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "background",
10+
value: "black",
11+
level: 0,
12+
selector: Some(
13+
Selector(
14+
":root[data-theme=dark] &:hover",
15+
),
16+
),
17+
style_order: None,
18+
},
19+
),
20+
Static(
21+
ExtractStaticStyle {
22+
property: "background",
23+
value: "red",
24+
level: 0,
25+
selector: Some(
26+
Selector(
27+
"&:hover",
28+
),
29+
),
30+
style_order: None,
31+
},
32+
),
33+
},
34+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={`${1} ${2} d0 d1`} />;\n",
35+
}

0 commit comments

Comments
 (0)