@@ -158,10 +158,26 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> {
158
158
/// ```
159
159
fn exit_static_block ( & mut self , block : & mut StaticBlock < ' a > , ctx : & mut TraverseCtx < ' a > ) {
160
160
let scope_id = block. scope_id ( ) ;
161
- if let Some ( replacement ) =
162
- self . transform_statements ( & mut block. body , None , scope_id , scope_id, ctx)
161
+ if let Some ( ( new_stmts , needs_await , using_ctx ) ) =
162
+ self . transform_statements ( & mut block. body , scope_id, ctx)
163
163
{
164
- block. body = ctx. ast . vec1 ( replacement) ;
164
+ let static_block_new_scope_id = ctx. insert_scope_between (
165
+ ctx. scoping ( ) . scope_parent_id ( scope_id) . unwrap ( ) ,
166
+ scope_id,
167
+ ScopeFlags :: ClassStaticBlock ,
168
+ ) ;
169
+
170
+ ctx. scoping_mut ( ) . set_symbol_scope_id ( using_ctx. symbol_id , static_block_new_scope_id) ;
171
+ ctx. scoping_mut ( ) . move_binding ( scope_id, static_block_new_scope_id, & using_ctx. name ) ;
172
+ * ctx. scoping_mut ( ) . scope_flags_mut ( scope_id) = ScopeFlags :: StrictMode ;
173
+
174
+ block. set_scope_id ( static_block_new_scope_id) ;
175
+ block. body = ctx. ast . vec1 ( Self :: create_try_stmt (
176
+ ctx. ast . block_statement_with_scope_id ( SPAN , new_stmts, scope_id) ,
177
+ Self :: create_catch_clause ( & using_ctx, static_block_new_scope_id, ctx) ,
178
+ Self :: create_finally_block ( & using_ctx, static_block_new_scope_id, needs_await, ctx) ,
179
+ ctx,
180
+ ) ) ;
165
181
}
166
182
}
167
183
@@ -186,14 +202,22 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> {
186
202
/// }
187
203
/// ```
188
204
fn enter_function_body ( & mut self , body : & mut FunctionBody < ' a > , ctx : & mut TraverseCtx < ' a > ) {
189
- if let Some ( replacement) = self . transform_statements (
190
- & mut body. statements ,
191
- None ,
192
- ctx. current_hoist_scope_id ( ) ,
193
- ctx. current_hoist_scope_id ( ) ,
194
- ctx,
195
- ) {
196
- body. statements = ctx. ast . vec1 ( replacement) ;
205
+ if let Some ( ( new_stmts, needs_await, using_ctx) ) =
206
+ self . transform_statements ( & mut body. statements , ctx. current_hoist_scope_id ( ) , ctx)
207
+ {
208
+ // FIXME: this creates the scopes in the correct place, however we never move the bindings contained
209
+ // within `new_stmts` to the new scope.
210
+ let block_stmt_scope_id =
211
+ ctx. insert_scope_below_statements ( & new_stmts, ScopeFlags :: empty ( ) ) ;
212
+
213
+ let current_scope_id = ctx. current_scope_id ( ) ;
214
+
215
+ body. statements = ctx. ast . vec1 ( Self :: create_try_stmt (
216
+ ctx. ast . block_statement_with_scope_id ( SPAN , new_stmts, block_stmt_scope_id) ,
217
+ Self :: create_catch_clause ( & using_ctx, current_scope_id, ctx) ,
218
+ Self :: create_finally_block ( & using_ctx, current_scope_id, needs_await, ctx) ,
219
+ ctx,
220
+ ) ) ;
197
221
}
198
222
}
199
223
@@ -461,15 +485,17 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
461
485
fn transform_block_statement ( & mut self , stmt : & mut Statement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
462
486
let Statement :: BlockStatement ( block_stmt) = stmt else { unreachable ! ( ) } ;
463
487
464
- let scope_id = block_stmt. scope_id ( ) ;
465
- if let Some ( replacement) = self . transform_statements (
466
- & mut block_stmt. body ,
467
- Some ( scope_id) ,
468
- ctx. current_scope_id ( ) ,
469
- ctx. current_hoist_scope_id ( ) ,
470
- ctx,
471
- ) {
472
- * stmt = replacement;
488
+ if let Some ( ( new_stmts, needs_await, using_ctx) ) =
489
+ self . transform_statements ( & mut block_stmt. body , ctx. current_hoist_scope_id ( ) , ctx)
490
+ {
491
+ let current_scope_id = ctx. current_scope_id ( ) ;
492
+
493
+ * stmt = Self :: create_try_stmt (
494
+ ctx. ast . block_statement_with_scope_id ( SPAN , new_stmts, block_stmt. scope_id ( ) ) ,
495
+ Self :: create_catch_clause ( & using_ctx, current_scope_id, ctx) ,
496
+ Self :: create_finally_block ( & using_ctx, current_scope_id, needs_await, ctx) ,
497
+ ctx,
498
+ ) ;
473
499
}
474
500
}
475
501
@@ -596,8 +622,6 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
596
622
597
623
/// Transforms:
598
624
/// - `node` - the statements to transform
599
- /// - `scope_id` - if provided, it will be used as the scope_id for the new block.
600
- /// - `parent_scope_id` - the parent scope
601
625
/// - `hoist_scope_id` - the hoist scope, used for generating new var bindings
602
626
/// - `ctx` - the traverse context
603
627
///
@@ -624,11 +648,9 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
624
648
fn transform_statements (
625
649
& mut self ,
626
650
stmts : & mut ArenaVec < ' a , Statement < ' a > > ,
627
- scope_id : Option < ScopeId > ,
628
- parent_scope_id : ScopeId ,
629
651
hoist_scope_id : ScopeId ,
630
652
ctx : & mut TraverseCtx < ' a > ,
631
- ) -> Option < Statement < ' a > > {
653
+ ) -> Option < ( ArenaVec < ' a , Statement < ' a > > , bool , BoundIdentifier < ' a > ) > {
632
654
let mut needs_await = false ;
633
655
634
656
let mut using_ctx = None ;
@@ -700,21 +722,16 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
700
722
) ;
701
723
stmts. insert ( 0 , Statement :: from ( helper) ) ;
702
724
703
- let scope_id_children_to_move = scope_id. unwrap_or ( parent_scope_id) ;
704
-
705
- let scope_id = scope_id
706
- . unwrap_or_else ( || ctx. create_child_scope ( parent_scope_id, ScopeFlags :: empty ( ) ) ) ;
707
- let block = ctx. ast . block_statement_with_scope_id ( SPAN , stmts, scope_id) ;
708
-
709
- let child_ids = ctx. scoping_mut ( ) . get_scope_child_ids ( scope_id_children_to_move) . to_vec ( ) ;
710
- for id in child_ids. iter ( ) . filter ( |id| * id != & scope_id) {
711
- ctx. scoping_mut ( ) . change_scope_parent_id ( * id, Some ( scope_id) ) ;
712
- }
713
-
714
- let catch = Self :: create_catch_clause ( & using_ctx, parent_scope_id, ctx) ;
715
- let finally = Self :: create_finally_block ( & using_ctx, parent_scope_id, needs_await, ctx) ;
725
+ Some ( ( stmts, needs_await, using_ctx) )
726
+ }
716
727
717
- Some ( ctx. ast . statement_try ( SPAN , block, Some ( catch) , Some ( finally) ) )
728
+ fn create_try_stmt (
729
+ body : BlockStatement < ' a > ,
730
+ catch : ArenaBox < ' a , CatchClause < ' a > > ,
731
+ finally : ArenaBox < ' a , BlockStatement < ' a > > ,
732
+ ctx : & TraverseCtx < ' a > ,
733
+ ) -> Statement < ' a > {
734
+ ctx. ast . statement_try ( SPAN , body, Some ( catch) , Some ( finally) )
718
735
}
719
736
720
737
/// `catch (_) { _usingCtx.e = _; }`
0 commit comments