feat(macros): Add #[swc_trace] (#3738)

This commit is contained in:
Donny/강동윤 2022-02-25 23:01:33 +09:00 committed by GitHub
parent 17e77da5c6
commit b4830b61dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 6 deletions

10
Cargo.lock generated
View File

@ -3189,6 +3189,7 @@ dependencies = [
"swc_ecma_transforms_testing",
"swc_ecma_utils",
"swc_ecma_visit",
"swc_trace_macro",
"testing",
"tracing",
]
@ -3579,6 +3580,15 @@ dependencies = [
"tracing",
]
[[package]]
name = "swc_trace_macro"
version = "0.1.0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "swc_visit"
version = "0.3.0"

View File

@ -34,6 +34,7 @@ swc_ecma_transforms_classes = {version = "0.50.0", path = "../swc_ecma_transform
swc_ecma_transforms_macros = {version = "0.3.0", path = "../swc_ecma_transforms_macros"}
swc_ecma_utils = {version = "0.68.0", path = "../swc_ecma_utils"}
swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit"}
swc_trace_macro = {versio = "0.1.0", path = "../swc_trace_macro"}
tracing = "0.1.31"
[dev-dependencies]

View File

@ -7,6 +7,7 @@ use swc_ecma_utils::{
prepend,
};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, InjectVars, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;
/// Compile ES2015 arrow functions to ES5
///
@ -67,10 +68,10 @@ struct Arrow {
hoister: FnEnvHoister,
}
#[swc_trace]
impl VisitMut for Arrow {
noop_visit_mut_type!();
#[tracing::instrument(level = "trace", skip_all)]
fn visit_mut_class(&mut self, c: &mut Class) {
if c.super_class.is_some() {
self.in_subclass = true;
@ -79,7 +80,6 @@ impl VisitMut for Arrow {
self.in_subclass = false;
}
#[tracing::instrument(level = "trace", skip_all)]
fn visit_mut_constructor(&mut self, c: &mut Constructor) {
c.params.visit_mut_children_with(self);
@ -108,7 +108,6 @@ impl VisitMut for Arrow {
}
}
#[tracing::instrument(level = "trace", skip_all)]
fn visit_mut_expr(&mut self, expr: &mut Expr) {
match expr {
Expr::Arrow(ArrowExpr {
@ -167,12 +166,10 @@ impl VisitMut for Arrow {
}
}
#[tracing::instrument(level = "trace", skip_all)]
fn visit_mut_function(&mut self, f: &mut Function) {
f.visit_mut_children_with(self);
}
#[tracing::instrument(level = "trace", skip_all)]
fn visit_mut_module_items(&mut self, stmts: &mut Vec<ModuleItem>) {
stmts.visit_mut_children_with(self);
@ -183,7 +180,6 @@ impl VisitMut for Arrow {
}
}
#[tracing::instrument(level = "trace", skip_all)]
fn visit_mut_stmts(&mut self, stmts: &mut Vec<Stmt>) {
let old_rep = self.hoister.take();

View File

@ -0,0 +1,17 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Proc macro for performance trace of swc"
documentation = "https://rustdoc.swc.rs/swc_trace_macro/"
edition = "2021"
license = "Apache-2.0"
name = "swc_trace_macro"
repository = "https://github.com/swc-project/swc.git"
version = "0.1.0"
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = {version = "1", features = ["full"]}

View File

@ -0,0 +1,35 @@
extern crate proc_macro;
use quote::ToTokens;
use syn::{parse_quote, AttrStyle, Attribute, ImplItem, ItemImpl};
/// Utility proc macro to add `#[tracing::instrument(level = "trace",
/// skip_all)]` to all methods in an impl block.
///
/// This attribute macro is typically applied on an `VisitMut` impl block.
/// If this is applied, all implemented methods will annotated with the
/// instrument annotation from `tracing`.
#[proc_macro_attribute]
pub fn swc_trace(
_args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let mut item = syn::parse::<ItemImpl>(input).expect("#[swc_trace] expects an impl block");
item.items.iter_mut().for_each(|item| {
// We only handle methods
if let ImplItem::Method(m) = item {
// #[tracing::instrument(level = "trace", skip_all)]
let attr = Attribute {
pound_token: Default::default(),
style: AttrStyle::Outer,
bracket_token: Default::default(),
path: parse_quote!(tracing::instrument),
tokens: parse_quote!((level = "trace", skip_all)),
};
m.attrs.push(attr);
}
});
item.to_token_stream().into()
}