Skip to content

Commit dfebd9c

Browse files
committed
Fix selector
1 parent 5116241 commit dfebd9c

File tree

4 files changed

+150
-49
lines changed

4 files changed

+150
-49
lines changed

libs/extractor/src/extractor/extract_style_from_expression.rs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -197,51 +197,47 @@ pub fn extract_style_from_expression<'a>(
197197
}
198198
}
199199

200-
let sel = part_of_selector
201-
.iter()
202-
.map(|name| {
203-
if let Some(selector) = selector {
204-
if name.starts_with("_") {
205-
if name.starts_with("_theme") {
206-
StyleSelector::from([
207-
to_kebab_case(name.strip_prefix("_").unwrap_or(name))
208-
.as_str(),
209-
&selector.to_string(),
210-
])
211-
.to_string()
212-
} else {
213-
StyleSelector::from([
214-
&selector.to_string(),
215-
to_kebab_case(name.strip_prefix("_").unwrap_or(name))
216-
.as_str(),
217-
])
218-
.to_string()
219-
}
200+
for sel in part_of_selector.iter().map(|name| {
201+
if let Some(selector) = selector {
202+
if name.starts_with("_") {
203+
if name.starts_with("_theme") {
204+
StyleSelector::from([
205+
to_kebab_case(name.strip_prefix("_").unwrap_or(name))
206+
.as_str(),
207+
&selector.to_string(),
208+
])
209+
.to_string()
220210
} else {
221-
name.replace("&", &selector.to_string())
211+
StyleSelector::from([
212+
&selector.to_string(),
213+
to_kebab_case(name.strip_prefix("_").unwrap_or(name))
214+
.as_str(),
215+
])
216+
.to_string()
222217
}
223-
} else if name.starts_with("_") {
224-
StyleSelector::from(
225-
to_kebab_case(name.strip_prefix("_").unwrap_or(name)).as_str(),
226-
)
227-
.to_string()
228218
} else {
229-
StyleSelector::from(name.strip_prefix("_").unwrap_or(name))
230-
.to_string()
219+
name.replace("&", &selector.to_string())
231220
}
232-
})
233-
.collect::<Vec<_>>()
234-
.join(",");
235-
props.extend(
236-
extract_style_from_expression(
237-
ast_builder,
238-
None,
239-
&mut o.value,
240-
level,
241-
&Some(StyleSelector::Selector(sel)),
242-
)
243-
.styles,
244-
);
221+
} else if name.starts_with("_") {
222+
StyleSelector::from(
223+
to_kebab_case(name.strip_prefix("_").unwrap_or(name)).as_str(),
224+
)
225+
.to_string()
226+
} else {
227+
StyleSelector::from(name.strip_prefix("_").unwrap_or(name)).to_string()
228+
}
229+
}) {
230+
props.extend(
231+
extract_style_from_expression(
232+
ast_builder,
233+
None,
234+
&mut o.value,
235+
level,
236+
&Some(StyleSelector::Selector(sel)),
237+
)
238+
.styles,
239+
);
240+
}
245241
}
246242
}
247243
return ExtractResult {

libs/extractor/src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,8 +2310,61 @@ import clsx from 'clsx'
23102310
)
23112311
.unwrap()
23122312
));
2313+
2314+
reset_class_map();
2315+
assert_debug_snapshot!(ToBTreeSet::from(
2316+
extract(
2317+
"test.tsx",
2318+
r#"import {Box} from '@devup-ui/core'
2319+
<Box selectors={{
2320+
"& .a, & .b": {
2321+
bg: "$primary"
2322+
}
2323+
}} />
2324+
"#,
2325+
ExtractOption {
2326+
package: "@devup-ui/core".to_string(),
2327+
css_dir: "@devup-ui/core".to_string(),
2328+
single_css: true,
2329+
import_main_css: false
2330+
}
2331+
)
2332+
.unwrap()
2333+
));
23132334
}
23142335

2336+
// #[test]
2337+
// #[serial]
2338+
// fn optimize_func1() {
2339+
// reset_class_map();
2340+
// assert_debug_snapshot!(ToBTreeSet::from(
2341+
// extract(
2342+
// "test.tsx",
2343+
// r#"import {Box} from '@devup-ui/core'
2344+
// <Box selectors={{
2345+
// ".test-picker__day--keyboard-selected": {
2346+
// _hover: {
2347+
// bg: "$primary"
2348+
// },
2349+
// selectors: {
2350+
// "&:active": {
2351+
// bg: "$primary"
2352+
// }
2353+
// }
2354+
// }
2355+
// }} />
2356+
// "#,
2357+
// ExtractOption {
2358+
// package: "@devup-ui/core".to_string(),
2359+
// css_dir: "@devup-ui/core".to_string(),
2360+
// single_css: true,
2361+
// import_main_css: false
2362+
// }
2363+
// )
2364+
// .unwrap()
2365+
// ));
2366+
// }
2367+
23152368
#[test]
23162369
#[serial]
23172370
fn optimize_func() {

libs/extractor/src/snapshots/extractor__tests__extract_selector-6.snap

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: libs/extractor/src/lib.rs
3-
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"_hover, _active\": {\n mx: 1\n }\n }} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
3+
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"_hover, _active\": {\n mx: 1\n }\n }} />\n \"#,\nExtractOption\n{\n package: \"@devup-ui/core\".to_string(), css_dir:\n \"@devup-ui/core\".to_string(), single_css: true, import_main_css: false\n}).unwrap())"
44
---
55
ToBTreeSet {
66
styles: {
@@ -11,7 +11,33 @@ ToBTreeSet {
1111
level: 0,
1212
selector: Some(
1313
Selector(
14-
"&:hover,&:active",
14+
"&:hover",
15+
),
16+
),
17+
style_order: None,
18+
},
19+
),
20+
Static(
21+
ExtractStaticStyle {
22+
property: "margin-left",
23+
value: "4px",
24+
level: 0,
25+
selector: Some(
26+
Selector(
27+
"&:active",
28+
),
29+
),
30+
style_order: None,
31+
},
32+
),
33+
Static(
34+
ExtractStaticStyle {
35+
property: "margin-right",
36+
value: "4px",
37+
level: 0,
38+
selector: Some(
39+
Selector(
40+
"&:hover",
1541
),
1642
),
1743
style_order: None,
@@ -24,12 +50,12 @@ ToBTreeSet {
2450
level: 0,
2551
selector: Some(
2652
Selector(
27-
"&:hover,&:active",
53+
"&:active",
2854
),
2955
),
3056
style_order: None,
3157
},
3258
),
3359
},
34-
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"a b\" />;\n",
60+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"a b c d\" />;\n",
3561
}

libs/extractor/src/snapshots/extractor__tests__extract_selector-7.snap

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: libs/extractor/src/lib.rs
3-
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"&:hover, &:active\": {\n mx: 1\n }\n }} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
3+
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"&:hover, &:active\": {\n mx: 1\n }\n }} />\n \"#,\nExtractOption\n{\n package: \"@devup-ui/core\".to_string(), css_dir:\n \"@devup-ui/core\".to_string(), single_css: true, import_main_css: false\n}).unwrap())"
44
---
55
ToBTreeSet {
66
styles: {
@@ -11,7 +11,33 @@ ToBTreeSet {
1111
level: 0,
1212
selector: Some(
1313
Selector(
14-
"&:hover,&:active",
14+
"&:hover",
15+
),
16+
),
17+
style_order: None,
18+
},
19+
),
20+
Static(
21+
ExtractStaticStyle {
22+
property: "margin-left",
23+
value: "4px",
24+
level: 0,
25+
selector: Some(
26+
Selector(
27+
"&:active",
28+
),
29+
),
30+
style_order: None,
31+
},
32+
),
33+
Static(
34+
ExtractStaticStyle {
35+
property: "margin-right",
36+
value: "4px",
37+
level: 0,
38+
selector: Some(
39+
Selector(
40+
"&:hover",
1541
),
1642
),
1743
style_order: None,
@@ -24,12 +50,12 @@ ToBTreeSet {
2450
level: 0,
2551
selector: Some(
2652
Selector(
27-
"&:hover,&:active",
53+
"&:active",
2854
),
2955
),
3056
style_order: None,
3157
},
3258
),
3359
},
34-
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"a b\" />;\n",
60+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"a b c d\" />;\n",
3561
}

0 commit comments

Comments
 (0)