Fix chained attribute if statements with static strings (#4721)
This commit is contained in:
1 parent
f2b1c6d97f
commit
c709c415c9
2 files changed
+28
-11
No files matched your search
@@ -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" },
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -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<Box<AttributeValue>> {
|
||||
@@ -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 }
|
||||
|
||||
Reference in new issue
Block a user