swc_trace_macro/
lib.rs

1extern crate proc_macro;
2
3use quote::ToTokens;
4use syn::{parse_quote, AttrStyle, Attribute, ImplItem, ItemImpl};
5
6/// Utility proc macro to add `#[tracing::instrument(level = "info",
7/// skip_all)]` to all methods in an impl block.
8///
9/// This attribute macro is typically applied on an `VisitMut` impl block.
10/// If this is applied, all implemented methods will annotated with the
11/// instrument annotation from `tracing`.
12#[proc_macro_attribute]
13pub fn swc_trace(
14    _args: proc_macro::TokenStream,
15    input: proc_macro::TokenStream,
16) -> proc_macro::TokenStream {
17    let mut item = syn::parse::<ItemImpl>(input).expect("#[swc_trace] expects an impl block");
18
19    item.items.iter_mut().for_each(|item| {
20        // We only handle methods
21        if let ImplItem::Fn(m) = item {
22            // #[tracing::instrument(level = "info", skip_all)]
23            let attr = Attribute {
24                pound_token: Default::default(),
25                style: AttrStyle::Outer,
26                bracket_token: Default::default(),
27                meta: parse_quote!(tracing::instrument(level = "debug", skip_all)),
28            };
29            m.attrs.push(attr);
30        }
31    });
32
33    item.to_token_stream().into()
34}