mirror of
https://github.com/swc-project/swc.git
synced 2024-11-23 17:54:15 +03:00
fix(es/transforms): Fix detection of this
(#2634)
swc_ecma_utils: - `contains_this_expr`: Exclude `this` in object properties. swc_ecma_transforms_compat: - `arrow`: Exclude `this` in object properties. (https://github.com/vercel/next.js/issues/30592) - `parameters`: Exclude `this` in object properties.
This commit is contained in:
parent
1c5f75485f
commit
f4efd7ad92
@ -284,4 +284,21 @@ impl VisitMut for ThisReplacer<'_> {
|
||||
}
|
||||
|
||||
fn visit_mut_function(&mut self, _: &mut Function) {}
|
||||
|
||||
/// Don't recurse into fn
|
||||
fn visit_mut_getter_prop(&mut self, n: &mut GetterProp) {
|
||||
n.key.visit_mut_with(self);
|
||||
}
|
||||
|
||||
/// Don't recurse into fn
|
||||
fn visit_mut_method_prop(&mut self, n: &mut MethodProp) {
|
||||
n.key.visit_mut_with(self);
|
||||
n.function.visit_mut_with(self);
|
||||
}
|
||||
|
||||
/// Don't recurse into fn
|
||||
fn visit_mut_setter_prop(&mut self, n: &mut SetterProp) {
|
||||
n.key.visit_mut_with(self);
|
||||
n.param.visit_mut_with(self);
|
||||
}
|
||||
}
|
||||
|
@ -576,4 +576,21 @@ impl VisitMut for ThisReplacer<'_> {
|
||||
}
|
||||
|
||||
fn visit_mut_function(&mut self, _: &mut Function) {}
|
||||
|
||||
/// Don't recurse into fn
|
||||
fn visit_mut_getter_prop(&mut self, n: &mut GetterProp) {
|
||||
n.key.visit_mut_with(self);
|
||||
}
|
||||
|
||||
/// Don't recurse into fn
|
||||
fn visit_mut_method_prop(&mut self, n: &mut MethodProp) {
|
||||
n.key.visit_mut_with(self);
|
||||
n.function.visit_mut_with(self);
|
||||
}
|
||||
|
||||
/// Don't recurse into fn
|
||||
fn visit_mut_setter_prop(&mut self, n: &mut SetterProp) {
|
||||
n.key.visit_mut_with(self);
|
||||
n.param.visit_mut_with(self);
|
||||
}
|
||||
}
|
||||
|
@ -58,11 +58,34 @@ impl Visit for ThisVisitor {
|
||||
/// Don't recurse into fn
|
||||
fn visit_function(&mut self, _: &Function, _: &dyn Node) {}
|
||||
|
||||
/// Don't recurse into fn
|
||||
fn visit_getter_prop(&mut self, n: &GetterProp, _: &dyn Node) {
|
||||
n.key.visit_with(n, self);
|
||||
}
|
||||
|
||||
/// Don't recurse into fn
|
||||
fn visit_method_prop(&mut self, n: &MethodProp, _: &dyn Node) {
|
||||
n.key.visit_with(n, self);
|
||||
n.function.visit_with(n, self);
|
||||
}
|
||||
|
||||
/// Don't recurse into fn
|
||||
fn visit_setter_prop(&mut self, n: &SetterProp, _: &dyn Node) {
|
||||
n.key.visit_with(n, self);
|
||||
n.param.visit_with(n, self);
|
||||
}
|
||||
|
||||
fn visit_this_expr(&mut self, _: &ThisExpr, _: &dyn Node) {
|
||||
self.found = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// This does not recurse into a function if `this` is changed by it.
|
||||
///
|
||||
/// e.g.
|
||||
///
|
||||
/// - The body of an arrow expression is visited.
|
||||
/// - The body of a function expression is **not** visited.
|
||||
pub fn contains_this_expr<N>(body: &N) -> bool
|
||||
where
|
||||
N: VisitWith<ThisVisitor>,
|
||||
|
16
tests/vercel/loader-only/next-30592/1/input/index.js
Normal file
16
tests/vercel/loader-only/next-30592/1/input/index.js
Normal file
@ -0,0 +1,16 @@
|
||||
const navigation = (path) => [
|
||||
{
|
||||
name: "Home",
|
||||
href: `/`,
|
||||
get current() {
|
||||
return this.href === path;
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Dashboard",
|
||||
href: `/dashboard`,
|
||||
get current() {
|
||||
return this.href === path;
|
||||
},
|
||||
},
|
||||
];
|
18
tests/vercel/loader-only/next-30592/1/output/index.js
Normal file
18
tests/vercel/loader-only/next-30592/1/output/index.js
Normal file
@ -0,0 +1,18 @@
|
||||
var navigation = function(path) {
|
||||
return [
|
||||
{
|
||||
name: "Home",
|
||||
href: "/",
|
||||
get current () {
|
||||
return this.href === path;
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "Dashboard",
|
||||
href: "/dashboard",
|
||||
get current () {
|
||||
return this.href === path;
|
||||
}
|
||||
},
|
||||
];
|
||||
};
|
Loading…
Reference in New Issue
Block a user