diff --git a/crates/swc/tests/tsc-references/witness.2.minified.js b/crates/swc/tests/tsc-references/witness.2.minified.js index 91dc5211add..c722839afaa 100644 --- a/crates/swc/tests/tsc-references/witness.2.minified.js +++ b/crates/swc/tests/tsc-references/witness.2.minified.js @@ -3,6 +3,8 @@ import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; var co2, cnd1, or1, or2, or3, and1, and3, propAcc1, M2, varInit = varInit; !function fnReturn1() { return fnReturn1(); +}(), function fnReturn2() { + return fnReturn2; }(); var co2 = co2, cnd1 = cnd1 ? 0 : 1, or1 = or1 || "", or2 = or2, or3 = or3 || or3, and1 = and1 && "", and3 = and3 && and3; !function fnCall() { diff --git a/crates/swc_ecma_minifier/src/compress/pure/misc.rs b/crates/swc_ecma_minifier/src/compress/pure/misc.rs index e96369075d9..ee43b9ca371 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/misc.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/misc.rs @@ -4,7 +4,7 @@ use swc_atoms::js_word; use swc_common::{iter::IdentifyLast, util::take::Take, Span, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::{ - ExprExt, ExprFactory, Type, + ExprExt, ExprFactory, IdentUsageFinder, Type, Value::{self, Known}, }; @@ -1060,6 +1060,12 @@ impl Pure<'_> { match &mut **callee { Expr::Fn(callee) => { if let Some(body) = &mut callee.function.body { + if let Some(ident) = &callee.ident { + if IdentUsageFinder::find(&ident.to_id(), body) { + return; + } + } + for stmt in &mut body.stmts { self.ignore_return_value_of_return_stmt(stmt, opts); } diff --git a/crates/swc_ecma_minifier/tests/benches-full/echarts.js b/crates/swc_ecma_minifier/tests/benches-full/echarts.js index 18c57790480..5f8a8692409 100644 --- a/crates/swc_ecma_minifier/tests/benches-full/echarts.js +++ b/crates/swc_ecma_minifier/tests/benches-full/echarts.js @@ -13268,6 +13268,7 @@ var textGuideLineConfig = el.textGuideLineConfig; labelLine.z = z, labelLine.zlevel = zlevel, isFinite(maxZ2) && (labelLine.z2 = maxZ2 + (textGuideLineConfig && textGuideLineConfig.showAbove ? 1 : -1)); } + return maxZ2; }(view.group, model.get('z') || 0, model.get('zlevel') || 0, -1 / 0); } function clearStates(model, view) { diff --git a/crates/swc_ecma_minifier/tests/benches-full/react.js b/crates/swc_ecma_minifier/tests/benches-full/react.js index 3d583b9257e..35bb7619bcd 100644 --- a/crates/swc_ecma_minifier/tests/benches-full/react.js +++ b/crates/swc_ecma_minifier/tests/benches-full/react.js @@ -291,7 +291,7 @@ return c; }); } else null != mappedChild && (isValidElement(mappedChild) && (oldElement = mappedChild, newKey = escapedPrefix + (mappedChild.key && (!_child || _child.key !== mappedChild.key) ? escapeUserProvidedKey('' + mappedChild.key) + '/' : '') + childKey, mappedChild = ReactElement(oldElement.type, newKey, oldElement.ref, oldElement._self, oldElement._source, oldElement._owner, oldElement.props)), array.push(mappedChild)); - return; + return 1; } var subtreeCount = 0, nextNamePrefix = '' === nameSoFar ? '.' : nameSoFar + ':'; if (Array.isArray(children)) for(var i = 0; i < children.length; i++)nextName = nextNamePrefix + getElementKey(child = children[i], i), subtreeCount += mapIntoArray(child, array, escapedPrefix, nextName, callback); @@ -306,6 +306,7 @@ throw Error("Objects are not valid as a React child (found: " + ('[object Object]' === childrenString ? 'object with keys {' + Object.keys(children).join(', ') + '}' : childrenString) + "). If you meant to render a collection of children, use an array instead."); } } + return subtreeCount; }(children, result, '', '', function(child) { return func.call(context, child, count++); }), result; diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/2257/full/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/2257/full/output.js index dfa735e16f1..d1ee27c1ed7 100644 --- a/crates/swc_ecma_minifier/tests/fixture/issues/2257/full/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/issues/2257/full/output.js @@ -16507,7 +16507,7 @@ ref: a1.ref, props: a1.props, _owner: a1._owner - }), b.push(d)); + }), b.push(d)), 1; if (h = 0, e = "" === e ? "." : e + ":", Array.isArray(a)) for(var g = 0; g < a.length; g++){ k = a[g]; var f = e + N(k, g); @@ -16515,6 +16515,7 @@ } else if ("function" == typeof (f = null === (a2 = a) || "object" != typeof a2 ? null : "function" == typeof (a2 = x && a2[x] || a2["@@iterator"]) ? a2 : null)) for(a = f.call(a), g = 0; !(k = a.next()).done;)f = e + N(k = k.value, g++), h += O(k, b, c, f, d); else if ("object" === k) throw Error(z(31, "[object Object]" == (b = "" + a) ? "object with keys {" + Object.keys(a).join(", ") + "}" : b)); + return h; }(a, e, "", "", function(a) { return b.call(c, a, d++); }), e; diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6141/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/6141/input.js new file mode 100644 index 00000000000..5e562454cd9 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6141/input.js @@ -0,0 +1,14 @@ +(function foo(obj) { + if (obj) { + for (const key in obj) { + const element = obj[key]; + + if (element && foo(element.children)) { + // do something + } + } + + return true; + } + return false; +})() \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6141/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/6141/output.js new file mode 100644 index 00000000000..4c7803c5efc --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6141/output.js @@ -0,0 +1,10 @@ +!function foo(obj) { + if (obj) { + for(const key in obj){ + const element = obj[key]; + element && foo(element.children); + } + return !0; + } + return !1; +}(); diff --git a/crates/swc_ecma_minifier/tests/fixture/next/regression-1/framework-798bab57daac3897/output.js b/crates/swc_ecma_minifier/tests/fixture/next/regression-1/framework-798bab57daac3897/output.js index bf850116126..5a3ca4a335b 100644 --- a/crates/swc_ecma_minifier/tests/fixture/next/regression-1/framework-798bab57daac3897/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/next/regression-1/framework-798bab57daac3897/output.js @@ -5946,7 +5946,7 @@ ref: a1.ref, props: a1.props, _owner: a1._owner - }), b.push(c)); + }), b.push(c)), 1; if (h = 0, d = "" === d ? "." : d + ":", I(a)) for(var g = 0; g < a.length; g++){ k = a[g]; var f = d + Q(k, g); @@ -5954,6 +5954,7 @@ } else if ("function" == typeof (f = null === (a2 = a) || "object" != typeof a2 ? null : "function" == typeof (a2 = z && a2[z] || a2["@@iterator"]) ? a2 : null)) for(a = f.call(a), g = 0; !(k = a.next()).done;)f = d + Q(k = k.value, g++), h += R(k, b, e, f, c); else if ("object" === k) throw Error("Objects are not valid as a React child (found: " + ("[object Object]" === (b = String(a)) ? "object with keys {" + Object.keys(a).join(", ") + "}" : b) + "). If you meant to render a collection of children, use an array instead."); + return h; }(a, d, "", "", function(a) { return b.call(e, a, c++); }), d; diff --git a/crates/swc_ecma_minifier/tests/single-pass/9/1/input.js b/crates/swc_ecma_minifier/tests/pass-1/9/1/input.js similarity index 100% rename from crates/swc_ecma_minifier/tests/single-pass/9/1/input.js rename to crates/swc_ecma_minifier/tests/pass-1/9/1/input.js diff --git a/crates/swc_ecma_minifier/tests/single-pass/9/1/output.js b/crates/swc_ecma_minifier/tests/pass-1/9/1/output.js similarity index 100% rename from crates/swc_ecma_minifier/tests/single-pass/9/1/output.js rename to crates/swc_ecma_minifier/tests/pass-1/9/1/output.js diff --git a/crates/swc_ecma_minifier/tests/single-pass/9/2/input.js b/crates/swc_ecma_minifier/tests/pass-1/9/2/input.js similarity index 100% rename from crates/swc_ecma_minifier/tests/single-pass/9/2/input.js rename to crates/swc_ecma_minifier/tests/pass-1/9/2/input.js diff --git a/crates/swc_ecma_minifier/tests/single-pass/9/2/output.js b/crates/swc_ecma_minifier/tests/pass-1/9/2/output.js similarity index 100% rename from crates/swc_ecma_minifier/tests/single-pass/9/2/output.js rename to crates/swc_ecma_minifier/tests/pass-1/9/2/output.js diff --git a/crates/swc_ecma_minifier/tests/projects/output/react-17.0.1.js b/crates/swc_ecma_minifier/tests/projects/output/react-17.0.1.js index 9d8029de7ec..c7c316f8226 100644 --- a/crates/swc_ecma_minifier/tests/projects/output/react-17.0.1.js +++ b/crates/swc_ecma_minifier/tests/projects/output/react-17.0.1.js @@ -291,7 +291,7 @@ return c; }); } else null != mappedChild && (isValidElement(mappedChild) && (oldElement = mappedChild, newKey = escapedPrefix + (mappedChild.key && (!_child || _child.key !== mappedChild.key) ? escapeUserProvidedKey("" + mappedChild.key) + "/" : "") + childKey, mappedChild = ReactElement(oldElement.type, newKey, oldElement.ref, oldElement._self, oldElement._source, oldElement._owner, oldElement.props)), array.push(mappedChild)); - return; + return 1; } var subtreeCount = 0, nextNamePrefix = "" === nameSoFar ? "." : nameSoFar + ":"; if (Array.isArray(children)) for(var i = 0; i < children.length; i++)nextName = nextNamePrefix + getElementKey(child = children[i], i), subtreeCount += mapIntoArray(child, array, escapedPrefix, nextName, callback); @@ -306,6 +306,7 @@ throw Error("Objects are not valid as a React child (found: " + ("[object Object]" === childrenString ? "object with keys {" + Object.keys(children).join(", ") + "}" : childrenString) + "). If you meant to render a collection of children, use an array instead."); } } + return subtreeCount; }(children, result, "", "", function(child) { return func.call(context, child, count++); }), result;