Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-mice-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devup-ui/wasm": patch
---

Refactor className logic
34 changes: 34 additions & 0 deletions libs/extractor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,40 @@ e(o, { className: "a", bg: variable, style: { color: "blue" }, ...props })
)
.unwrap()
));

reset_class_map();
assert_debug_snapshot!(ToBTreeSet::from(
extract(
"test.jsx",
r#"import { VStack } from '@devup-ui/core'

export default function Card({
children,
className,
...props
}) {
return (
<VStack
_active={{
boxShadow: 'none',
transform: 'scale(0.95)',
}}
className={className}
{...props}
>
{children}
</VStack>
)
}

"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap()
));
}

#[test]
Expand Down
114 changes: 58 additions & 56 deletions libs/extractor/src/prop_modify_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
}
}
}
println!("class_name_prop: {:?}", class_name_prop);
println!("style_prop: {:?}", style_prop);
if let Some(ex) = get_class_name_expression(
ast_builder,
&class_name_prop,
Expand Down Expand Up @@ -179,17 +181,26 @@
]
.into_iter()
.flatten()
.chain(spread_props.iter().map(|ex| {
convert_class_name(
ast_builder,
&Expression::StaticMemberExpression(ast_builder.alloc_static_member_expression(
SPAN,
ex.clone_in(ast_builder.allocator),
ast_builder.identifier_name(SPAN, ast_builder.atom("className")),
true,
)),
)
}))
.chain(if class_name_prop.is_some() {
vec![]
} else {
spread_props
.iter()

Check warning on line 188 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L188

Added line #L188 was not covered by tests
.map(|ex| {
convert_class_name(
ast_builder,
&Expression::StaticMemberExpression(
ast_builder.alloc_static_member_expression(
SPAN,

Check warning on line 194 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L194

Added line #L194 was not covered by tests
ex.clone_in(ast_builder.allocator),
ast_builder.identifier_name(SPAN, ast_builder.atom("className")),
true,

Check warning on line 197 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L197

Added line #L197 was not covered by tests
),
),
)
})
.collect::<Vec<_>>()

Check warning on line 202 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L202

Added line #L202 was not covered by tests
})
.collect::<Vec<_>>()
.as_slice(),
)
Expand Down Expand Up @@ -245,76 +256,67 @@
let mut string_literals: std::vec::Vec<String> = vec![];
let mut other_expressions = vec![];
let mut prev_str = String::new();
for (idx, ex) in expressions.iter().enumerate() {
for ex in expressions.iter() {
match ex {
Expression::StringLiteral(literal) => {
prev_str.push_str(
format!(
"{}{}",
if prev_str.trim().is_empty() && other_expressions.is_empty() {
""
} else {
" "
},
literal.value.trim()
)
.as_str(),
let target_prev = prev_str.trim();
let target = literal.value.trim();
prev_str = format!(
"{}{}{}",
target_prev,

Check warning on line 266 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L265-L266

Added lines #L265 - L266 were not covered by tests
if target_prev.is_empty() { "" } else { " " },
target

Check warning on line 268 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L268

Added line #L268 was not covered by tests
);
}
Expression::TemplateLiteral(template) => {
for (idx, q) in template.quasis.iter().enumerate() {
if !prev_str.is_empty() {
let target_prev = prev_str.trim();
let target = q.value.raw.trim();
if idx < template.quasis.len() - 1 {
string_literals.push(format!(
"{}{}{}{}",
prev_str.trim(),
if !prev_str.trim().is_empty() { " " } else { "" },
q.value.raw.trim(),
if idx == template.quasis.len() - 1 {
""
} else {
"{}{}{}{}{}",

Check warning on line 277 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L277

Added line #L277 was not covered by tests
if !other_expressions.is_empty() || idx > 0 {
" "
}
));
prev_str = String::new();
} else if q.tail {
prev_str = q.value.raw.trim().to_string();
} else {
string_literals.push(format!(
"{}{}{}",
if idx == 0
&& other_expressions.is_empty()
&& string_literals.is_empty()
{
""
} else {
" "
},
q.value.raw.trim(),
if q.value.raw.trim().is_empty() || !q.value.raw.ends_with(' ') {
""
} else {
},
target_prev,

Check warning on line 283 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L283

Added line #L283 was not covered by tests
if !target_prev.is_empty() { " " } else { "" },
target,

Check warning on line 285 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L285

Added line #L285 was not covered by tests
if !target.is_empty() && !target.ends_with("typo-") {
" "
} else {
""
}
));
prev_str = String::new();
} else {
prev_str = q.value.raw.trim().to_string();
}
}
other_expressions.extend(template.expressions.clone_in(ast_builder.allocator));
}
ex => {
let target_prev = prev_str.trim();
string_literals.push(format!(
"{}{}",
prev_str.trim(),
if idx > 0 { " " } else { "" }
"{}{}{}",

Check warning on line 301 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L301

Added line #L301 was not covered by tests
if !other_expressions.is_empty() {
" "
} else {
""
},
target_prev,

Check warning on line 307 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L307

Added line #L307 was not covered by tests
if !target_prev.is_empty() { " " } else { "" }
));
other_expressions.push(ex.clone_in(ast_builder.allocator));
prev_str = String::new();
}
}
}
if !prev_str.is_empty() {
string_literals.push(prev_str.trim_end().to_string());
}
string_literals.push(format!(
"{}{}",

Check warning on line 316 in libs/extractor/src/prop_modify_utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/prop_modify_utils.rs#L316

Added line #L316 was not covered by tests
if !prev_str.trim().is_empty() { " " } else { "" },
prev_str.trim(),
));
if other_expressions.is_empty() {
return Some(ast_builder.expression_string_literal(
SPAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ ToBTreeSet {
"buttonS",
),
},
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",
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",
}
Loading