@@ -9,7 +9,7 @@ use swc_core::ecma::{
99 ast:: {
1010 ArrowExpr , AssignExpr , AssignTarget , BlockStmt , ClassDecl , ClassExpr , ClassMethod ,
1111 Constructor , Expr , FnDecl , FnExpr , Ident , Lit , MemberProp , MethodProp , Module , ModuleItem ,
12- Pat , PropName , Script , SimpleAssignTarget , Stmt , Str , VarDecl ,
12+ Param , Pat , PropName , Script , SimpleAssignTarget , Stmt , Str , VarDecl ,
1313 } ,
1414 atoms:: Atom ,
1515} ;
@@ -60,9 +60,9 @@ impl Instrumentation {
6060 self . has_injected = false ;
6161 }
6262
63- fn new_fn ( & self , body : BlockStmt ) -> ArrowExpr {
63+ fn new_fn ( & self , body : BlockStmt , params : Vec < Pat > ) -> ArrowExpr {
6464 ArrowExpr {
65- params : vec ! [ ] ,
65+ params,
6666 body : Box :: new ( body. into ( ) ) ,
6767 is_async : self . config . function_query . kind ( ) . is_async ( ) ,
6868 is_generator : false ,
@@ -92,7 +92,7 @@ impl Instrumentation {
9292 define_channel
9393 }
9494
95- fn insert_tracing ( & mut self , body : & mut BlockStmt ) {
95+ fn insert_tracing ( & mut self , body : & mut BlockStmt , params : & [ Param ] ) {
9696 self . count += 1 ;
9797
9898 let original_stmts = std:: mem:: take ( & mut body. stmts ) ;
@@ -104,7 +104,20 @@ impl Instrumentation {
104104 ..body. clone ( )
105105 } ;
106106
107- let traced_fn = self . new_fn ( original_body) ;
107+ let original_params: Vec < Pat > = params. iter ( ) . map ( |p| p. pat . clone ( ) ) . collect ( ) ;
108+
109+ let wrapped_fn = self . new_fn ( original_body, original_params) ;
110+
111+ let traced_body = BlockStmt {
112+ span : Span :: default ( ) ,
113+ ctxt : SyntaxContext :: empty ( ) ,
114+ stmts : vec ! [
115+ quote!( "const __apm$wrapped = $wrapped;" as Stmt , wrapped: Expr = wrapped_fn. into( ) ) ,
116+ quote!( "return __apm$wrapped.apply(null, __apm$original_args);" as Stmt ) ,
117+ ] ,
118+ } ;
119+
120+ let traced_fn = self . new_fn ( traced_body, vec ! [ ] ) ;
108121
109122 let id_name = self . config . get_identifier_name ( ) ;
110123 let ch_ident = ident ! ( format!( "tr_ch_apm${}" , & id_name) ) ;
@@ -115,6 +128,7 @@ impl Instrumentation {
115128 ) ) ;
116129
117130 body. stmts = vec ! [
131+ quote!( "const __apm$original_args = arguments" as Stmt ) ,
118132 quote!( "const __apm$traced = $traced;" as Stmt , traced: Expr = traced_fn. into( ) ) ,
119133 quote!(
120134 "if (!$ch.hasSubscribers) return __apm$traced();" as Stmt ,
@@ -185,7 +199,7 @@ impl Instrumentation {
185199 && func_expr. function . body . is_some ( )
186200 {
187201 if let Some ( body) = func_expr. function . body . as_mut ( ) {
188- self . insert_tracing ( body) ;
202+ self . insert_tracing ( body, & func_expr . function . params ) ;
189203 }
190204 true
191205 } else {
@@ -223,7 +237,7 @@ impl Instrumentation {
223237 && node. function . body . is_some ( )
224238 {
225239 if let Some ( body) = node. function . body . as_mut ( ) {
226- self . insert_tracing ( body) ;
240+ self . insert_tracing ( body, & node . function . params ) ;
227241 }
228242 }
229243 true
@@ -279,7 +293,7 @@ impl Instrumentation {
279293 && node. function . body . is_some ( )
280294 {
281295 if let Some ( body) = node. function . body . as_mut ( ) {
282- self . insert_tracing ( body) ;
296+ self . insert_tracing ( body, & node . function . params ) ;
283297 }
284298 }
285299 true
@@ -312,7 +326,7 @@ impl Instrumentation {
312326 && node. function . body . is_some ( )
313327 {
314328 if let Some ( body) = node. function . body . as_mut ( ) {
315- self . insert_tracing ( body) ;
329+ self . insert_tracing ( body, & node . function . params ) ;
316330 }
317331 }
318332 false
0 commit comments