Skip to content

Commit 6cf75f5

Browse files
committed
Add testcase
1 parent 1ae6103 commit 6cf75f5

2 files changed

Lines changed: 75 additions & 38 deletions

File tree

libs/extractor/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14036,4 +14036,46 @@ export { c as Lib };"#,
1403614036
.unwrap()
1403714037
));
1403814038
}
14039+
14040+
#[test]
14041+
fn test_combine_conditional_class_name() {
14042+
use crate::prop_modify_utils::combine_conditional_class_name;
14043+
use crate::utils::expression_to_code;
14044+
use oxc_span::SPAN;
14045+
14046+
let allocator = Allocator::default();
14047+
let builder = oxc_ast::AstBuilder::new(&allocator);
14048+
14049+
let make_cond = || builder.expression_identifier(SPAN, builder.atom("cond"));
14050+
let make_str = |s| builder.expression_string_literal(SPAN, builder.atom(s), None);
14051+
14052+
// (Some, Some) — both branches have classNames
14053+
let result = combine_conditional_class_name(
14054+
&builder,
14055+
make_cond(),
14056+
Some(make_str("a")),
14057+
Some(make_str("b")),
14058+
);
14059+
assert!(result.is_some());
14060+
let code = expression_to_code(&result.unwrap());
14061+
assert!(code.contains("cond"), "expected condition in: {code}");
14062+
14063+
// (Some, None) — only consequent has className, alternate falls back to ""
14064+
let result =
14065+
combine_conditional_class_name(&builder, make_cond(), Some(make_str("a")), None);
14066+
assert!(result.is_some());
14067+
let code = expression_to_code(&result.unwrap());
14068+
assert!(code.contains("cond"), "expected condition in: {code}");
14069+
14070+
// (None, Some) — only alternate has className, consequent falls back to ""
14071+
let result =
14072+
combine_conditional_class_name(&builder, make_cond(), None, Some(make_str("b")));
14073+
assert!(result.is_some());
14074+
let code = expression_to_code(&result.unwrap());
14075+
assert!(code.contains("cond"), "expected condition in: {code}");
14076+
14077+
// (None, None) — neither has className
14078+
let result = combine_conditional_class_name(&builder, make_cond(), None, None);
14079+
assert!(result.is_none());
14080+
}
1403914081
}

libs/extractor/src/prop_modify_utils.rs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@ use oxc_ast::ast::{
1515
use oxc_span::SPAN;
1616
use std::collections::HashMap;
1717

18+
/// Combine two optional className expressions into a conditional expression.
19+
/// `condition ? con_expr : alt_expr`, falling back to `""` for the missing branch.
20+
/// Returns `None` only when both branches are `None`.
21+
pub(crate) fn combine_conditional_class_name<'a>(
22+
ast_builder: &AstBuilder<'a>,
23+
condition: Expression<'a>,
24+
con_expr: Option<Expression<'a>>,
25+
alt_expr: Option<Expression<'a>>,
26+
) -> Option<Expression<'a>> {
27+
match (con_expr, alt_expr) {
28+
(Some(con), Some(alt)) => {
29+
Some(ast_builder.expression_conditional(SPAN, condition, con, alt))
30+
}
31+
(Some(con), None) => Some(ast_builder.expression_conditional(
32+
SPAN,
33+
condition,
34+
con,
35+
ast_builder.expression_string_literal(SPAN, "", None),
36+
)),
37+
(None, Some(alt)) => Some(ast_builder.expression_conditional(
38+
SPAN,
39+
condition,
40+
ast_builder.expression_string_literal(SPAN, "", None),
41+
alt,
42+
)),
43+
(None, None) => None,
44+
}
45+
}
46+
1847
/// modify object props
1948
/// Returns extracted Tailwind styles from static className strings
2049
/// `conditional_branch`: If Some, contains (condition, alternate_styles, alternate_style_order)
@@ -79,25 +108,8 @@ pub fn modify_prop_object<'a>(
79108
filename,
80109
);
81110

82-
// Combine into conditional expression: condition ? consequent_class : alternate_class
83-
let combined_expr = match (con_expr, alt_expr) {
84-
(Some(con), Some(alt)) => {
85-
Some(ast_builder.expression_conditional(SPAN, condition, con, alt))
86-
}
87-
(Some(con), None) => Some(ast_builder.expression_conditional(
88-
SPAN,
89-
condition,
90-
con,
91-
ast_builder.expression_string_literal(SPAN, "", None),
92-
)),
93-
(None, Some(alt)) => Some(ast_builder.expression_conditional(
94-
SPAN,
95-
condition,
96-
ast_builder.expression_string_literal(SPAN, "", None),
97-
alt,
98-
)),
99-
(None, None) => None,
100-
};
111+
let combined_expr =
112+
combine_conditional_class_name(ast_builder, condition, con_expr, alt_expr);
101113

102114
let mut all_tailwind = con_tailwind;
103115
all_tailwind.extend(alt_tailwind);
@@ -226,25 +238,8 @@ pub fn modify_props<'a>(
226238
filename,
227239
);
228240

229-
// Combine into conditional expression: condition ? consequent_class : alternate_class
230-
let combined_expr = match (con_expr, alt_expr) {
231-
(Some(con), Some(alt)) => {
232-
Some(ast_builder.expression_conditional(SPAN, condition, con, alt))
233-
}
234-
(Some(con), None) => Some(ast_builder.expression_conditional(
235-
SPAN,
236-
condition,
237-
con,
238-
ast_builder.expression_string_literal(SPAN, "", None),
239-
)),
240-
(None, Some(alt)) => Some(ast_builder.expression_conditional(
241-
SPAN,
242-
condition,
243-
ast_builder.expression_string_literal(SPAN, "", None),
244-
alt,
245-
)),
246-
(None, None) => None,
247-
};
241+
let combined_expr =
242+
combine_conditional_class_name(ast_builder, condition, con_expr, alt_expr);
248243

249244
let mut all_tailwind = con_tailwind;
250245
all_tailwind.extend(alt_tailwind);

0 commit comments

Comments
 (0)