Skip to content

Fix appending attributes instead of inserting them #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,26 @@ fn strike_through_attributes(
true
}
});
dec_attrs.extend_from_slice(&strike_attrs[..]);

let (mut derive_attrs, rest_attrs) =
strike_attrs
.iter()
.fold((Vec::new(), Vec::new()), |(mut der, mut rest), i| {
if matches!(&i.path[0], TokenTree::Ident(token) if token == "derive") {
der.push(i.clone());
} else {
rest.push(i.clone());
}
(der, rest)
});

// place other attrs after the existing ones
dec_attrs.extend_from_slice(&rest_attrs[..]);

// insert derive attrs before attributes
derive_attrs
.drain(..)
.for_each(|attr| dec_attrs.insert(0, attr));
}

fn recurse_through_type_list(
Expand Down
9 changes: 7 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn check(nested: proc_macro2::TokenStream, planexpected: proc_macro2::TokenStrea
#[test]
fn strikethrough_derive() {
let from = quote! {
#[strikethrough[striked_attr]]
#[strikethrough[derive(Debug, Default, PartialEq)]]
#[gubbel]
struct Parent {
Expand All @@ -22,19 +23,23 @@ fn strikethrough_derive() {
e: u32,
}
};

let out = quote! {
#[derive(Debug, Default, PartialEq)]
#[striked_attr]
struct Shared {
d: i32
}
#[gobbel]
#[derive(Debug, Default, PartialEq)]
#[gobbel]
#[striked_attr]
struct A {
b: Shared,
c: Shared,
}
#[gubbel]
#[derive(Debug, Default, PartialEq)]
#[gubbel]
#[striked_attr]
struct Parent {
a: A,
e: u32,
Expand Down