fix dx fmt span attribute issue (#5132)
* fix: if expr spans causing invalid span locations * cleanups * clippy, typo
This commit is contained in:
1 parent
b050dfec5c
commit
db23197925
4 files changed
+63
-38
No files matched your search
@@ -17,7 +17,7 @@
|
||||
use super::literal::HotLiteral;
|
||||
use crate::{innerlude::*, partial_closure::PartialClosure};
|
||||
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use proc_macro2::{Span, TokenStream as TokenStream2};
|
||||
use quote::{quote, quote_spanned, ToTokens, TokenStreamExt};
|
||||
use std::fmt::Display;
|
||||
use syn::{
|
||||
@@ -173,7 +173,7 @@ impl Attribute {
|
||||
let AttributeValue::AttrExpr(expr) = &self.value else {
|
||||
unreachable!("Spread attributes should always be expressions")
|
||||
};
|
||||
return quote! { {#expr}.into_boxed_slice() };
|
||||
return quote_spanned! { expr.span() => {#expr}.into_boxed_slice() };
|
||||
}
|
||||
|
||||
let el_name = self
|
||||
@@ -289,8 +289,9 @@ impl Attribute {
|
||||
}
|
||||
};
|
||||
|
||||
let attr_span = attribute.span();
|
||||
let completion_hints = self.completion_hints();
|
||||
quote! {
|
||||
quote_spanned! { attr_span =>
|
||||
Box::new([
|
||||
{
|
||||
#completion_hints
|
||||
@@ -531,6 +532,7 @@ impl AttributeValue {
|
||||
/// A if else chain attribute value
|
||||
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
|
||||
pub struct IfAttributeValue {
|
||||
pub if_expr: ExprIf,
|
||||
pub condition: Expr,
|
||||
pub then_value: Box<AttributeValue>,
|
||||
pub else_value: Option<Box<AttributeValue>>,
|
||||
@@ -560,7 +562,7 @@ impl IfAttributeValue {
|
||||
return non_string_diagnostic(current_if_value.span());
|
||||
};
|
||||
|
||||
let condition = ¤t_if_value.condition;
|
||||
let condition = ¤t_if_value.if_expr.cond;
|
||||
expression.extend(quote! {
|
||||
if #condition {
|
||||
#new.to_string()
|
||||
@@ -599,8 +601,8 @@ impl IfAttributeValue {
|
||||
}
|
||||
}
|
||||
|
||||
fn span(&self) -> proc_macro2::Span {
|
||||
self.then_value.span()
|
||||
fn span(&self) -> Span {
|
||||
self.if_expr.span()
|
||||
}
|
||||
|
||||
fn is_terminated(&self) -> bool {
|
||||
@@ -646,7 +648,7 @@ impl IfAttributeValue {
|
||||
match stmt {
|
||||
syn::Stmt::Expr(exp, None) => {
|
||||
// Try parsing the statement as an IfmtInput by passing it through tokens
|
||||
let value: Result<HotLiteral, syn::Error> = syn::parse2(quote! { #exp });
|
||||
let value: Result<HotLiteral, syn::Error> = syn::parse2(exp.to_token_stream());
|
||||
Ok(match value {
|
||||
Ok(res) => Box::new(AttributeValue::AttrLiteral(res)),
|
||||
Err(_) => Box::new(AttributeValue::AttrExpr(PartialExpr::from_expr(exp))),
|
||||
@@ -663,9 +665,10 @@ impl IfAttributeValue {
|
||||
contains_expression: bool,
|
||||
) {
|
||||
let IfAttributeValue {
|
||||
condition,
|
||||
if_expr,
|
||||
then_value,
|
||||
else_value,
|
||||
..
|
||||
} = self;
|
||||
|
||||
// Quote an attribute value and convert the value to a string if it is formatted
|
||||
@@ -712,21 +715,20 @@ impl IfAttributeValue {
|
||||
Some(other) => {
|
||||
let other = quote_attribute_value_string(other, contains_expression);
|
||||
if terminated {
|
||||
quote! { #other }
|
||||
other
|
||||
} else {
|
||||
quote! { Some(#other) }
|
||||
quote_spanned! { other.span() => Some(#other) }
|
||||
}
|
||||
}
|
||||
None => quote! { None },
|
||||
};
|
||||
|
||||
tokens.append_all(quote! {
|
||||
{
|
||||
if #condition {
|
||||
#then_value
|
||||
} else {
|
||||
#else_value
|
||||
}
|
||||
let condition = &if_expr.cond;
|
||||
tokens.append_all(quote_spanned! { if_expr.span()=>
|
||||
if #condition {
|
||||
#then_value
|
||||
} else {
|
||||
#else_value
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -756,7 +758,7 @@ impl Parse for IfAttributeValue {
|
||||
// If it is a block, then the else is terminated
|
||||
Expr::Block(block) => Self::parse_attribute_value_from_block(&block.block)?,
|
||||
// Otherwise try to parse it as an if expression
|
||||
_ => Box::new(syn::parse2(quote! { #else_branch })?),
|
||||
_ => Box::new(syn::parse2(else_branch.to_token_stream())?),
|
||||
};
|
||||
Some(attribute_value)
|
||||
}
|
||||
@@ -764,7 +766,8 @@ impl Parse for IfAttributeValue {
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
condition: *if_expr.cond,
|
||||
condition: *if_expr.cond.clone(),
|
||||
if_expr,
|
||||
then_value,
|
||||
else_value,
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user