Skip to content

Commit ab132a7

Browse files
committed
Merge branch 'main' into feat-landing
2 parents 368c34f + cbfce78 commit ab132a7

File tree

9 files changed

+172
-658
lines changed

9 files changed

+172
-658
lines changed

.changeset/chilly-mice-leave.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+
Refactor className logic

libs/extractor/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,40 @@ e(o, { className: "a", bg: variable, style: { color: "blue" }, ...props })
22852285
)
22862286
.unwrap()
22872287
));
2288+
2289+
reset_class_map();
2290+
assert_debug_snapshot!(ToBTreeSet::from(
2291+
extract(
2292+
"test.jsx",
2293+
r#"import { VStack } from '@devup-ui/core'
2294+
2295+
export default function Card({
2296+
children,
2297+
className,
2298+
...props
2299+
}) {
2300+
return (
2301+
<VStack
2302+
_active={{
2303+
boxShadow: 'none',
2304+
transform: 'scale(0.95)',
2305+
}}
2306+
className={className}
2307+
{...props}
2308+
>
2309+
{children}
2310+
</VStack>
2311+
)
2312+
}
2313+
2314+
"#,
2315+
ExtractOption {
2316+
package: "@devup-ui/core".to_string(),
2317+
css_file: None
2318+
}
2319+
)
2320+
.unwrap()
2321+
));
22882322
}
22892323

22902324
#[test]

libs/extractor/src/prop_modify_utils.rs

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ pub fn modify_props<'a>(
133133
}
134134
}
135135
}
136+
println!("class_name_prop: {:?}", class_name_prop);
137+
println!("style_prop: {:?}", style_prop);
136138
if let Some(ex) = get_class_name_expression(
137139
ast_builder,
138140
&class_name_prop,
@@ -179,17 +181,26 @@ pub fn get_class_name_expression<'a>(
179181
]
180182
.into_iter()
181183
.flatten()
182-
.chain(spread_props.iter().map(|ex| {
183-
convert_class_name(
184-
ast_builder,
185-
&Expression::StaticMemberExpression(ast_builder.alloc_static_member_expression(
186-
SPAN,
187-
ex.clone_in(ast_builder.allocator),
188-
ast_builder.identifier_name(SPAN, ast_builder.atom("className")),
189-
true,
190-
)),
191-
)
192-
}))
184+
.chain(if class_name_prop.is_some() {
185+
vec![]
186+
} else {
187+
spread_props
188+
.iter()
189+
.map(|ex| {
190+
convert_class_name(
191+
ast_builder,
192+
&Expression::StaticMemberExpression(
193+
ast_builder.alloc_static_member_expression(
194+
SPAN,
195+
ex.clone_in(ast_builder.allocator),
196+
ast_builder.identifier_name(SPAN, ast_builder.atom("className")),
197+
true,
198+
),
199+
),
200+
)
201+
})
202+
.collect::<Vec<_>>()
203+
})
193204
.collect::<Vec<_>>()
194205
.as_slice(),
195206
)
@@ -245,76 +256,67 @@ fn merge_string_expressions<'a>(
245256
let mut string_literals: std::vec::Vec<String> = vec![];
246257
let mut other_expressions = vec![];
247258
let mut prev_str = String::new();
248-
for (idx, ex) in expressions.iter().enumerate() {
259+
for ex in expressions.iter() {
249260
match ex {
250261
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+
let target_prev = prev_str.trim();
263+
let target = literal.value.trim();
264+
prev_str = format!(
265+
"{}{}{}",
266+
target_prev,
267+
if target_prev.is_empty() { "" } else { " " },
268+
target
262269
);
263270
}
264271
Expression::TemplateLiteral(template) => {
265272
for (idx, q) in template.quasis.iter().enumerate() {
266-
if !prev_str.is_empty() {
273+
let target_prev = prev_str.trim();
274+
let target = q.value.raw.trim();
275+
if idx < template.quasis.len() - 1 {
267276
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 {
277+
"{}{}{}{}{}",
278+
if !other_expressions.is_empty() || idx > 0 {
275279
" "
276-
}
277-
));
278-
prev_str = String::new();
279-
} else if q.tail {
280-
prev_str = q.value.raw.trim().to_string();
281-
} else {
282-
string_literals.push(format!(
283-
"{}{}{}",
284-
if idx == 0
285-
&& other_expressions.is_empty()
286-
&& string_literals.is_empty()
287-
{
288-
""
289280
} else {
290-
" "
291-
},
292-
q.value.raw.trim(),
293-
if q.value.raw.trim().is_empty() || !q.value.raw.ends_with(' ') {
294281
""
295-
} else {
282+
},
283+
target_prev,
284+
if !target_prev.is_empty() { " " } else { "" },
285+
target,
286+
if !target.is_empty() && !target.ends_with("typo-") {
296287
" "
288+
} else {
289+
""
297290
}
298291
));
299-
prev_str = String::new();
292+
} else {
293+
prev_str = q.value.raw.trim().to_string();
300294
}
301295
}
302296
other_expressions.extend(template.expressions.clone_in(ast_builder.allocator));
303297
}
304298
ex => {
299+
let target_prev = prev_str.trim();
305300
string_literals.push(format!(
306-
"{}{}",
307-
prev_str.trim(),
308-
if idx > 0 { " " } else { "" }
301+
"{}{}{}",
302+
if !other_expressions.is_empty() {
303+
" "
304+
} else {
305+
""
306+
},
307+
target_prev,
308+
if !target_prev.is_empty() { " " } else { "" }
309309
));
310310
other_expressions.push(ex.clone_in(ast_builder.allocator));
311311
prev_str = String::new();
312312
}
313313
}
314314
}
315-
if !prev_str.is_empty() {
316-
string_literals.push(prev_str.trim_end().to_string());
317-
}
315+
string_literals.push(format!(
316+
"{}{}",
317+
if !prev_str.trim().is_empty() { " " } else { "" },
318+
prev_str.trim(),
319+
));
318320
if other_expressions.is_empty() {
319321
return Some(ast_builder.expression_string_literal(
320322
SPAN,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ ToBTreeSet {
2020
"buttonS",
2121
),
2222
},
23-
code: "import \"@devup-ui/core/devup-ui.css\";\nimport clsx from \"clsx\";\n<button {...props} className={`${clsx(variants[variant], isError && variant === \"default\" && errorClassNames, className) || \"\"} d0 ${isPrimary ? {\n\t\"md\": \"typo-buttonM\",\n\t\"sm\": \"typo-buttonS\"\n}[size] || \"\" : \"\"} ${props?.className || \"\"}`} style={props?.style} />;\n",
23+
code: "import \"@devup-ui/core/devup-ui.css\";\nimport clsx from \"clsx\";\n<button {...props} className={`${clsx(variants[variant], isError && variant === \"default\" && errorClassNames, className) || \"\"} d0 ${isPrimary ? {\n\t\"md\": \"typo-buttonM\",\n\t\"sm\": \"typo-buttonS\"\n}[size] || \"\" : \"\"}`} style={props?.style} />;\n",
2424
}

0 commit comments

Comments
 (0)