feat(es/minifier): Support PURE comment of seq exprs (#7245)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/7241.
This commit is contained in:
Donny/강동윤 2023-04-11 13:48:11 +09:00 committed by GitHub
parent a0e193d177
commit 559d1202bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 11 deletions

View File

@ -0,0 +1,18 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": true,
"mangle": true
}
},
"module": {
"type": "es6"
},
"isModule": true
}

View File

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

View File

@ -0,0 +1,4 @@
!function() {
let o = something();
console.log(o);
}();

View File

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

View File

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

View File

@ -0,0 +1,4 @@
!function() {
const _Test = something();
console.log(_Test);
}();