Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/renamer): Workaround a bug of Safari #9029

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
},
)
})
Expand Down
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9015/input/.swcrc
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-9xxx/9015/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
register(function duplicate_func(duplicate_param, offset = 0) { })
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-9xxx/9015/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
register(function e(i,n=0){});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions crates/swc_ecma_minifier/tests/benches-full/victory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_ecma_minifier/tests/mangle/1/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
};
}
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_ecma_minifier/tests/mangle/reuse/2/output.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function o(o, n = 3) {
console.log(o);
function o(n, c = 3) {
console.log(n);
}
g = ([[] = 123])=>{};
h = ([[o, n, c] = [
4,
5,
6
]] = [])=>{};
function n([[o, n, c] = [
function n([[o, c, l] = [
4,
5,
6
Expand Down
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
]) {}
18 changes: 16 additions & 2 deletions crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}

Expand Down
Loading