Skip to content

Commit 4a7e547

Browse files
authored
Merge pull request #231 from dev-five-git/fix-classname-refactor
Fix classname issue
2 parents d3f6f90 + 8885c6c commit 4a7e547

28 files changed

+479
-484
lines changed

.changeset/dull-eyes-eat.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

libs/extractor/src/gen_class_name.rs

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use crate::prop_modify_utils::convert_class_name;
12
use crate::{ExtractStyleProp, StyleProperty};
23
use oxc_allocator::CloneIn;
34
use oxc_ast::AstBuilder;
45
use oxc_ast::ast::{
5-
Expression, ObjectPropertyKind, PropertyKey,
6-
PropertyKind, TemplateElement, TemplateElementValue,
6+
Expression, PropertyKey, PropertyKind, TemplateElement,
7+
TemplateElementValue,
78
};
89
use oxc_span::SPAN;
910

@@ -34,14 +35,14 @@ fn gen_class_name<'a>(
3435
}
3536
let target = st.extract();
3637

37-
Some(Expression::StringLiteral(ast_builder.alloc_string_literal(
38+
Some(ast_builder.expression_string_literal(
3839
SPAN,
3940
ast_builder.atom(match &target {
4041
StyleProperty::ClassName(cls) => cls,
4142
StyleProperty::Variable { class_name, .. } => class_name,
4243
}),
4344
None,
44-
)))
45+
))
4546
}
4647
ExtractStyleProp::StaticArray(res) => merge_expression_for_class_name(
4748
ast_builder,
@@ -58,59 +59,60 @@ fn gen_class_name<'a>(
5859
let consequent = consequent
5960
.as_mut()
6061
.and_then(|ref mut con| gen_class_name(ast_builder, con.as_mut(), style_order))
61-
.unwrap_or_else(|| {
62-
Expression::StringLiteral(ast_builder.alloc_string_literal(SPAN, "", None))
63-
});
62+
.unwrap_or_else(|| ast_builder.expression_string_literal(SPAN, "", None));
6463

6564
let alternate = alternate
6665
.as_mut()
6766
.and_then(|ref mut alt| gen_class_name(ast_builder, alt, style_order))
68-
.unwrap_or_else(|| {
69-
Expression::StringLiteral(ast_builder.alloc_string_literal(SPAN, "", None))
70-
});
67+
.unwrap_or_else(|| ast_builder.expression_string_literal(SPAN, "", None));
7168
if is_same_expression(&consequent, &alternate) {
7269
Some(consequent)
7370
} else {
74-
Some(Expression::ConditionalExpression(
75-
ast_builder.alloc_conditional_expression(
76-
SPAN,
77-
condition.clone_in(ast_builder.allocator),
78-
consequent,
79-
alternate,
80-
),
71+
Some(ast_builder.expression_conditional(
72+
SPAN,
73+
condition.clone_in(ast_builder.allocator),
74+
consequent,
75+
alternate,
8176
))
8277
}
8378
}
8479
ExtractStyleProp::Expression { expression, .. } => {
8580
Some(expression.clone_in(ast_builder.allocator))
8681
}
87-
ExtractStyleProp::MemberExpression { map, expression } => Some(
88-
Expression::ComputedMemberExpression(ast_builder.alloc_computed_member_expression(
89-
SPAN,
90-
Expression::ObjectExpression(ast_builder.alloc_object_expression(
82+
// direct select
83+
ExtractStyleProp::MemberExpression { map, expression } => {
84+
let exp =
85+
Expression::ComputedMemberExpression(ast_builder.alloc_computed_member_expression(
9186
SPAN,
92-
ast_builder.vec_from_iter(map.iter_mut().filter_map(|(key, value)| {
93-
gen_class_name(ast_builder, value.as_mut(), style_order).map(|expr| {
94-
ObjectPropertyKind::ObjectProperty(ast_builder.alloc_object_property(
95-
SPAN,
96-
PropertyKind::Init,
97-
PropertyKey::StringLiteral(ast_builder.alloc_string_literal(
87+
ast_builder.expression_object(
88+
SPAN,
89+
ast_builder.vec_from_iter(map.iter_mut().filter_map(|(key, value)| {
90+
gen_class_name(ast_builder, value.as_mut(), style_order).map(|expr| {
91+
ast_builder.object_property_kind_object_property(
9892
SPAN,
99-
ast_builder.atom(key),
100-
None,
101-
)),
102-
expr,
103-
false,
104-
false,
105-
false,
106-
))
107-
})
108-
})),
109-
)),
110-
expression.clone_in(ast_builder.allocator),
111-
false,
112-
)),
113-
),
93+
PropertyKind::Init,
94+
PropertyKey::StringLiteral(ast_builder.alloc_string_literal(
95+
SPAN,
96+
ast_builder.atom(key),
97+
None,
98+
)),
99+
expr,
100+
false,
101+
false,
102+
false,
103+
)
104+
})
105+
})),
106+
),
107+
expression.clone_in(ast_builder.allocator),
108+
false,
109+
));
110+
if let Expression::Identifier(_) = &expression {
111+
Some(convert_class_name(ast_builder, &exp))
112+
} else {
113+
Some(exp)
114+
}
115+
}
114116
}
115117
}
116118
fn is_same_expression<'a>(a: &Expression<'a>, b: &Expression<'a>) -> bool {
@@ -196,21 +198,15 @@ pub fn merge_expression_for_class_name<'a>(
196198
}
197199
}
198200

199-
Some(Expression::TemplateLiteral(
200-
ast_builder.alloc_template_literal(
201-
SPAN,
202-
qu,
203-
oxc_allocator::Vec::from_iter_in(unknown_expr, ast_builder.allocator),
204-
),
201+
Some(ast_builder.expression_template_literal(
202+
SPAN,
203+
qu,
204+
oxc_allocator::Vec::from_iter_in(unknown_expr, ast_builder.allocator),
205205
))
206206
}
207207
} else if class_name.is_empty() {
208208
None
209209
} else {
210-
Some(Expression::StringLiteral(ast_builder.alloc_string_literal(
211-
SPAN,
212-
ast_builder.atom(&class_name),
213-
None,
214-
)))
210+
Some(ast_builder.expression_string_literal(SPAN, ast_builder.atom(&class_name), None))
215211
}
216212
}

0 commit comments

Comments
 (0)