Skip to content

Commit

Permalink
Remove this tracking for files with module syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcompiles committed Oct 20, 2023
1 parent 993fc0a commit 7103543
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
28 changes: 6 additions & 22 deletions packages/transformers/js/core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,11 @@ impl Visit for Collect {
return;
}
Expr::This(_this) => {
if self.is_module {
// Don't track this bindings if ESM syntax is present
return;
}

if self.in_module_this {
handle_export!();
} else if !self.in_class {
Expand Down Expand Up @@ -770,27 +775,6 @@ impl Visit for Collect {
self.used_imports.insert(id!(ident));
}
}
Expr::Bin(bin_expr) => {
if self.is_module & self.in_module_this {
// Some TSC polyfills use a pattern like below.
// We want to avoid marking these modules as CJS
// e.g. var _polyfill = (this && this.polyfill) || function () {}
if matches!(bin_expr.op, BinaryOp::LogicalAnd) && matches!(*bin_expr.left, Expr::This(..))
{
match &*bin_expr.right {
Expr::Member(member_expr) => {
if matches!(*member_expr.obj, Expr::This(..))
&& matches!(member_expr.prop, MemberProp::Ident(..))
{
return;
}
}
_ => {}
}
}
}
node.visit_children_with(self);
}
_ => {
node.visit_children_with(self);
}
Expand Down Expand Up @@ -823,7 +807,7 @@ impl Visit for Collect {
}

fn visit_this_expr(&mut self, node: &ThisExpr) {
if self.in_module_this {
if !self.is_module && self.in_module_this {
self.has_cjs_exports = true;
self.static_cjs_exports = false;
self.add_bailout(node.span, BailoutReason::FreeExports);
Expand Down
17 changes: 14 additions & 3 deletions packages/transformers/js/core/src/hoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,11 +1140,21 @@ mod tests {
);

let mut parser = Parser::new_from(lexer);
match parser.parse_module() {
Ok(module) => swc_core::common::GLOBALS.set(&Globals::new(), || {
match parser.parse_program() {
Ok(program) => swc_core::common::GLOBALS.set(&Globals::new(), || {
swc_core::ecma::transforms::base::helpers::HELPERS.set(
&swc_core::ecma::transforms::base::helpers::Helpers::new(false),
|| {
let is_module = program.is_module();
let module = match program {
Program::Module(module) => module,
Program::Script(script) => Module {
span: script.span,
shebang: None,
body: script.body.into_iter().map(ModuleItem::Stmt).collect(),
},
};

let unresolved_mark = Mark::fresh(Mark::root());
let global_mark = Mark::fresh(Mark::root());
let module = module.fold_with(&mut resolver(unresolved_mark, global_mark, false));
Expand All @@ -1155,7 +1165,7 @@ mod tests {
Mark::fresh(Mark::root()),
global_mark,
true,
true,
is_module,
);
module.visit_with(&mut collect);

Expand Down Expand Up @@ -1445,6 +1455,7 @@ mod tests {
// We want to avoid marking these modules as CJS
let (collect, _code, _hoist) = parse(
r#"
import 'something';
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function () {}
"#,
);
Expand Down

0 comments on commit 7103543

Please sign in to comment.