Skip to content

Commit adb0bd6

Browse files
committed
derive macro error span improvements
1 parent 5e0b7bc commit adb0bd6

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

variant-macro/src/class_macro.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ pub fn class_impl(input: TokenStream) -> TokenStream {
2222
.take_struct()
2323
.expect("Expected struct fields");
2424

25-
let base_class = container.class.unwrap_or_default();
25+
let base_class = container
26+
.class
27+
.as_ref()
28+
.map(syn::LitStr::value)
29+
.unwrap_or_default();
2630

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

5054
let field_idents = fields
5155
.iter()
52-
.map(|field| field.ident.as_ref().unwrap().clone());
56+
.map(|field| field.ident.as_ref().expect("struct field has ident"))
57+
.collect::<Vec<_>>();
5358

5459
let builder_impl = {
55-
let field_idents = field_idents.clone();
56-
5760
let builder_set_methods = fields.iter().map(|field| {
5861
let TwClassField { ident, ty, .. } = field;
5962
quote! {

variant-macro/src/model.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct TwVariantContainer {
1010
pub ident: syn::Ident,
1111
pub data: ast::Data<TwVariantOption, ()>,
1212
/// The base Tailwind class for the variant.
13-
pub class: Option<String>,
13+
pub class: Option<syn::LitStr>,
1414
}
1515

1616
#[derive(Debug, FromVariant)]
@@ -26,7 +26,7 @@ pub struct TwVariantOption {
2626
pub struct TwClassContainer {
2727
pub ident: syn::Ident,
2828
pub data: ast::Data<(), TwClassField>,
29-
pub class: Option<String>,
29+
pub class: Option<syn::LitStr>,
3030
/// Defaults to using `tw_merge`.
3131
pub merger: Option<IdentString>,
3232
}

variant-macro/src/variant_macro.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub fn variant_impl(input: TokenStream) -> TokenStream {
2626
.collect::<Vec<_>>();
2727

2828
if defaults.is_empty() {
29-
return syn::Error::new_spanned(
30-
input,
29+
return syn::Error::new(
30+
enum_ident.span(),
3131
"No default variant specified. Please mark one variant with `#[tw(default)]`",
3232
)
3333
.to_compile_error()
@@ -42,9 +42,8 @@ pub fn variant_impl(input: TokenStream) -> TokenStream {
4242
.map(|v| v.ident.to_string())
4343
.collect::<Vec<_>>()
4444
);
45-
return syn::Error::new_spanned(input, error)
46-
.to_compile_error()
47-
.into();
45+
let span = defaults[1].default.span();
46+
return syn::Error::new(span, error).to_compile_error().into();
4847
}
4948

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

0 commit comments

Comments
 (0)