Skip to content

Commit b4fc65d

Browse files
committed
Refactor
1 parent 2dd81f5 commit b4fc65d

17 files changed

+410
-304
lines changed

libs/extractor/src/gen_class_name.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{ExtractStyleProp, StyleProperty};
22
use oxc_allocator::CloneIn;
33
use oxc_ast::AstBuilder;
44
use oxc_ast::ast::{
5-
Expression, JSXAttribute, JSXAttributeValue, JSXExpression, ObjectPropertyKind, PropertyKey,
5+
Expression, ObjectPropertyKind, PropertyKey,
66
PropertyKind, TemplateElement, TemplateElementValue,
77
};
88
use oxc_span::SPAN;
@@ -214,52 +214,3 @@ pub fn merge_expression_for_class_name<'a>(
214214
)))
215215
}
216216
}
217-
218-
pub fn apply_class_name_attribute<'a>(
219-
ast_builder: &AstBuilder<'a>,
220-
class_prop: &mut JSXAttribute<'a>,
221-
expression: Expression<'a>,
222-
) {
223-
if let Some(ref value) = class_prop.value {
224-
if let Some(ret) = match value {
225-
JSXAttributeValue::StringLiteral(str) => merge_expression_for_class_name(
226-
ast_builder,
227-
vec![
228-
Expression::StringLiteral(str.clone_in(ast_builder.allocator)),
229-
expression,
230-
],
231-
),
232-
JSXAttributeValue::ExpressionContainer(container) => match container.expression {
233-
JSXExpression::EmptyExpression(_) => Some(expression),
234-
_ => merge_expression_for_class_name(
235-
ast_builder,
236-
vec![
237-
container
238-
.expression
239-
.clone_in(ast_builder.allocator)
240-
.into_expression(),
241-
expression,
242-
],
243-
),
244-
},
245-
_ => None,
246-
} {
247-
class_prop.value = match ret {
248-
Expression::StringLiteral(literal) => Some(JSXAttributeValue::StringLiteral(
249-
literal.clone_in(ast_builder.allocator),
250-
)),
251-
_ => Some(JSXAttributeValue::ExpressionContainer(
252-
ast_builder.alloc_jsx_expression_container(SPAN, JSXExpression::from(ret)),
253-
)),
254-
}
255-
}
256-
} else {
257-
class_prop.value = Some(if let Expression::StringLiteral(literal) = expression {
258-
JSXAttributeValue::StringLiteral(literal.clone_in(ast_builder.allocator))
259-
} else {
260-
JSXAttributeValue::ExpressionContainer(
261-
ast_builder.alloc_jsx_expression_container(SPAN, JSXExpression::from(expression)),
262-
)
263-
});
264-
};
265-
}

libs/extractor/src/gen_style.rs

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
use crate::{ExtractStyleProp, StyleProperty};
22
use oxc_allocator::CloneIn;
33
use oxc_ast::AstBuilder;
4-
use oxc_ast::ast::{
5-
Expression, JSXAttribute, JSXAttributeValue, JSXExpression, ObjectExpression,
6-
ObjectPropertyKind, PropertyKey, PropertyKind,
7-
};
4+
use oxc_ast::ast::{Expression, ObjectPropertyKind, PropertyKey, PropertyKind};
85
use oxc_span::SPAN;
96
use std::collections::BTreeMap;
107
pub fn gen_styles<'a>(
118
ast_builder: &AstBuilder<'a>,
129
style_props: &[ExtractStyleProp<'a>],
13-
) -> Option<ObjectExpression<'a>> {
10+
) -> Option<Expression<'a>> {
1411
if style_props.is_empty() {
1512
return None;
1613
}
@@ -22,9 +19,11 @@ pub fn gen_styles<'a>(
2219
if properties.is_empty() {
2320
return None;
2421
}
25-
Some(ast_builder.object_expression(
26-
SPAN,
27-
oxc_allocator::Vec::from_iter_in(properties, ast_builder.allocator),
22+
Some(Expression::ObjectExpression(
23+
ast_builder.alloc_object_expression(
24+
SPAN,
25+
oxc_allocator::Vec::from_iter_in(properties, ast_builder.allocator),
26+
),
2827
))
2928
}
3029
fn gen_style<'a>(
@@ -345,39 +344,3 @@ fn gen_style<'a>(
345344
properties.reverse();
346345
properties
347346
}
348-
349-
pub fn apply_style_attribute<'a>(
350-
ast_builder: &AstBuilder<'a>,
351-
style_prop: &mut JSXAttribute<'a>,
352-
// must be an object expression
353-
mut expression: ObjectExpression<'a>,
354-
) {
355-
if let Some(ref mut value) = style_prop.value {
356-
if let JSXAttributeValue::ExpressionContainer(container) = value {
357-
if let Some(spread_property) = match &container.expression {
358-
JSXExpression::ObjectExpression(obj) => Some(Expression::ObjectExpression(
359-
obj.clone_in(ast_builder.allocator),
360-
)),
361-
JSXExpression::Identifier(ident) => Some(Expression::Identifier(
362-
ident.clone_in(ast_builder.allocator),
363-
)),
364-
_ => None,
365-
} {
366-
expression.properties.insert(
367-
0,
368-
ObjectPropertyKind::SpreadProperty(
369-
ast_builder.alloc_spread_element(SPAN, spread_property),
370-
),
371-
);
372-
}
373-
container.expression = JSXExpression::ObjectExpression(ast_builder.alloc(expression));
374-
}
375-
} else {
376-
style_prop.value = Some(JSXAttributeValue::ExpressionContainer(
377-
ast_builder.alloc_jsx_expression_container(
378-
SPAN,
379-
JSXExpression::ObjectExpression(ast_builder.alloc(expression)),
380-
),
381-
));
382-
};
383-
}

libs/extractor/src/lib.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,54 @@ export {
18181818
)
18191819
.unwrap()
18201820
));
1821+
1822+
reset_class_map();
1823+
assert_debug_snapshot!(ToBTreeSet::from(
1824+
extract(
1825+
"test.js",
1826+
r#"import { jsx as e } from "react/jsx-runtime";
1827+
import { Box as o } from "@devup-ui/core";
1828+
e(o, { className: "a", bg: "red" })
1829+
"#,
1830+
ExtractOption {
1831+
package: "@devup-ui/core".to_string(),
1832+
css_file: None
1833+
}
1834+
)
1835+
.unwrap()
1836+
));
1837+
1838+
reset_class_map();
1839+
assert_debug_snapshot!(ToBTreeSet::from(
1840+
extract(
1841+
"test.js",
1842+
r#"import { jsx as e } from "react/jsx-runtime";
1843+
import { Box as o } from "@devup-ui/core";
1844+
e(o, { className: "a", bg: variable, style: { color: "blue" } })
1845+
"#,
1846+
ExtractOption {
1847+
package: "@devup-ui/core".to_string(),
1848+
css_file: None
1849+
}
1850+
)
1851+
.unwrap()
1852+
));
1853+
1854+
reset_class_map();
1855+
assert_debug_snapshot!(ToBTreeSet::from(
1856+
extract(
1857+
"test.js",
1858+
r#"import { jsx as e } from "react/jsx-runtime";
1859+
import { Box as o } from "@devup-ui/core";
1860+
e(o, { className: "a", bg: variable, style: { color: "blue" }, ...props })
1861+
"#,
1862+
ExtractOption {
1863+
package: "@devup-ui/core".to_string(),
1864+
css_file: None
1865+
}
1866+
)
1867+
.unwrap()
1868+
));
18211869
}
18221870

18231871
#[test]
@@ -3260,4 +3308,24 @@ export {
32603308
.unwrap()
32613309
));
32623310
}
3311+
3312+
#[test]
3313+
#[serial]
3314+
fn style_variables_mjs() {
3315+
reset_class_map();
3316+
assert_debug_snapshot!(ToBTreeSet::from(
3317+
extract(
3318+
"test.js",
3319+
r#"import { jsx as e } from "react/jsx-runtime";
3320+
import { Box as o } from "@devup-ui/core";
3321+
e(o, { styleVars: { c: "yellow" } })
3322+
"#,
3323+
ExtractOption {
3324+
package: "@devup-ui/core".to_string(),
3325+
css_file: None
3326+
}
3327+
)
3328+
.unwrap()
3329+
));
3330+
}
32633331
}

0 commit comments

Comments
 (0)