Skip to content

Commit 2bf316a

Browse files
fix(es/compat): Fix remaining post-order traversal issues in arrow transform
This commit addresses the remaining post-order traversal issues identified in the code review. Changes: - Fixed arrow function params: Now hoists `this`/`arguments` before converting nested arrows - Fixed getter_prop body: Now hoists `this`/`arguments` before converting nested arrows - Fixed setter_prop body: Now hoists `this`/`arguments` before converting nested arrows The fix ensures that in all cases, `this` and `arguments` references are replaced with hoisted variables BEFORE nested arrow functions are converted to regular functions. This prevents the issue where the hoister can't recurse into already-converted regular functions. Test results: - All 26 arrow function tests pass - Updated getter-setter snapshot to reflect correct hoisting behavior 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Donny/강동윤 <kdy1@users.noreply.github.com>
1 parent 8bd23ac commit 2bf316a

File tree

2 files changed

+22
-7
lines changed
  • crates
    • swc_ecma_compat_es2015/src
    • swc_ecma_transforms_compat/tests/arrow/getter-setter

2 files changed

+22
-7
lines changed

crates/swc_ecma_compat_es2015/src/arrow.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ impl VisitMut for Arrow {
122122
is_generator,
123123
..
124124
}) => {
125-
params.visit_mut_with(self);
125+
// First, hoist `this` references in params
126126
params.visit_mut_with(&mut self.hoister);
127+
// Then, convert nested arrow functions in params
128+
params.visit_mut_with(self);
127129

128130
let params: Vec<Param> = params
129131
.take()
@@ -196,6 +198,11 @@ impl VisitMut for Arrow {
196198
if let Some(body) = &mut f.body {
197199
let old_rep = self.hoister.take();
198200

201+
// First, replace `this` with `_this` in the body
202+
// This must happen before converting nested arrow functions
203+
body.visit_mut_with(&mut self.hoister);
204+
205+
// Then, convert nested arrow functions
199206
body.visit_mut_with(self);
200207

201208
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();
@@ -233,6 +240,11 @@ impl VisitMut for Arrow {
233240
if let Some(body) = &mut f.body {
234241
let old_rep = self.hoister.take();
235242

243+
// First, replace `this` with `_this` in the body
244+
// This must happen before converting nested arrow functions
245+
body.visit_mut_with(&mut self.hoister);
246+
247+
// Then, convert nested arrow functions
236248
body.visit_mut_with(self);
237249

238250
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();

crates/swc_ecma_transforms_compat/tests/arrow/getter-setter/output.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ var _this = this;
22
const a1 = function() {
33
return {
44
get this () {
5-
this;
6-
arguments;
5+
var _this1 = this, _arguments = arguments;
6+
_this1;
7+
_arguments;
78
},
89
set arguments (a = this){
9-
this;
10-
arguments;
10+
var _this2 = this, _arguments1 = arguments;
11+
_this2;
12+
_arguments1;
1113
},
1214
get [_this] () {
13-
this;
14-
arguments;
15+
var _this3 = this, _arguments2 = arguments;
16+
_this3;
17+
_arguments2;
1518
}
1619
};
1720
};

0 commit comments

Comments
 (0)