1- use oxc_allocator:: Allocator ;
2- use oxc_ast:: ast:: MethodDefinition ;
1+ use oxc_allocator:: { Allocator , Box } ;
2+ use oxc_ast:: ast:: { FunctionBody , MethodDefinition } ;
33use oxc_codegen:: { Codegen , CodegenOptions } ;
44use oxc_parser:: { ParseOptions , Parser } ;
55use oxc_semantic:: SemanticBuilder ;
@@ -48,9 +48,6 @@ pub fn transform_code_str(code: &str, instructions_json: &str, src_type: i32) ->
4848 let t = & mut Transformer {
4949 allocator : & allocator,
5050 file_instructions : & file_instructions,
51- //current_function_identifier: None,
52- //modify_return_value: false,
53- //sub_function_counter: 0,
5451 } ;
5552
5653 traverse_mut ( t, & allocator, program, symbols, scopes) ;
@@ -77,18 +74,12 @@ pub fn transform_code_str(code: &str, instructions_json: &str, src_type: i32) ->
7774 } )
7875 . build ( & program) ;
7976
80- // Debug helper
81- //format!("{:?}", parser_result.program);
82-
8377 js. code
8478}
8579
8680struct Transformer < ' a > {
8781 allocator : & ' a Allocator ,
8882 file_instructions : & ' a FileInstructions ,
89- //current_function_identifier: Option<String>, // Only set if we want to instrument the current function
90- //sub_function_counter: i32, // Counter to keep track of how many sub functions we are in
91- //modify_return_value: bool,
9283}
9384
9485impl < ' a > Traverse < ' a > for Transformer < ' a > {
@@ -106,8 +97,6 @@ impl<'a> Traverse<'a> for Transformer<'a> {
10697 return ;
10798 }
10899
109- // Todo implement submethod counting for nested functions for supporting modifications of return value
110-
111100 let method_name = node. key . name ( ) . unwrap ( ) . to_string ( ) ;
112101
113102 let found_instruction = self
@@ -117,17 +106,11 @@ impl<'a> Traverse<'a> for Transformer<'a> {
117106 . find ( |f| f. node_type == "MethodDefinition" && f. name == method_name) ;
118107
119108 if found_instruction. is_none ( ) {
120- //self.current_function_identifier = None;
121- //self.modify_return_value = false;
122109 return ;
123110 }
124111
125112 let instruction = found_instruction. unwrap ( ) ;
126113
127- /*let function_identifier = format!("{}.{}", self.module_name, method_name);
128- self.current_function_identifier = Some(function_identifier.clone());
129- self.modify_return_value = instruction.modify_return_value;*/
130-
131114 // We need to collect the arg names before we make the body mutable
132115 let arg_names = if instruction. modify_args {
133116 // Todo check whats appens if rest parameter is used
@@ -149,13 +132,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
149132 arg_names_str, instruction. identifier, arg_names_str
150133 ) ) ;
151134
152- body. statements . insert (
153- 0 ,
154- parse_js_code_to_statements ( self . allocator , & source_text, SourceType :: mjs ( ) )
155- . into_iter ( )
156- . next ( )
157- . unwrap ( ) ,
158- ) ;
135+ insert_single_statement_into_func ( self . allocator , body, 0 , & source_text) ;
159136 }
160137
161138 if instruction. inspect_args {
@@ -165,52 +142,24 @@ impl<'a> Traverse<'a> for Transformer<'a> {
165142 instruction. identifier
166143 ) ) ;
167144
168- body. statements . insert (
169- 0 ,
170- parse_js_code_to_statements ( self . allocator , & source_text, SourceType :: mjs ( ) )
171- . into_iter ( )
172- . next ( )
173- . unwrap ( ) ,
174- ) ;
175- }
176- }
177-
178- /*fn exit_method_definition(
179- &mut self,
180- _node: &mut MethodDefinition<'a>,
181- ctx: &mut TraverseCtx<'a>,
182- ) {
183- if self.current_function_identifier.is_none() {
184- return;
185- }
186-
187- // If we are in a sub function, we need to decrease the counter
188- // If we leave the last sub function, we need to modify the return value again
189- if self.sub_function_counter > 0 {
190- self.sub_function_counter -= 1;
191-
192- if self.sub_function_counter == 0 {
193- self.modify_return_value = true;
194- }
195-
196- return;
145+ insert_single_statement_into_func ( self . allocator , body, 0 , source_text) ;
197146 }
198147
199- // We leave the current function we want to instrument
200- self.modify_return_value = false;
201- self.current_function_identifier = None;
148+ // Todo support return value modification
202149 }
150+ }
203151
204- fn enter_return_statement(
205- &mut self,
206- node: &mut oxc_ast::ast::ReturnStatement<'a>,
207- ctx: &mut TraverseCtx<'a>,
208- ) {
209- if !self.modify_return_value {
210- return;
211- }
212-
213- // Todo support modifying return value
214- //AstBuilder::new(self.allocator).return_statement(node.span, argument)
215- }*/
152+ fn insert_single_statement_into_func < ' a > (
153+ allocator : & ' a Allocator ,
154+ body : & mut Box < ' a , FunctionBody < ' a > > ,
155+ pos : usize ,
156+ source_text : & ' a str ,
157+ ) {
158+ body. statements . insert (
159+ pos,
160+ parse_js_code_to_statements ( & allocator, & source_text, SourceType :: mjs ( ) )
161+ . into_iter ( )
162+ . next ( )
163+ . unwrap ( ) ,
164+ ) ;
216165}
0 commit comments