Skip to content

Commit cb114cc

Browse files
committed
Extract nested
1 parent 79758c9 commit cb114cc

File tree

34 files changed

+1931
-162
lines changed

34 files changed

+1931
-162
lines changed

libs/extractor/src/gen_class_name.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{ExtractStyleProp, StyleProperty};
22
use oxc_allocator::CloneIn;
33
use oxc_ast::ast::{
4-
Expression, JSXAttribute, JSXAttributeValue, JSXExpression, TemplateElement,
5-
TemplateElementValue,
4+
Expression, JSXAttribute, JSXAttributeValue, JSXExpression, ObjectPropertyKind, PropertyKey,
5+
PropertyKind, TemplateElement, TemplateElementValue,
66
};
77
use oxc_ast::AstBuilder;
88
use oxc_span::SPAN;
@@ -78,6 +78,34 @@ fn gen_class_name<'a>(
7878
ExtractStyleProp::Expression { expression, .. } => {
7979
Some(expression.clone_in(ast_builder.allocator))
8080
}
81+
ExtractStyleProp::MemberExpression { map, expression } => Some(
82+
Expression::ComputedMemberExpression(ast_builder.alloc_computed_member_expression(
83+
SPAN,
84+
Expression::ObjectExpression(ast_builder.alloc_object_expression(
85+
SPAN,
86+
ast_builder.vec_from_iter(map.iter().filter_map(|(key, value)| {
87+
gen_class_name(ast_builder, value).map(|expr| {
88+
ObjectPropertyKind::ObjectProperty(ast_builder.alloc_object_property(
89+
SPAN,
90+
PropertyKind::Init,
91+
PropertyKey::StringLiteral(ast_builder.alloc_string_literal(
92+
SPAN,
93+
key.as_str(),
94+
None,
95+
)),
96+
expr,
97+
false,
98+
false,
99+
false,
100+
))
101+
})
102+
})),
103+
None,
104+
)),
105+
expression.clone_in(ast_builder.allocator),
106+
false,
107+
)),
108+
),
81109
}
82110
}
83111
fn is_same_expression<'a>(a: &Expression<'a>, b: &Expression<'a>) -> bool {

libs/extractor/src/gen_style.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use oxc_ast::ast::{
66
};
77
use oxc_ast::AstBuilder;
88
use oxc_span::SPAN;
9+
use std::collections::BTreeMap;
910
pub fn gen_styles<'a>(
1011
ast_builder: &AstBuilder<'a>,
1112
style_props: &[ExtractStyleProp<'a>],
@@ -198,6 +199,90 @@ fn gen_style<'a>(
198199
}
199200
}
200201
}
202+
ExtractStyleProp::MemberExpression { map, expression } => {
203+
let mut tmp_map = BTreeMap::<String, Vec<(String, String)>>::new();
204+
for (key, value) in map.iter() {
205+
for style in value.extract() {
206+
match style.extract() {
207+
StyleProperty::ClassName(_) => {}
208+
StyleProperty::Variable {
209+
variable_name,
210+
identifier,
211+
..
212+
} => {
213+
tmp_map
214+
.entry(variable_name)
215+
.or_default()
216+
.push((key.to_string(), identifier));
217+
}
218+
}
219+
}
220+
}
221+
222+
for (key, value) in tmp_map {
223+
properties.push(ObjectPropertyKind::ObjectProperty(
224+
ast_builder.alloc_object_property(
225+
SPAN,
226+
PropertyKind::Init,
227+
PropertyKey::StringLiteral(
228+
ast_builder.alloc_string_literal(SPAN, key, None),
229+
),
230+
if value.len() == 1 {
231+
// do not create object expression when property is single
232+
Expression::Identifier(
233+
ast_builder.alloc_identifier_reference(SPAN, &value[0].1),
234+
)
235+
} else {
236+
Expression::ComputedMemberExpression(
237+
ast_builder.alloc_computed_member_expression(
238+
SPAN,
239+
Expression::ObjectExpression(
240+
ast_builder.alloc_object_expression(
241+
SPAN,
242+
oxc_allocator::Vec::from_iter_in(
243+
value
244+
.into_iter()
245+
.map(|(k, v)| {
246+
ObjectPropertyKind::ObjectProperty(
247+
ast_builder.alloc_object_property(
248+
SPAN,
249+
PropertyKind::Init,
250+
PropertyKey::StaticIdentifier(
251+
ast_builder
252+
.alloc_identifier_name(
253+
SPAN, k,
254+
),
255+
),
256+
Expression::Identifier(
257+
ast_builder
258+
.alloc_identifier_reference(
259+
SPAN, v,
260+
),
261+
),
262+
false,
263+
false,
264+
false,
265+
),
266+
)
267+
})
268+
.collect::<Vec<_>>(),
269+
ast_builder.allocator,
270+
),
271+
None,
272+
),
273+
),
274+
expression.clone_in(ast_builder.allocator),
275+
false,
276+
),
277+
)
278+
},
279+
false,
280+
false,
281+
false,
282+
),
283+
));
284+
}
285+
}
201286
}
202287
properties.sort_by_key(|p| {
203288
if let ObjectPropertyKind::ObjectProperty(p) = p {

0 commit comments

Comments
 (0)