Skip to content

Commit

Permalink
derive macro error span improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburniske committed Jun 26, 2024
1 parent 5e0b7bc commit adb0bd6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
11 changes: 7 additions & 4 deletions variant-macro/src/class_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ pub fn class_impl(input: TokenStream) -> TokenStream {
.take_struct()
.expect("Expected struct fields");

let base_class = container.class.unwrap_or_default();
let base_class = container
.class
.as_ref()
.map(syn::LitStr::value)
.unwrap_or_default();

let merger = {
if let Some(merger) = container.merger {
Expand All @@ -49,11 +53,10 @@ pub fn class_impl(input: TokenStream) -> TokenStream {

let field_idents = fields
.iter()
.map(|field| field.ident.as_ref().unwrap().clone());
.map(|field| field.ident.as_ref().expect("struct field has ident"))
.collect::<Vec<_>>();

let builder_impl = {
let field_idents = field_idents.clone();

let builder_set_methods = fields.iter().map(|field| {
let TwClassField { ident, ty, .. } = field;
quote! {
Expand Down
4 changes: 2 additions & 2 deletions variant-macro/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct TwVariantContainer {
pub ident: syn::Ident,
pub data: ast::Data<TwVariantOption, ()>,
/// The base Tailwind class for the variant.
pub class: Option<String>,
pub class: Option<syn::LitStr>,
}

#[derive(Debug, FromVariant)]
Expand All @@ -26,7 +26,7 @@ pub struct TwVariantOption {
pub struct TwClassContainer {
pub ident: syn::Ident,
pub data: ast::Data<(), TwClassField>,
pub class: Option<String>,
pub class: Option<syn::LitStr>,
/// Defaults to using `tw_merge`.
pub merger: Option<IdentString>,
}
Expand Down
9 changes: 4 additions & 5 deletions variant-macro/src/variant_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub fn variant_impl(input: TokenStream) -> TokenStream {
.collect::<Vec<_>>();

if defaults.is_empty() {
return syn::Error::new_spanned(
input,
return syn::Error::new(
enum_ident.span(),
"No default variant specified. Please mark one variant with `#[tw(default)]`",
)
.to_compile_error()
Expand All @@ -42,9 +42,8 @@ pub fn variant_impl(input: TokenStream) -> TokenStream {
.map(|v| v.ident.to_string())
.collect::<Vec<_>>()
);
return syn::Error::new_spanned(input, error)
.to_compile_error()
.into();
let span = defaults[1].default.span();
return syn::Error::new(span, error).to_compile_error().into();
}

let default_variant = defaults.into_iter().next().map(|v| {
Expand Down

0 comments on commit adb0bd6

Please sign in to comment.