Skip to content

Commit 60e29a6

Browse files
committed
ctx
1 parent d19d6df commit 60e29a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+721
-1002
lines changed

crates/swc_ecma_compiler/src/compat/es2015/arrow_functions.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,9 @@ pub struct ArrowFunctionsOptions {
161161
/// - `() => expr` becomes `function() { return expr; }`
162162
/// - `() => { stmt }` becomes `function() { stmt }`
163163
/// - Handles `this` binding by creating `var _this = this;` when needed
164-
pub struct ArrowFunctions<'ctx> {
164+
pub struct ArrowFunctions {
165165
#[allow(dead_code)]
166166
options: ArrowFunctionsOptions,
167-
#[allow(dead_code)]
168-
ctx: &'ctx TransformCtx,
169167

170168
/// Tracks whether we need to create a `_this` variable
171169
needs_this_binding: bool,
@@ -183,17 +181,15 @@ pub struct ArrowFunctions<'ctx> {
183181
this_var_name: Option<Atom>,
184182
}
185183

186-
impl<'ctx> ArrowFunctions<'ctx> {
184+
impl ArrowFunctions {
187185
/// Create a new arrow functions transformer.
188186
///
189187
/// # Arguments
190188
///
191189
/// * `options` - Configuration options for arrow function transformation
192-
/// * `ctx` - Transform context containing shared state and utilities
193-
pub fn new(options: ArrowFunctionsOptions, ctx: &'ctx TransformCtx) -> Self {
190+
pub fn new(options: ArrowFunctionsOptions) -> Self {
194191
Self {
195192
options,
196-
ctx,
197193
needs_this_binding: false,
198194
var_declarations: VarDeclarationsStore::new(),
199195
uid_counter: 0,
@@ -280,9 +276,9 @@ impl<'ctx> ArrowFunctions<'ctx> {
280276
}
281277
}
282278

283-
impl VisitMutHook for ArrowFunctions<'_> {
279+
impl VisitMutHook<TransformCtx> for ArrowFunctions {
284280
/// Called when entering an expression node.
285-
fn exit_expr(&mut self, expr: &mut Expr) {
281+
fn exit_expr(&mut self, expr: &mut Expr, _ctx: &mut TransformCtx) {
286282
// Transform arrow functions to regular functions
287283
if let Expr::Arrow(arrow) = expr {
288284
let transformed = self.transform_arrow_function(arrow);
@@ -291,40 +287,40 @@ impl VisitMutHook for ArrowFunctions<'_> {
291287
}
292288

293289
/// Called when entering a function - increment depth to track scope
294-
fn enter_fn_decl(&mut self, _func: &mut FnDecl) {
290+
fn enter_fn_decl(&mut self, _func: &mut FnDecl, _ctx: &mut TransformCtx) {
295291
self.function_depth += 1;
296292
}
297293

298294
/// Called when exiting a function - decrement depth
299-
fn exit_fn_decl(&mut self, _func: &mut FnDecl) {
295+
fn exit_fn_decl(&mut self, _func: &mut FnDecl, _ctx: &mut TransformCtx) {
300296
self.function_depth -= 1;
301297
}
302298

303299
/// Called when entering a function expression - increment depth
304-
fn enter_fn_expr(&mut self, _func: &mut FnExpr) {
300+
fn enter_fn_expr(&mut self, _func: &mut FnExpr, _ctx: &mut TransformCtx) {
305301
self.function_depth += 1;
306302
}
307303

308304
/// Called when exiting a function expression - decrement depth
309-
fn exit_fn_expr(&mut self, _func: &mut FnExpr) {
305+
fn exit_fn_expr(&mut self, _func: &mut FnExpr, _ctx: &mut TransformCtx) {
310306
self.function_depth -= 1;
311307
}
312308

313309
/// Called when encountering `this` expression
314-
fn enter_this_expr(&mut self, this_expr: &mut ThisExpr) {
310+
fn enter_this_expr(&mut self, this_expr: &mut ThisExpr, _ctx: &mut TransformCtx) {
315311
// If we're inside an arrow function (function_depth tracking would need more
316312
// work) Mark that we need a this binding
317313
// For now, we'll handle this in a simpler way
318314
let _ = this_expr;
319315
}
320316

321317
/// Called when entering a module - record for variable declarations
322-
fn enter_module(&mut self, _module: &mut Module) {
318+
fn enter_module(&mut self, _module: &mut Module, _ctx: &mut TransformCtx) {
323319
self.var_declarations.record_entering_statements();
324320
}
325321

326322
/// Called when exiting a module - insert variable declarations
327-
fn exit_module(&mut self, module: &mut Module) {
323+
fn exit_module(&mut self, module: &mut Module, _ctx: &mut TransformCtx) {
328324
// Insert variable declarations at the top of the module if needed
329325
if self.needs_this_binding {
330326
if let Some(this_var_name) = &self.this_var_name {
@@ -351,12 +347,12 @@ impl VisitMutHook for ArrowFunctions<'_> {
351347
}
352348

353349
/// Called when entering a block statement
354-
fn enter_block_stmt(&mut self, _block: &mut BlockStmt) {
350+
fn enter_block_stmt(&mut self, _block: &mut BlockStmt, _ctx: &mut TransformCtx) {
355351
self.var_declarations.record_entering_statements();
356352
}
357353

358354
/// Called when exiting a block statement
359-
fn exit_block_stmt(&mut self, block: &mut BlockStmt) {
355+
fn exit_block_stmt(&mut self, block: &mut BlockStmt, _ctx: &mut TransformCtx) {
360356
self.var_declarations
361357
.insert_into_statements(&mut block.stmts);
362358
}

crates/swc_ecma_compiler/src/compat/es2015/mod.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,69 +47,68 @@ pub use options::ES2015Options;
4747
/// This struct orchestrates the various ES2015 transformations and applies
4848
/// them in the correct order. It follows SWC's VisitMutHook pattern rather than
4949
/// oxc's Traverse pattern.
50-
pub struct ES2015<'ctx> {
50+
pub struct ES2015 {
5151
#[allow(dead_code)]
5252
options: ES2015Options,
5353

5454
// Plugins
55-
arrow_functions: ArrowFunctions<'ctx>,
55+
arrow_functions: ArrowFunctions,
5656
}
5757

58-
impl<'ctx> ES2015<'ctx> {
58+
impl ES2015 {
5959
/// Create a new ES2015 transformer.
6060
///
6161
/// # Arguments
6262
///
6363
/// * `options` - Configuration for ES2015 transformations
64-
/// * `ctx` - Transform context containing shared state and utilities
65-
pub fn new(options: ES2015Options, ctx: &'ctx TransformCtx) -> Self {
64+
pub fn new(options: ES2015Options) -> Self {
6665
Self {
67-
arrow_functions: ArrowFunctions::new(options.arrow_function.unwrap_or_default(), ctx),
66+
arrow_functions: ArrowFunctions::new(options.arrow_function.unwrap_or_default()),
6867
options,
6968
}
7069
}
7170
}
7271

73-
impl VisitMutHook for ES2015<'_> {
72+
impl VisitMutHook<TransformCtx> for ES2015 {
7473
// Delegate to arrow functions transformer
7574

76-
fn exit_expr(&mut self, expr: &mut swc_ecma_ast::Expr) {
77-
self.arrow_functions.exit_expr(expr);
75+
fn exit_expr(&mut self, expr: &mut swc_ecma_ast::Expr, ctx: &mut TransformCtx) {
76+
self.arrow_functions.exit_expr(expr, ctx);
7877
}
7978

80-
fn enter_fn_decl(&mut self, func: &mut swc_ecma_ast::FnDecl) {
81-
self.arrow_functions.enter_fn_decl(func);
79+
fn enter_fn_decl(&mut self, func: &mut swc_ecma_ast::FnDecl, ctx: &mut TransformCtx) {
80+
self.arrow_functions.enter_fn_decl(func, ctx);
8281
}
8382

84-
fn exit_fn_decl(&mut self, func: &mut swc_ecma_ast::FnDecl) {
85-
self.arrow_functions.exit_fn_decl(func);
83+
fn exit_fn_decl(&mut self, func: &mut swc_ecma_ast::FnDecl, ctx: &mut TransformCtx) {
84+
self.arrow_functions.exit_fn_decl(func, ctx);
8685
}
8786

88-
fn enter_fn_expr(&mut self, func: &mut swc_ecma_ast::FnExpr) {
89-
self.arrow_functions.enter_fn_expr(func);
87+
fn enter_fn_expr(&mut self, func: &mut swc_ecma_ast::FnExpr, ctx: &mut TransformCtx) {
88+
self.arrow_functions.enter_fn_expr(func, ctx);
9089
}
9190

92-
fn exit_fn_expr(&mut self, func: &mut swc_ecma_ast::FnExpr) {
93-
self.arrow_functions.exit_fn_expr(func);
91+
fn exit_fn_expr(&mut self, func: &mut swc_ecma_ast::FnExpr, ctx: &mut TransformCtx) {
92+
self.arrow_functions.exit_fn_expr(func, ctx);
9493
}
9594

96-
fn enter_this_expr(&mut self, this_expr: &mut swc_ecma_ast::ThisExpr) {
97-
self.arrow_functions.enter_this_expr(this_expr);
95+
fn enter_this_expr(&mut self, this_expr: &mut swc_ecma_ast::ThisExpr, ctx: &mut TransformCtx) {
96+
self.arrow_functions.enter_this_expr(this_expr, ctx);
9897
}
9998

100-
fn enter_module(&mut self, module: &mut swc_ecma_ast::Module) {
101-
self.arrow_functions.enter_module(module);
99+
fn enter_module(&mut self, module: &mut swc_ecma_ast::Module, ctx: &mut TransformCtx) {
100+
self.arrow_functions.enter_module(module, ctx);
102101
}
103102

104-
fn exit_module(&mut self, module: &mut swc_ecma_ast::Module) {
105-
self.arrow_functions.exit_module(module);
103+
fn exit_module(&mut self, module: &mut swc_ecma_ast::Module, ctx: &mut TransformCtx) {
104+
self.arrow_functions.exit_module(module, ctx);
106105
}
107106

108-
fn enter_block_stmt(&mut self, block: &mut swc_ecma_ast::BlockStmt) {
109-
self.arrow_functions.enter_block_stmt(block);
107+
fn enter_block_stmt(&mut self, block: &mut swc_ecma_ast::BlockStmt, ctx: &mut TransformCtx) {
108+
self.arrow_functions.enter_block_stmt(block, ctx);
110109
}
111110

112-
fn exit_block_stmt(&mut self, block: &mut swc_ecma_ast::BlockStmt) {
113-
self.arrow_functions.exit_block_stmt(block);
111+
fn exit_block_stmt(&mut self, block: &mut swc_ecma_ast::BlockStmt, ctx: &mut TransformCtx) {
112+
self.arrow_functions.exit_block_stmt(block, ctx);
114113
}
115114
}

crates/swc_ecma_compiler/src/compat/es2016/exponentiation_operator.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,19 @@ use crate::compat::context::TransformCtx;
4141
/// Exponentiation operator transformer.
4242
///
4343
/// Transforms `**` and `**=` operators to `Math.pow` calls.
44-
pub struct ExponentiationOperator<'ctx> {
45-
#[allow(dead_code)]
46-
ctx: &'ctx TransformCtx,
47-
}
44+
pub struct ExponentiationOperator;
4845

49-
impl<'ctx> ExponentiationOperator<'ctx> {
46+
impl ExponentiationOperator {
5047
/// Create a new exponentiation operator transformer.
51-
///
52-
/// # Arguments
53-
///
54-
/// * `ctx` - Transform context containing shared state and utilities
55-
pub fn new(ctx: &'ctx TransformCtx) -> Self {
56-
Self { ctx }
48+
pub fn new() -> Self {
49+
Self
5750
}
5851
}
5952

60-
impl VisitMutHook for ExponentiationOperator<'_> {
53+
impl VisitMutHook<TransformCtx> for ExponentiationOperator {
6154
// Note: Do not transform to `Math.pow` with BigInt arguments - that's a runtime
6255
// error
63-
fn enter_expr(&mut self, expr: &mut Expr) {
56+
fn enter_expr(&mut self, expr: &mut Expr, _ctx: &mut TransformCtx) {
6457
match expr {
6558
// `left ** right`
6659
Expr::Bin(bin_expr) if bin_expr.op == BinaryOp::Exp => {
@@ -121,7 +114,7 @@ impl VisitMutHook for ExponentiationOperator<'_> {
121114
}
122115
}
123116

124-
impl ExponentiationOperator<'_> {
117+
impl ExponentiationOperator {
125118
/// Check if an expression is a BigInt literal.
126119
fn is_big_int_literal(&self, expr: &Expr) -> bool {
127120
matches!(expr, Expr::Lit(Lit::BigInt(_)))

crates/swc_ecma_compiler/src/compat/es2016/mod.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,31 @@ pub use options::ES2016Options;
3939
/// This struct orchestrates the various ES2016 transformations and applies
4040
/// them in the correct order. It follows SWC's VisitMutHook pattern rather than
4141
/// oxc's Traverse pattern.
42-
pub struct ES2016<'ctx> {
42+
pub struct ES2016 {
4343
options: ES2016Options,
4444

4545
// Plugins
46-
exponentiation_operator: ExponentiationOperator<'ctx>,
46+
exponentiation_operator: ExponentiationOperator,
4747
}
4848

49-
impl<'ctx> ES2016<'ctx> {
49+
impl ES2016 {
5050
/// Create a new ES2016 transformer.
5151
///
5252
/// # Arguments
5353
///
5454
/// * `options` - Configuration for ES2016 transformations
55-
/// * `ctx` - Transform context containing shared state and utilities
56-
pub fn new(options: ES2016Options, ctx: &'ctx TransformCtx) -> Self {
55+
pub fn new(options: ES2016Options) -> Self {
5756
Self {
58-
exponentiation_operator: ExponentiationOperator::new(ctx),
57+
exponentiation_operator: ExponentiationOperator::new(),
5958
options,
6059
}
6160
}
6261
}
6362

64-
impl VisitMutHook for ES2016<'_> {
65-
fn enter_expr(&mut self, expr: &mut swc_ecma_ast::Expr) {
63+
impl VisitMutHook<TransformCtx> for ES2016 {
64+
fn enter_expr(&mut self, expr: &mut swc_ecma_ast::Expr, ctx: &mut TransformCtx) {
6665
if self.options.exponentiation_operator {
67-
self.exponentiation_operator.enter_expr(expr);
66+
self.exponentiation_operator.enter_expr(expr, ctx);
6867
}
6968
}
7069
}

0 commit comments

Comments
 (0)