diff --git a/crates/swc/tests/fixture/issues-7xxx/7241/input/.swcrc b/crates/swc/tests/fixture/issues-7xxx/7241/input/.swcrc new file mode 100644 index 00000000000..ee83abeb432 --- /dev/null +++ b/crates/swc/tests/fixture/issues-7xxx/7241/input/.swcrc @@ -0,0 +1,18 @@ +{ + "jsc": { + "parser": { + "syntax": "ecmascript", + "jsx": false + }, + "target": "es2022", + "loose": false, + "minify": { + "compress": true, + "mangle": true + } + }, + "module": { + "type": "es6" + }, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-7xxx/7241/input/index.js b/crates/swc/tests/fixture/issues-7xxx/7241/input/index.js new file mode 100644 index 00000000000..f8b404b9315 --- /dev/null +++ b/crates/swc/tests/fixture/issues-7xxx/7241/input/index.js @@ -0,0 +1,18 @@ +(function () { + function forwardRef() { + return something(); + } + + function Test() { + return 'Test'; + } + const _Test = /*#__PURE__*/ (0, forwardRef)(Test); + function Other() { + return 'Other'; + } + const _Other = /*#__PURE__*/ (0, forwardRef)(Other); + + + console.log((0, _Test)); + +})(); \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-7xxx/7241/output/index.js b/crates/swc/tests/fixture/issues-7xxx/7241/output/index.js new file mode 100644 index 00000000000..25ef426dc60 --- /dev/null +++ b/crates/swc/tests/fixture/issues-7xxx/7241/output/index.js @@ -0,0 +1,4 @@ +!function() { + let o = something(); + console.log(o); +}(); diff --git a/crates/swc_ecma_minifier/src/metadata/mod.rs b/crates/swc_ecma_minifier/src/metadata/mod.rs index 0ec68f5a5aa..de4541a5112 100644 --- a/crates/swc_ecma_minifier/src/metadata/mod.rs +++ b/crates/swc_ecma_minifier/src/metadata/mod.rs @@ -132,7 +132,17 @@ impl VisitMut for InfoMarker<'_> { n.span = n.span.apply_mark(self.marks.noinline); } - if self.has_pure(n.span) { + // We check callee in some cases because we move comments + // See https://github.com/swc-project/swc/issues/7241 + if self.has_pure(n.span) + || match &n.callee { + Callee::Expr(e) => match &**e { + Expr::Seq(callee) => self.has_pure(callee.span), + _ => false, + }, + _ => false, + } + { n.span = n.span.apply_mark(self.marks.pure); } else if let Some(pure_fns) = &self.pure_funcs { if let Callee::Expr(e) = &n.callee { @@ -146,14 +156,6 @@ impl VisitMut for InfoMarker<'_> { } } - fn visit_mut_new_expr(&mut self, n: &mut NewExpr) { - n.visit_mut_children_with(self); - - if self.has_pure(n.span) { - n.span = n.span.apply_mark(self.marks.pure); - } - } - fn visit_mut_export_default_decl(&mut self, e: &mut ExportDefaultDecl) { self.state.is_in_export = true; e.visit_mut_children_with(self); @@ -199,7 +201,7 @@ impl VisitMut for InfoMarker<'_> { fn visit_mut_lit(&mut self, _: &mut Lit) {} - fn visit_mut_script(&mut self, n: &mut Script) { + fn visit_mut_module(&mut self, n: &mut Module) { n.visit_mut_children_with(self); if self.state.is_bundle { @@ -210,7 +212,15 @@ impl VisitMut for InfoMarker<'_> { } } - fn visit_mut_module(&mut self, n: &mut Module) { + fn visit_mut_new_expr(&mut self, n: &mut NewExpr) { + n.visit_mut_children_with(self); + + if self.has_pure(n.span) { + n.span = n.span.apply_mark(self.marks.pure); + } + } + + fn visit_mut_script(&mut self, n: &mut Script) { n.visit_mut_children_with(self); if self.state.is_bundle { diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7241/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/7241/input.js new file mode 100644 index 00000000000..f8b404b9315 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7241/input.js @@ -0,0 +1,18 @@ +(function () { + function forwardRef() { + return something(); + } + + function Test() { + return 'Test'; + } + const _Test = /*#__PURE__*/ (0, forwardRef)(Test); + function Other() { + return 'Other'; + } + const _Other = /*#__PURE__*/ (0, forwardRef)(Other); + + + console.log((0, _Test)); + +})(); \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7241/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/7241/output.js new file mode 100644 index 00000000000..70ee78471ce --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7241/output.js @@ -0,0 +1,4 @@ +!function() { + const _Test = something(); + console.log(_Test); +}();