Skip to content

Commit

Permalink
fix(es/bugfixes): Fix fn transform in nameless fns (#8796)
Browse files Browse the repository at this point in the history
**Description:**

This pull request fixes
`bugfix/transform-safari-id-destructuring-collision-in-function-expression`
module. Previously the transform ignored code inside of function which
did not have specified identifier. Now the visitor should go through the
content of the nameless functions.

**Related issue:**

 - Closes #8788
  • Loading branch information
AlfonzAlfonz authored Apr 2, 2024
1 parent 56e03a1 commit 7ad004e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ impl VisitMut for SafariIdDestructuringCollisionInFunctionExpression {
}

fn visit_mut_fn_expr(&mut self, n: &mut FnExpr) {
let old_in_body = self.in_body;
if let Some(ident) = &n.ident {
let old_span = self.destructured_id_span.take();
let old_fn_expr_name = self.fn_expr_name.clone();
let old_in_body = self.in_body;

self.fn_expr_name = ident.sym.clone();
self.in_body = false;
Expand All @@ -71,10 +71,15 @@ impl VisitMut for SafariIdDestructuringCollisionInFunctionExpression {
n.function.visit_mut_children_with(&mut rename(&rename_map));
}

self.in_body = old_in_body;
self.fn_expr_name = old_fn_expr_name;
self.destructured_id_span = old_span;
} else {
self.in_body = false;
n.function.params.visit_mut_children_with(self);
self.in_body = true;
n.function.body.visit_mut_children_with(self);
}
self.in_body = old_in_body;
}

fn visit_mut_ident(&mut self, ident: &mut Ident) {
Expand Down Expand Up @@ -179,4 +184,46 @@ mod tests {
}
"
);

test!(
Syntax::default(),
|_| tr(),
in_nameless_fn,
"(function () {
(function a(a) {a});
});
"
);

test!(
Syntax::default(),
|_| tr(),
in_nameless_fn_multiple,
"// nameless iife
var x = function() {
// not transformed
var b = function a(a) {
return a;
};
}();
// nameless iife
var x = function x() {
var b = function a(_a) {
return _a;
};
}();
// nameless function
(function() {
// not transformed
var b = function a(a) {
return a;
};
});
// named function
(function x() {
var b = function a(_a) {
return _a;
};
});"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(function () {
(function a(_a) {
_a;
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// nameless iife
var x = function() {
// not transformed
var b = function a(_a) {
return _a;
};
}();
// nameless iife
var x = function x() {
var b = function a(_a) {
return _a;
};
}();
// nameless function
(function() {
// not transformed
var b = function a(_a1) {
return _a1;
};
});
// named function
(function x() {
var b = function a(_a) {
return _a;
};
});

0 comments on commit 7ad004e

Please sign in to comment.