mirror of
https://github.com/swc-project/swc.git
synced 2024-11-24 18:28:13 +03:00
fix(es/minifier): Preserve return values of recursive IIFE (#6142)
**Description:** This PR fixes the logic for dropping return values of IIFE. **Related issue:** - Closes https://github.com/swc-project/swc/issues/6141.
This commit is contained in:
parent
31023f8e7e
commit
3d271e82a2
@ -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() {
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
14
crates/swc_ecma_minifier/tests/fixture/issues/6141/input.js
Normal file
14
crates/swc_ecma_minifier/tests/fixture/issues/6141/input.js
Normal file
@ -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;
|
||||
})()
|
10
crates/swc_ecma_minifier/tests/fixture/issues/6141/output.js
Normal file
10
crates/swc_ecma_minifier/tests/fixture/issues/6141/output.js
Normal file
@ -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;
|
||||
}();
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user