Make inacurate error spans capture the entire item

This commit is contained in:
Gustavo
2024-03-07 16:11:47 -03:00
parent 501e87c324
commit b8a1df92ff
5 changed files with 22 additions and 16 deletions
+2 -2
View File
@@ -116,7 +116,7 @@ fn format_variant(
quote!(format!("{{ \"{}\": \"{}\" }}", #tag, #name))
} else {
let ty = match (type_override, type_as) {
(Some(_), Some(_)) => syn_err!("`type` is not compatible with `as`"),
(Some(_), Some(_)) => syn_err_spanned!(variant; "`type` is not compatible with `as`"),
(Some(type_override), None) => quote! { #type_override },
(None, Some(type_as)) => {
quote!(<#type_as as ts_rs::TS>::name())
@@ -165,7 +165,7 @@ fn format_variant(
quote!(format!("{{ \"{}\": \"{}\" }}", #tag, #name))
} else {
let ty = match (type_override, type_as) {
(Some(_), Some(_)) => syn_err!("`type` is not compatible with `as`"),
(Some(_), Some(_)) => syn_err_spanned!(variant; "`type` is not compatible with `as`"),
(Some(type_override), None) => quote! { #type_override },
(None, Some(type_as)) => {
quote!(<#type_as as ts_rs::TS>::name())
+5 -5
View File
@@ -93,7 +93,7 @@ fn format_field(
}
if type_as.is_some() && type_override.is_some() {
syn_err!(field.ident.span(); "`type` is not compatible with `as`")
syn_err_spanned!(field; "`type` is not compatible with `as`")
}
let parsed_ty = if let Some(ref type_as) = type_as {
@@ -120,10 +120,10 @@ fn format_field(
if flatten {
match (&type_as, &type_override, &rename, inline) {
(Some(_), _, _, _) => syn_err!(field.ident.span(); "`as` is not compatible with `flatten`"),
(_, Some(_), _, _) => syn_err!(field.ident.span(); "`type` is not compatible with `flatten`"),
(_, _, Some(_), _) => syn_err!(field.ident.span(); "`rename` is not compatible with `flatten`"),
(_, _, _, true) => syn_err!(field.ident.span(); "`inline` is not compatible with `flatten`"),
(Some(_), _, _, _) => syn_err_spanned!(field; "`as` is not compatible with `flatten`"),
(_, Some(_), _, _) => syn_err_spanned!(field; "`type` is not compatible with `flatten`"),
(_, _, Some(_), _) => syn_err_spanned!(field; "`rename` is not compatible with `flatten`"),
(_, _, _, true) => syn_err_spanned!(field; "`inline` is not compatible with `flatten`"),
_ => {}
}
+4 -4
View File
@@ -32,15 +32,15 @@ pub(crate) fn newtype(
} = FieldAttr::from_attrs(&inner.attrs)?;
match (&rename_inner, skip, optional.optional, flatten) {
(Some(_), ..) => syn_err!("`rename` is not applicable to newtype fields"),
(Some(_), ..) => syn_err_spanned!(fields; "`rename` is not applicable to newtype fields"),
(_, true, ..) => return super::unit::null(attr, name, generics.clone()),
(_, _, true, ..) => syn_err!("`optional` is not applicable to newtype fields"),
(_, _, _, true) => syn_err!("`flatten` is not applicable to newtype fields"),
(_, _, true, ..) => syn_err_spanned!(fields; "`optional` is not applicable to newtype fields"),
(_, _, _, true) => syn_err_spanned!(fields; "`flatten` is not applicable to newtype fields"),
_ => {}
};
if type_as.is_some() && type_override.is_some() {
syn_err!("`type` is not compatible with `as`")
syn_err_spanned!(fields; "`type` is not compatible with `as`")
}
let inner_ty = if let Some(ref type_as) = type_as {
+5 -5
View File
@@ -1,6 +1,6 @@
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{Field, FieldsUnnamed, Generics, Result, Type, spanned::Spanned};
use syn::{Field, FieldsUnnamed, Generics, Result, Type};
use crate::{
attr::{FieldAttr, StructAttr},
@@ -72,19 +72,19 @@ fn format_field(
};
if type_as.is_some() && type_override.is_some() {
syn_err!(field.ty.span(); "`type` is not compatible with `as`")
syn_err_spanned!(field; "`type` is not compatible with `as`")
}
if rename.is_some() {
syn_err!(field.ty.span(); "`rename` is not applicable to tuple structs")
syn_err_spanned!(field; "`rename` is not applicable to tuple structs")
}
if optional.optional {
syn_err!(field.ty.span(); "`optional` is not applicable to tuple fields")
syn_err_spanned!(field; "`optional` is not applicable to tuple fields")
}
if flatten {
syn_err!(field.ty.span(); "`flatten` is not applicable to tuple fields")
syn_err_spanned!(field; "`flatten` is not applicable to tuple fields")
}
formatted_fields.push(match type_override {
+6
View File
@@ -17,6 +17,12 @@ macro_rules! syn_err {
};
}
macro_rules! syn_err_spanned {
($s:expr; $l:literal $(, $a:expr)*) => {
return Err(syn::Error::new_spanned($s, format!($l $(, $a)*)))
};
}
macro_rules! impl_parse {
($i:ident ($input:ident, $out:ident) { $($k:pat => $e:expr),* $(,)? }) => {
impl std::convert::TryFrom<&syn::Attribute> for $i {