diff --git a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs index e57461436703..3f9e4fb6e1a2 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs @@ -430,7 +430,9 @@ impl Optimizer<'_> { pub(super) fn invoke_iife(&mut self, e: &mut Expr) { trace_op!("iife: invoke_iife"); - if self.options.inline == 0 { + if self.options.inline == 0 + && !(self.options.reduce_vars && self.options.reduce_fns && self.options.evaluate) + { let skip = match e { Expr::Call(v) => !v.callee.span().is_dummy(), _ => true, diff --git a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs index 820888ed1a3b..5a7704819749 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs @@ -31,7 +31,7 @@ use crate::{ maybe_par, mode::Mode, option::{CompressOptions, MangleOptions}, - program_data::ProgramData, + program_data::{ProgramData, VarUsageInfo}, util::{ contains_eval, contains_leaping_continue_with_label, make_number, ExprOptExt, ModuleItemExt, }, @@ -323,7 +323,7 @@ impl From<&Function> for FnMetadata { impl Optimizer<'_> { fn may_remove_ident(&self, id: &Ident) -> bool { - if self.ctx.is_exported { + if let Some(VarUsageInfo { exported: true, .. }) = self.data.vars.get(&id.clone().to_id()) { return false; } @@ -2041,6 +2041,7 @@ impl VisitMut for Optimizer<'_> { let ctx = Ctx { top_level: false, + in_fn_like: true, is_lhs_of_assign: false, is_exact_lhs_of_assign: false, ..self.ctx @@ -2062,7 +2063,14 @@ impl VisitMut for Optimizer<'_> { } } - e.visit_mut_children_with(self); + let ctx = Ctx { + top_level: false, + in_fn_like: true, + is_lhs_of_assign: false, + is_exact_lhs_of_assign: false, + ..self.ctx + }; + e.visit_mut_children_with(&mut *self.with_ctx(ctx)); } #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index 946714199bdb..eedfdd1050f1 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -557,7 +557,16 @@ impl Optimizer<'_> { options: &CompressOptions, ) -> Option>> { Some(match s { - Stmt::Expr(e) => vec![Mergable::Expr(&mut e.expr)], + Stmt::Expr(e) => { + if self.options.sequences() + || self.options.collapse_vars + || self.options.side_effects + { + vec![Mergable::Expr(&mut e.expr)] + } else { + return None; + } + } Stmt::Decl(Decl::Var(v)) => { if options.reduce_vars || options.collapse_vars { v.decls.iter_mut().map(Mergable::Var).collect() @@ -612,7 +621,7 @@ impl Optimizer<'_> { where T: ModuleItemExt, { - if !self.options.sequences() && !self.options.collapse_vars { + if !self.options.sequences() && !self.options.collapse_vars && !self.options.reduce_vars { log_abort!("sequences: [x] Disabled"); return; } @@ -1513,6 +1522,16 @@ impl Optimizer<'_> { ) }; + // Respect top_retain + if let Some(a_id) = a.id() { + if a_id.0 == "arguments" + || (matches!(a, Mergable::Var(_) | Mergable::FnDecl(_)) + && !self.may_remove_ident(&Ident::from(a_id))) + { + return Ok(false); + } + } + if match &*b { Expr::Arrow(..) | Expr::Fn(..) diff --git a/crates/swc_ecma_minifier/tests/TODO.txt b/crates/swc_ecma_minifier/tests/TODO.txt index 36c51d818c38..52ba12010a4d 100644 --- a/crates/swc_ecma_minifier/tests/TODO.txt +++ b/crates/swc_ecma_minifier/tests/TODO.txt @@ -43,7 +43,6 @@ collapse_vars/issue_1858/input.js collapse_vars/issue_2187_2/input.js collapse_vars/issue_2203_2/input.js collapse_vars/issue_2203_4/input.js -collapse_vars/issue_2298/input.js collapse_vars/issue_2319_1/input.js collapse_vars/issue_2319_3/input.js collapse_vars/issue_2436_1/input.js @@ -119,11 +118,8 @@ drop_unused/issue_2660_2/input.js drop_unused/issue_2665/input.js drop_unused/issue_2846/input.js drop_unused/issue_t161_top_retain_10/input.js -drop_unused/issue_t161_top_retain_11/input.js drop_unused/issue_t161_top_retain_12/input.js drop_unused/issue_t161_top_retain_15/input.js -drop_unused/issue_t161_top_retain_8/input.js -drop_unused/issue_t161_top_retain_9/input.js drop_unused/issue_t183/input.js drop_unused/keep_assign/input.js drop_unused/reassign_const/input.js @@ -159,7 +155,6 @@ functions/issue_2107/input.js functions/issue_2114_1/input.js functions/issue_2114_2/input.js functions/issue_2601_2/input.js -functions/issue_2604_2/input.js functions/issue_2620_2/input.js functions/issue_2620_3/input.js functions/issue_2630_2/input.js @@ -297,13 +292,8 @@ reduce_vars/iife/input.js reduce_vars/iife_new/input.js reduce_vars/inner_var_for_2/input.js reduce_vars/inverted_var/input.js -reduce_vars/issue_1595_3/input.js -reduce_vars/issue_1670_2/input.js -reduce_vars/issue_1670_4/input.js -reduce_vars/issue_1670_5/input.js reduce_vars/issue_1850_2/input.js reduce_vars/issue_2485/input.js -reduce_vars/issue_2757_1/input.js reduce_vars/issue_2799_2/input.js reduce_vars/issue_2836/input.js reduce_vars/issue_2860_2/input.js diff --git a/crates/swc_ecma_minifier/tests/fixture/next/56408/output.js b/crates/swc_ecma_minifier/tests/fixture/next/56408/output.js index 2c2202d78dbc..603cb4ee2579 100644 --- a/crates/swc_ecma_minifier/tests/fixture/next/56408/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/next/56408/output.js @@ -4,9 +4,7 @@ exports.ids = [ ]; exports.modules = { 4622: function(e, t, n) { - !function(e, i) { - i(t, n(9885), n(5601)); - }(0, function(e, t, n) { + void function(e, t, n) { "use strict"; function r(e) { return e && "object" == typeof e && "default" in e ? e : { @@ -677,8 +675,7 @@ exports.modules = { function P(e) { var t = e.getAsFile(); if (!t) return Promise.reject("".concat(e, " is not a File")); - var n = h(t); - return Promise.resolve(n); + return Promise.resolve(h(t)); } function S(e) { return v(this, void 0, void 0, function() { @@ -754,8 +751,7 @@ exports.modules = { 2, new Promise(function(t, n) { e.file(function(n) { - var i = h(n, e.fullPath); - t(i); + t(h(n, e.fullPath)); }, function(e) { n(e); }); @@ -765,8 +761,7 @@ exports.modules = { }); } var T = "file-invalid-type", I = "file-too-large", M = "file-too-small", L = "too-many-files", _ = function(e) { - e = Array.isArray(e) && 1 === e.length ? e[0] : e; - var t = Array.isArray(e) ? "one of ".concat(e.join(", ")) : e; + var t = Array.isArray(e = Array.isArray(e) && 1 === e.length ? e[0] : e) ? "one of ".concat(e.join(", ")) : e; return { code: T, message: "File type must be ".concat(t) @@ -947,9 +942,7 @@ exports.modules = { return D(e) && D(e.dataTransfer) ? [ 2, j(e.dataTransfer, e.type) - ] : function(e) { - return D(e) && D(e.target); - }(e) ? [ + ] : D(e) && D(e.target) ? [ 2, x(e) ] : Array.isArray(e) && e.every(function(e) { @@ -1030,9 +1023,9 @@ exports.modules = { return "function" == typeof es ? es : me; }, [ es - ]), eF = t.useRef(null), ej = t.useRef(null), ek = t.useReducer(ve, ec), eE = s(ek, 2), eP = eE[0], eC = eE[1], eS = eP.isFocused, ez = eP.isFileDialogActive, eR = t.useRef("undefined" != typeof window && window.isSecureContext && ep && Z()), ie = function() { - !eR.current && ez && setTimeout(function() { - ej.current && (ej.current.files.length || (eC({ + ]), eF = t.useRef(null), ej = t.useRef(null), ek = s(t.useReducer(ve, ec), 2), eE = ek[0], eP = ek[1], eC = eE.isFocused, eS = eE.isFileDialogActive, ez = t.useRef("undefined" != typeof window && window.isSecureContext && ep && Z()), ie = function() { + !ez.current && eS && setTimeout(function() { + ej.current && (ej.current.files.length || (eP({ type: "closeDialog" }), eO())); }, 300); @@ -1043,12 +1036,12 @@ exports.modules = { }; }, [ ej, - ez, + eS, eO, - eR + ez ]); - var eT = t.useRef([]), ce = function(e) { - eF.current && eF.current.contains(e.target) || (e.preventDefault(), eT.current = []); + var eR = t.useRef([]), ce = function(e) { + eF.current && eF.current.contains(e.target) || (e.preventDefault(), eR.current = []); }; t.useEffect(function() { return ev && (document.addEventListener("dragover", Y, !1), document.addEventListener("drop", ce, !1)), function() { @@ -1064,12 +1057,12 @@ exports.modules = { ed, b ]); - var eI = t.useCallback(function(e) { + var eT = t.useCallback(function(e) { eh ? eh(e) : console.error(e); }, [ eh - ]), eM = t.useCallback(function(e) { - e.preventDefault(), e.persist(), ke(e), eT.current = [].concat(f(eT.current), [ + ]), eI = t.useCallback(function(e) { + e.preventDefault(), e.persist(), ke(e), eR.current = [].concat(f(eR.current), [ e.target ]), N(e) && Promise.resolve(w(e)).then(function(t) { if (!H(e) || eb) { @@ -1082,7 +1075,7 @@ exports.modules = { maxFiles: M, validator: eD }); - eC({ + eP({ isDragAccept: i, isDragReject: n > 0 && !i, isDragActive: !0, @@ -1090,12 +1083,12 @@ exports.modules = { }), L && L(e); } }).catch(function(e) { - return eI(e); + return eT(e); }); }, [ w, L, - eI, + eT, eb, ew, T, @@ -1103,7 +1096,7 @@ exports.modules = { I, M, eD - ]), eL = t.useCallback(function(e) { + ]), eM = t.useCallback(function(e) { e.preventDefault(), e.persist(), ke(e); var t = N(e); if (t && e.dataTransfer) try { @@ -1113,12 +1106,12 @@ exports.modules = { }, [ en, eb - ]), e_ = t.useCallback(function(e) { + ]), eL = t.useCallback(function(e) { e.preventDefault(), e.persist(), ke(e); - var t = eT.current.filter(function(e) { + var t = eR.current.filter(function(e) { return eF.current && eF.current.contains(e); }), n = t.indexOf(e.target); - -1 !== n && t.splice(n, 1), eT.current = t, t.length > 0 || (eC({ + -1 !== n && t.splice(n, 1), eR.current = t, t.length > 0 || (eP({ type: "setDraggedFiles", isDragActive: !1, isDragAccept: !1, @@ -1128,7 +1121,7 @@ exports.modules = { eF, et, eb - ]), eB = t.useCallback(function(e, t) { + ]), e_ = t.useCallback(function(e, t) { var n = [], i = []; e.forEach(function(e) { var t = s(U(e, ew), 2), b = t[0], w = t[1], I = s(W(e, T, R), 2), M = I[0], L = I[1], q = eD ? eD(e) : null; @@ -1152,13 +1145,13 @@ exports.modules = { q ] }); - }), n.splice(0)), eC({ + }), n.splice(0)), eP({ acceptedFiles: n, fileRejections: i, type: "setFiles" }), ei && ei(n, i, t), i.length > 0 && el && el(i, t), n.length > 0 && eu && eu(n, t); }, [ - eC, + eP, I, ew, T, @@ -1168,22 +1161,22 @@ exports.modules = { eu, el, eD - ]), eK = t.useCallback(function(e) { - e.preventDefault(), e.persist(), ke(e), eT.current = [], N(e) && Promise.resolve(w(e)).then(function(t) { - H(e) && !eb || eB(t, e); + ]), eB = t.useCallback(function(e) { + e.preventDefault(), e.persist(), ke(e), eR.current = [], N(e) && Promise.resolve(w(e)).then(function(t) { + H(e) && !eb || e_(t, e); }).catch(function(e) { - return eI(e); - }), eC({ + return eT(e); + }), eP({ type: "reset" }); }, [ w, - eB, - eI, + e_, + eT, eb - ]), e$ = t.useCallback(function() { - if (eR.current) { - eC({ + ]), eK = t.useCallback(function() { + if (ez.current) { + eP({ type: "openDialog" }), eA(); window.showOpenFilePicker({ @@ -1192,44 +1185,44 @@ exports.modules = { }).then(function(e) { return w(e); }).then(function(e) { - eB(e, null), eC({ + e_(e, null), eP({ type: "closeDialog" }); }).catch(function(e) { - ne(e) ? (eO(e), eC({ + ne(e) ? (eO(e), eP({ type: "closeDialog" - })) : re(e) ? (eR.current = !1, ej.current ? (ej.current.value = null, ej.current.click()) : eI(Error("Cannot open the file picker because the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API is not supported and no was provided."))) : eI(e); + })) : re(e) ? (ez.current = !1, ej.current ? (ej.current.value = null, ej.current.click()) : eT(Error("Cannot open the file picker because the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API is not supported and no was provided."))) : eT(e); }); - } else ej.current && (eC({ + } else ej.current && (eP({ type: "openDialog" }), eA(), ej.current.value = null, ej.current.click()); }, [ - eC, + eP, eA, eO, ep, - eB, - eI, + e_, + eT, ex, I - ]), eX = t.useCallback(function(e) { - eF.current && eF.current.isEqualNode(e.target) && (" " !== e.key && "Enter" !== e.key && 32 !== e.keyCode && 13 !== e.keyCode || (e.preventDefault(), e$())); + ]), e$ = t.useCallback(function(e) { + eF.current && eF.current.isEqualNode(e.target) && (" " !== e.key && "Enter" !== e.key && 32 !== e.keyCode && 13 !== e.keyCode || (e.preventDefault(), eK())); }, [ eF, - e$ - ]), eH = t.useCallback(function() { - eC({ + eK + ]), eX = t.useCallback(function() { + eP({ type: "focus" }); - }, []), eN = t.useCallback(function() { - eC({ + }, []), eH = t.useCallback(function() { + eP({ type: "blur" }); - }, []), eU = t.useCallback(function() { - em || (V() ? setTimeout(e$, 0) : e$()); + }, []), eN = t.useCallback(function() { + em || (V() ? setTimeout(eK, 0) : eK()); }, [ em, - e$ + eK ]), Ae = function(e) { return b ? null : e; }, Oe = function(e) { @@ -1238,18 +1231,18 @@ exports.modules = { return ey ? null : Ae(e); }, ke = function(e) { eb && e.stopPropagation(); - }, eW = t.useMemo(function() { + }, eU = t.useMemo(function() { return function() { var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.refKey, n = e.role, i = e.onKeyDown, w = e.onFocus, R = e.onBlur, T = e.onClick, I = e.onDragEnter, M = e.onDragOver, L = e.onDragLeave, q = e.onDrop, et = l(e, er); return c(c(u({ - onKeyDown: Oe(X(i, eX)), - onFocus: Oe(X(w, eH)), - onBlur: Oe(X(R, eN)), - onClick: Ae(X(T, eU)), - onDragEnter: Ee(X(I, eM)), - onDragOver: Ee(X(M, eL)), - onDragLeave: Ee(X(L, e_)), - onDrop: Ee(X(q, eK)), + onKeyDown: Oe(X(i, e$)), + onFocus: Oe(X(w, eX)), + onBlur: Oe(X(R, eH)), + onClick: Ae(X(T, eN)), + onDragEnter: Ee(X(I, eI)), + onDragOver: Ee(X(M, eM)), + onDragLeave: Ee(X(L, eL)), + onDrop: Ee(X(q, eB)), role: "string" == typeof n && "" !== n ? n : "presentation" }, void 0 === t ? "ref" : t, eF), b || eg ? {} : { tabIndex: 0 @@ -1257,48 +1250,48 @@ exports.modules = { }; }, [ eF, + e$, eX, eH, eN, - eU, + eI, eM, eL, - e_, - eK, + eB, eg, ey, b - ]), eG = t.useCallback(function(e) { + ]), eW = t.useCallback(function(e) { e.stopPropagation(); - }, []), eY = t.useMemo(function() { + }, []), eG = t.useMemo(function() { return function() { - var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.refKey, n = e.onChange, i = e.onClick, b = l(e, eo), w = u({ + var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.refKey, n = e.onChange, i = e.onClick, b = l(e, eo); + return c(c({}, u({ accept: ew, multiple: I, type: "file", style: { display: "none" }, - onChange: Ae(X(n, eK)), - onClick: Ae(X(i, eG)), + onChange: Ae(X(n, eB)), + onClick: Ae(X(i, eW)), tabIndex: -1 - }, void 0 === t ? "ref" : t, ej); - return c(c({}, w), b); + }, void 0 === t ? "ref" : t, ej)), b); }; }, [ ej, i, I, - eK, + eB, b ]); - return c(c({}, eP), {}, { - isFocused: eS && !b, - getRootProps: eW, - getInputProps: eY, + return c(c({}, eE), {}, { + isFocused: eC && !b, + getRootProps: eU, + getInputProps: eG, rootRef: eF, inputRef: ej, - open: Ae(e$) + open: Ae(eK) }); } function ve(e, t) { @@ -1345,6 +1338,6 @@ exports.modules = { }, e.default = ei, e.useDropzone = de, Object.defineProperty(e, "__esModule", { value: !0 }); - }); + }(t, n(9885), n(5601)); } }; diff --git a/crates/swc_ecma_minifier/tests/passing.txt b/crates/swc_ecma_minifier/tests/passing.txt index ab0647af1078..252de272a74b 100644 --- a/crates/swc_ecma_minifier/tests/passing.txt +++ b/crates/swc_ecma_minifier/tests/passing.txt @@ -188,6 +188,7 @@ collapse_vars/issue_2203_1/input.js collapse_vars/issue_2203_3/input.js collapse_vars/issue_2250_1/input.js collapse_vars/issue_2250_2/input.js +collapse_vars/issue_2298/input.js collapse_vars/issue_2313_1/input.js collapse_vars/issue_2313_2/input.js collapse_vars/issue_2319_2/input.js @@ -459,6 +460,7 @@ drop_unused/issue_3192/input.js drop_unused/issue_805_1/input.js drop_unused/issue_805_2/input.js drop_unused/issue_t161_top_retain_1/input.js +drop_unused/issue_t161_top_retain_11/input.js drop_unused/issue_t161_top_retain_13/input.js drop_unused/issue_t161_top_retain_14/input.js drop_unused/issue_t161_top_retain_2/input.js @@ -467,6 +469,8 @@ drop_unused/issue_t161_top_retain_4/input.js drop_unused/issue_t161_top_retain_5/input.js drop_unused/issue_t161_top_retain_6/input.js drop_unused/issue_t161_top_retain_7/input.js +drop_unused/issue_t161_top_retain_8/input.js +drop_unused/issue_t161_top_retain_9/input.js drop_unused/keep_fnames/input.js drop_unused/unused_block_decls/input.js drop_unused/unused_block_decls_in_catch/input.js @@ -624,6 +628,7 @@ functions/issue_2531_2/input.js functions/issue_2531_3/input.js functions/issue_2601_1/input.js functions/issue_2604_1/input.js +functions/issue_2604_2/input.js functions/issue_2616/input.js functions/issue_2620_1/input.js functions/issue_2620_4/input.js @@ -1308,10 +1313,14 @@ reduce_vars/issue_1533_1/input.js reduce_vars/issue_1533_2/input.js reduce_vars/issue_1595_1/input.js reduce_vars/issue_1595_2/input.js +reduce_vars/issue_1595_3/input.js reduce_vars/issue_1595_4/input.js reduce_vars/issue_1606/input.js reduce_vars/issue_1670_1/input.js +reduce_vars/issue_1670_2/input.js reduce_vars/issue_1670_3/input.js +reduce_vars/issue_1670_4/input.js +reduce_vars/issue_1670_5/input.js reduce_vars/issue_1670_6/input.js reduce_vars/issue_1814_1/input.js reduce_vars/issue_1814_2/input.js @@ -1352,6 +1361,7 @@ reduce_vars/issue_2496/input.js reduce_vars/issue_2598/input.js reduce_vars/issue_2669/input.js reduce_vars/issue_2670/input.js +reduce_vars/issue_2757_1/input.js reduce_vars/issue_2757_2/input.js reduce_vars/issue_2774/input.js reduce_vars/issue_2799_1/input.js diff --git a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_14/output.js b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_14/output.js index ffc287766515..3fc81a2d17f3 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_14/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_14/output.js @@ -4,7 +4,7 @@ class Alpha { } } let x = 2, z = 4; -console.log(2, 3, 4, 6, 8, 12, 2, 3, 4, new Alpha().num(), new class { +console.log(2, 3, z, 6, 2 * z, 3 * z, 2, 3, z, new Alpha().num(), new class { num() { return 3; } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_3/output.js index 2f276ce45e88..446e64265475 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_3/output.js @@ -1 +1,4 @@ -console.log(2, 3); +function f() { + return 2; +} +console.log(f(), 3); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js index c1ac28d23483..528a05163fdf 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js @@ -1,6 +1,5 @@ function f() { - var a = (function () { + return (function() { return x(); - })(); - return a(42); + })()(42); } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js index 510f9716e3a9..d135a3374f16 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js @@ -1,12 +1,11 @@ (function(outputs) { var handlers = []; for(var i = 0; i < outputs.length; i++){ - var output = outputs[i]; var handleEventClosure = function(eventName) { return function() { return console.log(eventName); }; - }(output.eventName); + }(outputs[i].eventName); handlers.push(handleEventClosure); } return handlers; diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js index 1c76575b74c2..6d3b61fb0b31 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js @@ -1,14 +1,11 @@ (function() { (function() { - { - const data = function(data) { - return data[data[0]]; - }([ - 1, - 2, - 3 - ]); - console.log(data); - } + console.log(function(data) { + return data[data[0]]; + }([ + 1, + 2, + 3 + ])); })(); })(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_1/output.js index 5190f9d8d7e4..9a4742864df4 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_1/output.js @@ -1,5 +1 @@ -(function () { - (function () { - f(1).bar = 1; - })(); -})(); +f(1).bar = 1; diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_2/output.js index 90abd344fc0f..127fc602c90e 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_2/output.js @@ -1,6 +1,4 @@ -(function () { - (function () { - var b = "long piece of string"; - f(b).bar = b; - })(); +(function() { + var b; + b = "long piece of string", f(b).bar = b; })(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_1656/f7/output.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_1656/f7/output.js index 1dc131a91fdb..223ae0cd40b3 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_1656/f7/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_1656/f7/output.js @@ -1,4 +1,2 @@ var b = 10; -!function() { - b = 100; -}(), console.log(100, b); +console.log(100, b = 100); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js index 915e71b8c6b4..4df86f06d751 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js @@ -1,4 +1,4 @@ -!(function () { +!function() { var a; console.log(a); -})(); +}(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js index 915e71b8c6b4..4df86f06d751 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js @@ -1,4 +1,4 @@ -!(function () { +!function() { var a; console.log(a); -})(); +}(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/defun_reference/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/defun_reference/output.js index ced459561939..234b5b6082f9 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/defun_reference/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/defun_reference/output.js @@ -3,10 +3,7 @@ function f() { x(); return a; } - var a = function() { - y(); - return 2; - }(); + var a = (y(), 2); var b = 2; return a + 2; } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/iife_assign/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/iife_assign/output.js index 96b2b6a0dd1b..296d5492b003 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/iife_assign/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/iife_assign/output.js @@ -1,10 +1 @@ -!(function () { - var a = 1, - b = 0; - !(function () { - b++; - return; - a = 2; - })(); - console.log(a); -})(); +console.log(1); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/immutable/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/immutable/output.js index 6cf85793521d..86cac6b94aa3 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/immutable/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/immutable/output.js @@ -1,3 +1 @@ -!(function () { - console.log("test".indexOf("e")); -})(); +console.log("test".indexOf("e")); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/inner_var_for_in_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/inner_var_for_in_1/output.js index 531609c6dea5..3954b13c27a0 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/inner_var_for_in_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/inner_var_for_in_1/output.js @@ -1,11 +1,7 @@ function f() { - var a = 1, - b = 2; - for (b in (function () { - return x(1, b, c); - })()) { - var c = 3, - d = 4; + var a = 1, b = 2; + for(b in x(1, b, c)){ + var c = 3, d = 4; x(1, b, c, d); } x(1, b, c, d); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js index 74462d6b35c8..4c14ab0a4ab0 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js @@ -1,3 +1 @@ -(function(a) { - return g(3); -})(0); +g(3); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_3/output.js index c44c43c1d31a..4c14ab0a4ab0 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_3/output.js @@ -1,3 +1 @@ -(function (a) { - return g(3); -})(); +g(3); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js index 8385afee60ed..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js @@ -1,4 +1 @@ -(function () { - var a; - void 0 === a ? console.log("PASS") : console.log("FAIL"); -})(); +console.log("PASS"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js index 1d10f61fe231..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js @@ -1,3 +1 @@ -(function () { - console.log("PASS"); -})(); +console.log("PASS"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js index 8385afee60ed..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js @@ -1,4 +1 @@ -(function () { - var a; - void 0 === a ? console.log("PASS") : console.log("FAIL"); -})(); +console.log("PASS"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_4/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_4/output.js index 1d10f61fe231..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_4/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_4/output.js @@ -1,3 +1 @@ -(function () { - console.log("PASS"); -})(); +console.log("PASS"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js index 17a626e090fb..296d5492b003 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js @@ -1,3 +1 @@ -(function () { - console.log(1); -})(); +console.log(1); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_1/output.js index 6bf7793cafb0..3f684501d414 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_1/output.js @@ -1,6 +1,5 @@ const a = 42; !function() { - !function(a) { - console.log(a++, 42); - }(0); + var a; + a = 0, console.log(a++, 42); }(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_2/output.js index 0743f91fda70..a2756e48d5f1 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_2/output.js @@ -1,6 +1,5 @@ const a = "32"; !function() { - !function(a) { - console.log("321", a++); - }(0); + var a; + a = 0, console.log("321", a++); }(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/lvalues_def_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/lvalues_def_2/output.js index c9757f3de3fc..42a40d68d719 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/lvalues_def_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/lvalues_def_2/output.js @@ -1,3 +1,3 @@ -var b = 1; -var a = b += 1, b = NaN; +var b; +var a = b = 2, b = NaN; console.log(a, b); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_1/output.js index 23806c824580..3efbb16ddd20 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_1/output.js @@ -1,5 +1,3 @@ var x = 3; -do (function() { - console.log("bar:", --x); -})(); +do console.log("bar:", --x); while (x) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_2/output.js index 82d1caa8657a..1e52ba580cd0 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_2/output.js @@ -1,3 +1 @@ -for(;;)(function() { - console.log("bar:"); -})(); +for(;;)console.log("bar:"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_1/output.js index 80cde5a86f24..e00b68aa31ad 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_1/output.js @@ -1,3 +1 @@ -!(function () { - console.log(2); -})(); +console.log(2); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_2/output.js index 6aa4f3429c94..e00b68aa31ad 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_2/output.js @@ -1,3 +1 @@ -!function() { - console.log(2); -}(); +console.log(2); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_5/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_5/output.js index b6a1c4996a6b..3150c743eeaa 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_5/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_5/output.js @@ -1,6 +1,5 @@ !function() { var a; - !function(b) { - console.log(a = 2, b); - }(a); + var b; + b = a, console.log(a = 2, b); }(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_6/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_6/output.js index 46c2da7be8b1..653cbc482c32 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_6/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_6/output.js @@ -1,4 +1,4 @@ -!(function () { - var a = (function () {})((a = 1)); +!function() { + var a = void (a = 1); console.log(a); -})(); +}();