You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
unsafe compress option produces invalid output when using ternary if - case not really hinted by readme #988
function breaks_terser() {
const t = [];
(Math.random() < 0.5 ? t : []).push("lengthisnownolongerzero")
return 1 <= t.length
}
const possible_outputs = [];
for (let i = 0; i < 100; i++) {
possible_outputs.push(breaks_terser());
}
console.log([...new Set(possible_outputs)]);
terser output or error
function n(){const n=[];return(Math.random()<.5?n:[]).push("lengthisnownolongerzero"),!1}const o=[];for(let e=0;e<100;e++)o.push(n());console.log([...new Set(o)]);
Output when ran: [false]
Expected result
Expected code output: function n(){const n=[];return(Math.random()<.5?n:[]).push("lengthisnownolongerzero"),1<=n.length}const o=[];for(let e=0;e<100;e++)o.push(n());console.log([...new Set(o)]); (same as without unsafe compress option)
Expected output when ran: [true, false] or [false, true]
Context
I wondered today why my slider is not loading the images behind the viewport after the images in the viewport has loaded in production but does so in development. The example code here is really dumbed down to demonstrate the issue. But basically terser assumes the length of the array is going to be zero because it doesn't understand that the ternary statement can change its length.
If the ternary statement is converted to a normal if statement like this the terser output is as expected:
function breaks_terser() {
const t = [];
if (Math.random() < 0.5) {
t.push("lengthisnownolongerzero")
} else {
[].push("lengthisnownolongerzero");
}
return 1 <= t.length
}
const possible_outputs = [];
for (let i = 0; i < 100; i++) {
possible_outputs.push(breaks_terser());
}
console.log([...new Set(possible_outputs)]);
Activity
fabiosantoscode commentedon May 30, 2023
Looks like this has been since fixed!