Skip to content

Commit f167da2

Browse files
authored
Merge pull request #217 from dev-five-git/fix-classname-order-issue
Fix classname order issue
2 parents 7d1cbbb + 2ab9a9e commit f167da2

9 files changed

+192
-53
lines changed

.changeset/tame-berries-jam.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 classname order issue

libs/extractor/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,40 @@ mod tests {
570570
)
571571
.unwrap()
572572
));
573+
574+
reset_class_map();
575+
assert_debug_snapshot!(ToBTreeSet::from(
576+
extract(
577+
"test.tsx",
578+
r#"import {Box as C} from '@devup-ui/core'
579+
<C padding={1} margin={2} className={variable} />
580+
"#,
581+
ExtractOption {
582+
package: "@devup-ui/core".to_string(),
583+
css_file: None
584+
}
585+
)
586+
.unwrap()
587+
));
588+
589+
reset_class_map();
590+
assert_debug_snapshot!(ToBTreeSet::from(
591+
extract(
592+
"test.tsx",
593+
r#"import {Box as C} from '@devup-ui/core'
594+
<C padding={1}
595+
_hover={{
596+
borderColor: true ? 'blue' : ``,
597+
}}
598+
className={variable} />
599+
"#,
600+
ExtractOption {
601+
package: "@devup-ui/core".to_string(),
602+
css_file: None
603+
}
604+
)
605+
.unwrap()
606+
));
573607
}
574608

575609
#[test]

libs/extractor/src/prop_modify_utils.rs

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,40 +205,98 @@ fn merge_string_expressions<'a>(
205205
return None;
206206
}
207207
if expressions.len() == 1 {
208-
return Some(expressions[0].clone_in(ast_builder.allocator));
208+
return Some(expressions.first().unwrap().clone_in(ast_builder.allocator));
209209
}
210210

211-
let mut string_literals = vec![];
211+
let mut string_literals: std::vec::Vec<String> = vec![];
212212
let mut other_expressions = vec![];
213213
for ex in expressions {
214+
string_literals.push("".to_string());
214215
if let Expression::StringLiteral(literal) = ex {
215-
string_literals.push(literal.value.trim());
216+
if !string_literals.is_empty() {
217+
string_literals
218+
.last_mut()
219+
.unwrap()
220+
.push_str(&format!(" {}", literal.value.trim()));
221+
} else {
222+
string_literals.push(literal.value.trim().to_string());
223+
}
224+
} else if let Expression::TemplateLiteral(template) = ex {
225+
if !string_literals.is_empty() {
226+
string_literals.last_mut().unwrap().push_str(&format!(
227+
" {}",
228+
template
229+
.quasis
230+
.iter()
231+
.map(|q| q.value.raw.trim())
232+
.filter(|q| !q.is_empty())
233+
.collect::<Vec<_>>()
234+
.join(" ")
235+
));
236+
} else {
237+
string_literals.push(
238+
template
239+
.quasis
240+
.iter()
241+
.map(|q| q.value.raw.as_str())
242+
.collect::<Vec<_>>()
243+
.join(" "),
244+
);
245+
}
246+
other_expressions.extend(template.expressions.clone_in(ast_builder.allocator));
216247
} else {
217-
other_expressions.push(ex);
248+
other_expressions.push(ex.clone_in(ast_builder.allocator));
218249
}
219250
}
220251
if other_expressions.is_empty() {
221-
return Some(Expression::StringLiteral(ast_builder.alloc_string_literal(
222-
SPAN,
223-
ast_builder.atom(&string_literals.join(" ")),
224-
None,
225-
)));
252+
return Some(Expression::StringLiteral(
253+
ast_builder.alloc_string_literal(
254+
SPAN,
255+
ast_builder.atom(
256+
&string_literals
257+
.iter()
258+
.map(|s| s.trim())
259+
.filter(|s| !s.is_empty())
260+
.collect::<Vec<_>>()
261+
.join(" "),
262+
),
263+
None,
264+
),
265+
));
226266
}
267+
let literals = oxc_allocator::Vec::from_iter_in(
268+
string_literals.iter().enumerate().map(|(idx, s)| {
269+
ast_builder.template_element(
270+
SPAN,
271+
TemplateElementValue {
272+
raw: ast_builder.atom(&{
273+
let trimmed = s.trim();
274+
if trimmed.is_empty() {
275+
"".to_string()
276+
} else if idx > 0 && idx == string_literals.len() - 1 {
277+
if string_literals.len() == other_expressions.len() {
278+
format!(" {trimmed} ")
279+
} else {
280+
format!(" {trimmed}")
281+
}
282+
} else if idx == string_literals.len() - 1 {
283+
trimmed.to_string()
284+
} else {
285+
format!("{trimmed} ")
286+
}
287+
}),
288+
cooked: None,
289+
},
290+
false,
291+
)
292+
}),
293+
ast_builder.allocator,
294+
);
227295

228296
Some(Expression::TemplateLiteral(
229297
ast_builder.alloc_template_literal(
230298
SPAN,
231-
oxc_allocator::Vec::from_iter_in(
232-
[ast_builder.template_element(
233-
SPAN,
234-
TemplateElementValue {
235-
raw: ast_builder.atom(&format!("{} ", string_literals.join(" "))),
236-
cooked: None,
237-
},
238-
false,
239-
)],
240-
ast_builder.allocator,
241-
),
299+
literals,
242300
oxc_allocator::Vec::from_iter_in(
243301
other_expressions
244302
.into_iter()
@@ -264,7 +322,7 @@ fn merge_object_expressions<'a>(
264322
ast_builder.alloc_object_expression(
265323
SPAN,
266324
oxc_allocator::Vec::from_iter_in(
267-
expressions.into_iter().map(|ex| {
325+
expressions.iter().map(|ex| {
268326
ObjectPropertyKind::SpreadProperty(
269327
ast_builder.alloc_spread_element(SPAN, ex.clone_in(ast_builder.allocator)),
270328
)

libs/extractor/src/snapshots/extractor__tests__extract_class_name_from_component.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ ToBTreeSet {
4545
},
4646
),
4747
},
48-
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={`d0 d1 d2 d3 ${\"a\" + \"b\"}`} />;\n",
48+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={`${\"a\" + \"b\"} d0 d1 d2 d3`} />;\n",
4949
}

libs/extractor/src/snapshots/extractor__tests__extract_style_props_with_class_name-5.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ ToBTreeSet {
2323
},
2424
),
2525
},
26-
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={`d0 d1 ${\"a\" + \"b\"}`} />;\n",
26+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={`${\"a\" + \"b\"} d0 d1`} />;\n",
2727
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ ToBTreeSet {
2323
},
2424
),
2525
},
26-
code: "import \"@devup-ui/core/devup-ui.css\";\n<img src=\"/next.svg\" alt=\"Next.js logo\" className={`d0 d1 ${styles.logo}`} />;\n",
26+
code: "import \"@devup-ui/core/devup-ui.css\";\n<img src=\"/next.svg\" alt=\"Next.js logo\" className={`${styles.logo} d0 d1`} />;\n",
2727
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {Box as C} from '@devup-ui/core'\n <C padding={1} margin={2} className={variable} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "margin",
10+
value: "8px",
11+
level: 0,
12+
selector: None,
13+
style_order: None,
14+
},
15+
),
16+
Static(
17+
ExtractStaticStyle {
18+
property: "padding",
19+
value: "4px",
20+
level: 0,
21+
selector: None,
22+
style_order: None,
23+
},
24+
),
25+
},
26+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={`${variable} d0 d1`} />;\n",
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {Box as C} from '@devup-ui/core'\n <C padding={1} \n _hover={{\n borderColor: true ? 'blue' : ``,\n }}\n className={variable} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "borderColor",
10+
value: "",
11+
level: 0,
12+
selector: Some(
13+
Selector(
14+
"&:hover",
15+
),
16+
),
17+
style_order: None,
18+
},
19+
),
20+
Static(
21+
ExtractStaticStyle {
22+
property: "borderColor",
23+
value: "blue",
24+
level: 0,
25+
selector: Some(
26+
Selector(
27+
"&:hover",
28+
),
29+
),
30+
style_order: None,
31+
},
32+
),
33+
Static(
34+
ExtractStaticStyle {
35+
property: "padding",
36+
value: "4px",
37+
level: 0,
38+
selector: None,
39+
style_order: None,
40+
},
41+
),
42+
},
43+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={`${variable} d0 ${true ? \"d1\" : \"d2\"}`} />;\n",
44+
}

libs/extractor/src/snapshots/extractor__tests__extract_style_props_with_class_name1.snap

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)