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:
Donny/강동윤 2021-11-03 13:53:23 +09:00 committed by GitHub
parent 1c5f75485f
commit f4efd7ad92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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>,

View 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;
},
},
];

View 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;
}
},
];
};