Skip to content

Commit

Permalink
fix(es/minifier): Abort property hoisting on eval (#8957)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored May 16, 2024
1 parent f681924 commit 02729f2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ impl Optimizer<'_> {
return None;
}

if !self.may_add_ident() {
return None;
}

// If a variable is initialized multiple time, we currently don't do anything
// smart.
let usage = self.data.vars.get(&name.to_id())?;
Expand Down
40 changes: 40 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/next/46887-2/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var Za = {
set: Qi,
get: Qg,
enforce: function (e1) {
return Ri(e1) ? Qg(e1) : Qi(e1, {});
},
getterFor: function (e1) {
return function (t) {
var n;
if (!ha(t) || (n = Qg(t)).type !== e1) throw TypeError("Incompatible receiver, " + e1 + " required");
return n;
};
}
}

export function exposed() {
try {
var a = eval("quire".replace(/^/, "re"))(c);
if (a && (a.length || Object.keys(a).length)) return a;
} catch (e1) { }
return null;
}

export default (function (e) {
var t = Za.get,
n = Za.enforce,
r = String(String).split("String");
(e.exports = function (e, t, i, o) {
var a = !!o && !!o.unsafe,
s = !!o && !!o.enumerable;
if (o = !!o && !!o.noTargetGet, "function" == typeof i) {
"string" != typeof t || Q(i, "name") || Na(i, "name", t);
var u = n(i);
u.source || (u.source = r.join("string" == typeof t ? t : ""))
}
e === y ? s ? e[t] = i : Oi(t, i) : (a ? !o && e[t] && (s = !0) : delete e[t], s ? e[t] = i : Na(e, t, i))
})(Function.prototype, "toString", (function () {
return "function" == typeof this && t(this).source || Pi(this)
}))
})
35 changes: 35 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/next/46887-2/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var Za = {
set: Qi,
get: Qg,
enforce: function(e1) {
return Ri(e1) ? Qg(e1) : Qi(e1, {});
},
getterFor: function(e1) {
return function(t) {
var n;
if (!ha(t) || (n = Qg(t)).type !== e1) throw TypeError("Incompatible receiver, " + e1 + " required");
return n;
};
}
};
export function exposed() {
try {
var a = eval("quire".replace(/^/, "re"))(c);
if (a && (a.length || Object.keys(a).length)) return a;
} catch (e1) {}
return null;
}
export default function(e) {
var t = Za.get, n = Za.enforce, r = String(String).split("String");
(e.exports = function(e, t, i, o) {
var a = !!o && !!o.unsafe, s = !!o && !!o.enumerable;
if (o = !!o && !!o.noTargetGet, "function" == typeof i) {
"string" != typeof t || Q(i, "name") || Na(i, "name", t);
var u = n(i);
u.source || (u.source = r.join("string" == typeof t ? t : ""));
}
e === y ? s ? e[t] = i : Oi(t, i) : (a ? !o && e[t] && (s = !0) : delete e[t], s ? e[t] = i : Na(e, t, i));
})(Function.prototype, "toString", function() {
return "function" == typeof this && t(this).source || Pi(this);
});
}

0 comments on commit 02729f2

Please sign in to comment.