From 8e9ae70773246f341e2625c1b7e3dc060bc2dc75 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Fri, 3 Oct 2025 11:58:56 -0500 Subject: [PATCH] Fix chained attribute if statements with static strings --- .../tests/conditional_formatted_attributes.rs | 15 ++++++++++++ packages/rsx/src/attribute.rs | 24 ++++++++++--------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/core/tests/conditional_formatted_attributes.rs b/packages/core/tests/conditional_formatted_attributes.rs index cd0e860ec8..26b9cf5574 100644 --- a/packages/core/tests/conditional_formatted_attributes.rs +++ b/packages/core/tests/conditional_formatted_attributes.rs @@ -28,5 +28,20 @@ fn partially_formatted_conditional_attribute() { input { type: if true { opt.to_string() } else if true { "" } else { "text with {width}" }, } + input { + type: if true { "one" } else if true { "two" } else { "three" }, + } + input { + type: if true { "one" } else if true { "two" } else if true { "three" } else { opt }, + } + input { + type: if true { "one" } else if true { if false { "true" } else { "false" } } else { "three" }, + } + input { + type: if true { "one".to_string() }, + } + input { + type: if true { "one" }, + } }; } diff --git a/packages/rsx/src/attribute.rs b/packages/rsx/src/attribute.rs index 6b96a1897b..9353febc5f 100644 --- a/packages/rsx/src/attribute.rs +++ b/packages/rsx/src/attribute.rs @@ -614,17 +614,19 @@ impl IfAttributeValue { } fn contains_expression(&self) -> bool { - if let AttributeValue::AttrExpr(_) = &*self.then_value { - return true; - } - match &self.else_value { - Some(attribute) => match attribute.as_ref() { - AttributeValue::IfExpr(if_expr) => if_expr.is_terminated(), - AttributeValue::AttrExpr(_) => true, - _ => false, - }, - None => false, + fn attribute_value_contains_expression(expr: &AttributeValue) -> bool { + match expr { + AttributeValue::IfExpr(if_expr) => if_expr.contains_expression(), + AttributeValue::AttrLiteral(_) => false, + _ => true, + } } + + attribute_value_contains_expression(&self.then_value) + || self + .else_value + .as_deref() + .is_some_and(attribute_value_contains_expression) } fn parse_attribute_value_from_block(block: &Block) -> syn::Result> { @@ -691,7 +693,7 @@ impl IfAttributeValue { } } - let then_value = quote_attribute_value_string(then_value, terminated); + let then_value = quote_attribute_value_string(then_value, contains_expression); let then_value = if terminated { quote! { #then_value }