diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index 11f05df89cf0..95f01b178f0e 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -1006,7 +1006,11 @@ impl Compiler { .with_emit_assert_for_import_attributes( config.emit_assert_for_import_attributes, ), - output: Some(output), + output: if output.is_empty() { + None + } else { + Some(output) + }, }, ) }) diff --git a/crates/swc/tests/fixture/issues-9xxx/9015/input/.swcrc b/crates/swc/tests/fixture/issues-9xxx/9015/input/.swcrc new file mode 100644 index 000000000000..0b625e989920 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9015/input/.swcrc @@ -0,0 +1,19 @@ +{ + "jsc": { + "parser": { + "syntax": "ecmascript", + "jsx": false + }, + "target": "es2015", + "loose": false, + "minify": { + "compress": false, + "mangle": true + } + }, + "module": { + "type": "es6" + }, + "minify": true, + "isModule": false +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9015/input/1.js b/crates/swc/tests/fixture/issues-9xxx/9015/input/1.js new file mode 100644 index 000000000000..7fd21cbb53ad --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9015/input/1.js @@ -0,0 +1 @@ +register(function duplicate_func(duplicate_param, offset = 0) { }) diff --git a/crates/swc/tests/fixture/issues-9xxx/9015/output/1.js b/crates/swc/tests/fixture/issues-9xxx/9015/output/1.js new file mode 100644 index 000000000000..88d474b91428 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9015/output/1.js @@ -0,0 +1 @@ +register(function e(i,n=0){}); diff --git a/crates/swc/tests/tsc-references/parameterInitializersForwardReferencing.2(target=es2015).1.normal.js b/crates/swc/tests/tsc-references/parameterInitializersForwardReferencing.2(target=es2015).1.normal.js index c9a59d368b27..273e67f9d735 100644 --- a/crates/swc/tests/tsc-references/parameterInitializersForwardReferencing.2(target=es2015).1.normal.js +++ b/crates/swc/tests/tsc-references/parameterInitializersForwardReferencing.2(target=es2015).1.normal.js @@ -3,14 +3,14 @@ import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties"; function a() {} function b(_param = a()) { - var { b = a() } = _param, x = _object_without_properties(_param, [ + var { b: b1 = a() } = _param, x = _object_without_properties(_param, [ "b" ]); var a1; } const x = ""; function c(_param = a(), d = x) { - var { b } = _param, c = _object_without_properties(_param, [ + var { b } = _param, c1 = _object_without_properties(_param, [ "b" ]); var x1; diff --git a/crates/swc/tests/tsc-references/parameterInitializersForwardReferencing.2(target=esnext).1.normal.js b/crates/swc/tests/tsc-references/parameterInitializersForwardReferencing.2(target=esnext).1.normal.js index ca6a5a52a09d..3ce65475ccc6 100644 --- a/crates/swc/tests/tsc-references/parameterInitializersForwardReferencing.2(target=esnext).1.normal.js +++ b/crates/swc/tests/tsc-references/parameterInitializersForwardReferencing.2(target=esnext).1.normal.js @@ -1,10 +1,10 @@ //// [parameterInitializersForwardReferencing.2.ts] // https://github.com/microsoft/TypeScript/issues/36295 function a() {} -function b({ b = a(), ...x } = a()) { +function b({ b: b1 = a(), ...x } = a()) { var a1; } const x = ""; -function c({ b, ...c } = a(), d = x) { +function c({ b, ...c1 } = a(), d = x) { var x1; } diff --git a/crates/swc_ecma_minifier/tests/benches-full/victory.js b/crates/swc_ecma_minifier/tests/benches-full/victory.js index 604359246992..70bbc01bcfc4 100644 --- a/crates/swc_ecma_minifier/tests/benches-full/victory.js +++ b/crates/swc_ecma_minifier/tests/benches-full/victory.js @@ -25189,10 +25189,10 @@ "use strict"; function pairs(values, pairof = pair) { let previous; - const pairs = []; + const pairs1 = []; let first = !1; - for (const value of values)first && pairs.push(pairof(previous, value)), previous = value, first = !0; - return pairs; + for (const value of values)first && pairs1.push(pairof(previous, value)), previous = value, first = !0; + return pairs1; } function pair(a, b) { return [ diff --git a/crates/swc_ecma_minifier/tests/fixture/next/36127/2/3/output.js b/crates/swc_ecma_minifier/tests/fixture/next/36127/2/3/output.js index 4c1bad4d5f39..fb88c8b8d87b 100644 --- a/crates/swc_ecma_minifier/tests/fixture/next/36127/2/3/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/next/36127/2/3/output.js @@ -204,10 +204,10 @@ class e extends Text { return a(), 1 == o.length ? o[0] : new e(o, i); } } -function n(t, e, n = 0, i = 1e9) { - for(let s = 0, h = 0, l = !0; h < t.length && s <= i; h++){ - let r = t[h], o = s + r.length; - o >= n && (o > i && (r = r.slice(0, i - s)), s < n && (r = r.slice(n - s)), l ? (e[e.length - 1] += r, l = !1) : e.push(r)), s = o + 1; +function n(t, e, i = 0, s = 1e9) { + for(let n = 0, h = 0, l = !0; h < t.length && n <= s; h++){ + let r = t[h], o = n + r.length; + o >= i && (o > s && (r = r.slice(0, s - n)), n < i && (r = r.slice(i - n)), l ? (e[e.length - 1] += r, l = !1) : e.push(r)), n = o + 1; } return e; } diff --git a/crates/swc_ecma_minifier/tests/mangle/1/output.js b/crates/swc_ecma_minifier/tests/mangle/1/output.js index ce5f57a786ad..a5b875322e32 100644 --- a/crates/swc_ecma_minifier/tests/mangle/1/output.js +++ b/crates/swc_ecma_minifier/tests/mangle/1/output.js @@ -9,7 +9,7 @@ Object.defineProperty(exports, "__esModule", { }); const e = t(require("./config.json")); function n({ config: t = e.default } = {}) { - function n(e) { + function r(e) { const n = t[e] || null; if (!n) { return false; @@ -20,13 +20,13 @@ function n({ config: t = e.default } = {}) { } return Date.now() >= u && r; } - function r(e) { + function u(e) { const n = t[e] || null; return n; } return { - longlong10: r, - longlong2: n, + longlong10: u, + longlong2: r, longlong100: t }; } diff --git a/crates/swc_ecma_minifier/tests/mangle/reuse/2/output.js b/crates/swc_ecma_minifier/tests/mangle/reuse/2/output.js index b71439b432d0..99756cdda264 100644 --- a/crates/swc_ecma_minifier/tests/mangle/reuse/2/output.js +++ b/crates/swc_ecma_minifier/tests/mangle/reuse/2/output.js @@ -1,8 +1,8 @@ -function e(e, t = {}, n = false) { +function e(t, n = {}, r = false) { return { - rule: e, - params: t, - implicit: n + rule: t, + params: n, + implicit: r }; } function t(t) { diff --git a/crates/swc_ecma_minifier/tests/terser/compress/destructuring/issue_3205_3/output.mangleOnly.js b/crates/swc_ecma_minifier/tests/terser/compress/destructuring/issue_3205_3/output.mangleOnly.js index 75124db198cd..f10ff2f31424 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/destructuring/issue_3205_3/output.mangleOnly.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/destructuring/issue_3205_3/output.mangleOnly.js @@ -1,6 +1,6 @@ (function() { - function n(n, { a: o } = n) { - console.log(o); + function n(o, { a: c } = o) { + console.log(c); } n({ a: "PASS" diff --git a/crates/swc_ecma_minifier/tests/terser/compress/harmony/default_assign/output.mangleOnly.js b/crates/swc_ecma_minifier/tests/terser/compress/harmony/default_assign/output.mangleOnly.js index b05976810529..c07c998c16be 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/harmony/default_assign/output.mangleOnly.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/harmony/default_assign/output.mangleOnly.js @@ -1,5 +1,5 @@ -function o(o, n = 3) { - console.log(o); +function o(n, c = 3) { + console.log(n); } g = ([[] = 123])=>{}; h = ([[o, n, c] = [ @@ -7,7 +7,7 @@ h = ([[o, n, c] = [ 5, 6 ]] = [])=>{}; -function n([[o, n, c] = [ +function n([[o, c, l] = [ 4, 5, 6 diff --git a/crates/swc_ecma_minifier/tests/terser/compress/harmony/issue_t80/output.mangleOnly.js b/crates/swc_ecma_minifier/tests/terser/compress/harmony/issue_t80/output.mangleOnly.js index 0df4b7bdfac7..d0ad925860b9 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/harmony/issue_t80/output.mangleOnly.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/harmony/issue_t80/output.mangleOnly.js @@ -1,11 +1,11 @@ -function n(n = []) { - var l, u = "unused"; +function n(l = []) { + var u, e = "unused"; if (arguments.length == 1) { - n = [ - n + l = [ + l ]; } - return n; + return l; } console.log(JSON.stringify([ n(), diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_1043/issue_1043/output.mangleOnly.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_1043/issue_1043/output.mangleOnly.js index 2401cd065d18..abdc6efc53de 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_1043/issue_1043/output.mangleOnly.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_1043/issue_1043/output.mangleOnly.js @@ -1,9 +1,9 @@ -function* l(l = 0, n = null, f = 1) { - if (n == null) { - n = l; - l = 0; +function* l(n = 0, f = null, i = 1) { + if (f == null) { + f = n; + n = 0; } - for(let i = l; i < n; i += f){ - yield i; + for(let l = n; l < f; l += i){ + yield l; } } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/parameters/default_arguments/output.mangleOnly.js b/crates/swc_ecma_minifier/tests/terser/compress/parameters/default_arguments/output.mangleOnly.js index 9d2d8cdd9366..f7358efb607f 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/parameters/default_arguments/output.mangleOnly.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/parameters/default_arguments/output.mangleOnly.js @@ -1,5 +1,5 @@ -function n(n = 6) {} -function n(n = 6 + 5) {} -function n({ foo: n } = {}, [c] = [ +function n(c = 6) {} +function n(c = 6 + 5) {} +function n({ foo: c } = {}, [f] = [ 1 ]) {} diff --git a/crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs b/crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs index 9c26d5cc4c25..bb7f5ad429ac 100644 --- a/crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs +++ b/crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs @@ -250,7 +250,14 @@ impl Visit for Analyzer { self.add_decl(f.ident.to_id(), true); // https://github.com/swc-project/swc/issues/6819 - let has_rest = f.function.params.iter().any(|p| p.pat.is_rest()); + // + // We need to check for assign pattern because safari has a bug. + // https://github.com/swc-project/swc/issues/9015 + let has_rest = f + .function + .params + .iter() + .any(|p| p.pat.is_rest() || p.pat.is_assign()); if has_rest { self.add_usage(f.ident.to_id()); } @@ -274,7 +281,14 @@ impl Visit for Analyzer { v.add_decl(id.to_id(), true); v.with_fn_scope(|v| { // https://github.com/swc-project/swc/issues/6819 - if f.function.params.iter().any(|p| p.pat.is_rest()) { + // + // We need to check for assign pattern because safari has a bug. + // https://github.com/swc-project/swc/issues/9015 + if f.function + .params + .iter() + .any(|p| p.pat.is_rest() || p.pat.is_assign()) + { v.add_usage(id.to_id()); }