Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/module): handle this in class method #5065

Merged
merged 5 commits into from Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_module/src/amd.rs
Expand Up @@ -160,7 +160,7 @@ impl VisitMut for Amd {
stmts.visit_mut_children_with(&mut ModuleRefRewriter {
import_map,
lazy_record: Default::default(),
top_level: true,
is_global_this: true,
});

// ====================
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_module/src/common_js.rs
Expand Up @@ -136,7 +136,7 @@ impl VisitMut for Cjs {
stmts.visit_mut_children_with(&mut ModuleRefRewriter {
import_map,
lazy_record,
top_level: true,
is_global_this: true,
});

*n = stmts;
Expand Down
28 changes: 17 additions & 11 deletions crates/swc_ecma_transforms_module/src/module_ref_rewriter.rs
Expand Up @@ -35,7 +35,7 @@ pub(crate) struct ModuleRefRewriter {

pub lazy_record: AHashSet<Id>,

pub top_level: bool,
pub is_global_this: bool,
}

impl VisitMut for ModuleRefRewriter {
Expand Down Expand Up @@ -67,7 +67,7 @@ impl VisitMut for ModuleRefRewriter {
}

Expr::This(ThisExpr { span }) => {
if self.top_level {
if self.is_global_this {
*n = *undefined(*span);
}
}
Expand Down Expand Up @@ -101,36 +101,42 @@ impl VisitMut for ModuleRefRewriter {
fn visit_mut_function(&mut self, n: &mut Function) {
n.params.visit_mut_with(self);
magic-akari marked this conversation as resolved.
Show resolved Hide resolved

self.visit_mut_with_non_top_level(&mut n.body);
self.visit_mut_with_non_global_this(&mut n.body);
}

fn visit_mut_constructor(&mut self, n: &mut Constructor) {
n.params.visit_mut_with(self);

self.visit_mut_with_non_top_level(&mut n.body);
self.visit_mut_with_non_global_this(&mut n.body);
}

fn visit_mut_class_prop(&mut self, n: &mut ClassProp) {
n.key.visit_mut_with(self);

self.visit_mut_with_non_top_level(&mut n.value);
self.visit_mut_with_non_global_this(&mut n.value);
}

fn visit_mut_class_method(&mut self, n: &mut ClassMethod) {
n.key.visit_mut_with(self);

self.visit_mut_with_non_global_this(&mut n.function);
}

fn visit_mut_static_block(&mut self, n: &mut StaticBlock) {
self.visit_mut_with_non_top_level(&mut n.body);
self.visit_mut_with_non_global_this(n);
}
}

impl ModuleRefRewriter {
fn visit_mut_with_non_top_level<T>(&mut self, n: &mut T)
fn visit_mut_with_non_global_this<T>(&mut self, n: &mut T)
where
T: VisitMutWith<Self>,
{
let top_level = self.top_level;
let top_level = self.is_global_this;

self.top_level = false;
n.visit_mut_with(self);
self.top_level = top_level;
self.is_global_this = false;
n.visit_mut_children_with(self);
self.is_global_this = top_level;
}

fn map_module_ref_ident(&mut self, ref_ident: &Ident) -> Option<Expr> {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_module/src/umd.rs
Expand Up @@ -140,7 +140,7 @@ impl VisitMut for Umd {
stmts.visit_mut_children_with(&mut ModuleRefRewriter {
import_map,
lazy_record: Default::default(),
top_level: true,
is_global_this: true,
});

// ====================
Expand Down
@@ -0,0 +1,11 @@
class Foo {
bar = 5;
getThing(a, b = this.bar) {
return a + b;
}

static baz = 6;
static foo(a, b = this.baz) {
return a + b;
}
}
@@ -0,0 +1,15 @@
define([
"require"
], function(require) {
"use strict";
class Foo {
bar = 5;
getThing(a, b = this.bar) {
return a + b;
}
static baz = 6;
static foo(a, b = this.baz) {
return a + b;
}
}
});
@@ -0,0 +1,11 @@
"use strict";
class Foo {
bar = 5;
getThing(a, b = this.bar) {
return a + b;
}
static baz = 6;
static foo(a, b = this.baz) {
return a + b;
}
}
@@ -0,0 +1,17 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
class Foo {
bar = 5;
getThing(a, b = this.bar) {
return a + b;
}
static baz = 6;
static foo(a, b = this.baz) {
return a + b;
}
}
});