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/dull-eyes-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devup-ui/wasm": patch
---

Refactor classname
102 changes: 49 additions & 53 deletions libs/extractor/src/gen_class_name.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::prop_modify_utils::convert_class_name;
use crate::{ExtractStyleProp, StyleProperty};
use oxc_allocator::CloneIn;
use oxc_ast::AstBuilder;
use oxc_ast::ast::{
Expression, ObjectPropertyKind, PropertyKey,
PropertyKind, TemplateElement, TemplateElementValue,
Expression, PropertyKey, PropertyKind, TemplateElement,
TemplateElementValue,
};
use oxc_span::SPAN;

Expand Down Expand Up @@ -34,14 +35,14 @@
}
let target = st.extract();

Some(Expression::StringLiteral(ast_builder.alloc_string_literal(
Some(ast_builder.expression_string_literal(
SPAN,
ast_builder.atom(match &target {
StyleProperty::ClassName(cls) => cls,
StyleProperty::Variable { class_name, .. } => class_name,
}),
None,
)))
))
}
ExtractStyleProp::StaticArray(res) => merge_expression_for_class_name(
ast_builder,
Expand All @@ -58,59 +59,60 @@
let consequent = consequent
.as_mut()
.and_then(|ref mut con| gen_class_name(ast_builder, con.as_mut(), style_order))
.unwrap_or_else(|| {
Expression::StringLiteral(ast_builder.alloc_string_literal(SPAN, "", None))
});
.unwrap_or_else(|| ast_builder.expression_string_literal(SPAN, "", None));

let alternate = alternate
.as_mut()
.and_then(|ref mut alt| gen_class_name(ast_builder, alt, style_order))
.unwrap_or_else(|| {
Expression::StringLiteral(ast_builder.alloc_string_literal(SPAN, "", None))
});
.unwrap_or_else(|| ast_builder.expression_string_literal(SPAN, "", None));
if is_same_expression(&consequent, &alternate) {
Some(consequent)
} else {
Some(Expression::ConditionalExpression(
ast_builder.alloc_conditional_expression(
SPAN,
condition.clone_in(ast_builder.allocator),
consequent,
alternate,
),
Some(ast_builder.expression_conditional(
SPAN,

Check warning on line 72 in libs/extractor/src/gen_class_name.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/gen_class_name.rs#L72

Added line #L72 was not covered by tests
condition.clone_in(ast_builder.allocator),
consequent,
alternate,

Check warning on line 75 in libs/extractor/src/gen_class_name.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/gen_class_name.rs#L74-L75

Added lines #L74 - L75 were not covered by tests
))
}
}
ExtractStyleProp::Expression { expression, .. } => {
Some(expression.clone_in(ast_builder.allocator))
}
ExtractStyleProp::MemberExpression { map, expression } => Some(
Expression::ComputedMemberExpression(ast_builder.alloc_computed_member_expression(
SPAN,
Expression::ObjectExpression(ast_builder.alloc_object_expression(
// direct select
ExtractStyleProp::MemberExpression { map, expression } => {
let exp =
Expression::ComputedMemberExpression(ast_builder.alloc_computed_member_expression(

Check warning on line 85 in libs/extractor/src/gen_class_name.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/gen_class_name.rs#L85

Added line #L85 was not covered by tests
SPAN,
ast_builder.vec_from_iter(map.iter_mut().filter_map(|(key, value)| {
gen_class_name(ast_builder, value.as_mut(), style_order).map(|expr| {
ObjectPropertyKind::ObjectProperty(ast_builder.alloc_object_property(
SPAN,
PropertyKind::Init,
PropertyKey::StringLiteral(ast_builder.alloc_string_literal(
ast_builder.expression_object(
SPAN,

Check warning on line 88 in libs/extractor/src/gen_class_name.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/gen_class_name.rs#L88

Added line #L88 was not covered by tests
ast_builder.vec_from_iter(map.iter_mut().filter_map(|(key, value)| {
gen_class_name(ast_builder, value.as_mut(), style_order).map(|expr| {
ast_builder.object_property_kind_object_property(
SPAN,
ast_builder.atom(key),
None,
)),
expr,
false,
false,
false,
))
})
})),
)),
expression.clone_in(ast_builder.allocator),
false,
)),
),
PropertyKind::Init,
PropertyKey::StringLiteral(ast_builder.alloc_string_literal(
SPAN,

Check warning on line 95 in libs/extractor/src/gen_class_name.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/gen_class_name.rs#L95

Added line #L95 was not covered by tests
ast_builder.atom(key),
None,
)),
expr,
false,
false,
false,

Check warning on line 102 in libs/extractor/src/gen_class_name.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/gen_class_name.rs#L99-L102

Added lines #L99 - L102 were not covered by tests
)
})
})),
),
expression.clone_in(ast_builder.allocator),
false,

Check warning on line 108 in libs/extractor/src/gen_class_name.rs

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/gen_class_name.rs#L108

Added line #L108 was not covered by tests
));
if let Expression::Identifier(_) = &expression {
Some(convert_class_name(ast_builder, &exp))
} else {
Some(exp)
}
}
}
}
fn is_same_expression<'a>(a: &Expression<'a>, b: &Expression<'a>) -> bool {
Expand Down Expand Up @@ -196,21 +198,15 @@
}
}

Some(Expression::TemplateLiteral(
ast_builder.alloc_template_literal(
SPAN,
qu,
oxc_allocator::Vec::from_iter_in(unknown_expr, ast_builder.allocator),
),
Some(ast_builder.expression_template_literal(
SPAN,

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

View check run for this annotation

Codecov / codecov/patch

libs/extractor/src/gen_class_name.rs#L202

Added line #L202 was not covered by tests
qu,
oxc_allocator::Vec::from_iter_in(unknown_expr, ast_builder.allocator),
))
}
} else if class_name.is_empty() {
None
} else {
Some(Expression::StringLiteral(ast_builder.alloc_string_literal(
SPAN,
ast_builder.atom(&class_name),
None,
)))
Some(ast_builder.expression_string_literal(SPAN, ast_builder.atom(&class_name), None))
}
}
Loading