Skip to content

Commit

Permalink
fix(es/minifier): Consider side effects of operands of binary express…
Browse files Browse the repository at this point in the history
…ions (#8929)

**Related issue:**

 - Closes #8923
  • Loading branch information
kdy1 authored May 7, 2024
1 parent 9bf5790 commit 4d4a7a9
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 6 deletions.
3 changes: 2 additions & 1 deletion crates/swc_ecma_minifier/src/compress/pure/bools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 523,8 @@ impl Pure<'_> {
.as_bin()
.filter(|b| b.op.precedence() == op.precedence())
.is_none()
&& !left.may_have_side_effects(&self.expr_ctx));
&& !left.may_have_side_effects(&self.expr_ctx)
&& !right.may_have_side_effects(&self.expr_ctx));

if can_swap {
report_change!("Swapping operands of binary expession");
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/benches-full/d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 1672,7 @@
else {
var d1 = Math.sqrt(d2), b0 = (w1 * w1 - w0 * w0 rho4 * d2) / (2 * w0 * rho2 * d1), b1 = (w1 * w1 - w0 * w0 - rho4 * d2) / (2 * w1 * rho2 * d1), r0 = Math.log(Math.sqrt(b0 * b0 1) - b0);
S = (Math.log(Math.sqrt(b1 * b1 1) - b1) - r0) / rho, i = function(t) {
var x, x1, s = t * S, coshr0 = cosh(r0), u = w0 / (rho2 * d1) * (((x = Math.exp(2 * (x = rho * s r0))) - 1) / (x 1) * coshr0 - ((x1 = Math.exp(x1 = r0)) - 1 / x1) / 2);
var x, x1, s = t * S, coshr0 = cosh(r0), u = w0 / (rho2 * d1) * (coshr0 * (((x = Math.exp(2 * (x = rho * s r0))) - 1) / (x 1)) - ((x1 = Math.exp(x1 = r0)) - 1 / x1) / 2);
return [
ux0 u * dx,
uy0 u * dy,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/benches-full/echarts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 1158,7 @@
},
elasticInOut: function(k) {
var s, a = 0.1;
return 0 === k ? 0 : 1 === k ? 1 : (!a || a < 1 ? (a = 1, s = 0.1) : s = 0.4 * Math.asin(1 / a) / (2 * Math.PI), (k *= 2) < 1) ? -(a * Math.pow(2, 10 * (k -= 1)) * Math.sin(2 * Math.PI * (k - s) / 0.4) * 0.5) : a * Math.pow(2, -10 * (k -= 1)) * Math.sin(2 * Math.PI * (k - s) / 0.4) * 0.5 1;
return 0 === k ? 0 : 1 === k ? 1 : (!a || a < 1 ? (a = 1, s = 0.1) : s = 0.4 * Math.asin(1 / a) / (2 * Math.PI), (k *= 2) < 1) ? -0.5 * (a * Math.pow(2, 10 * (k -= 1)) * Math.sin(2 * Math.PI * (k - s) / 0.4)) : a * Math.pow(2, -10 * (k -= 1)) * Math.sin(2 * Math.PI * (k - s) / 0.4) * 0.5 1;
},
backIn: function(k) {
return k * k * (2.70158 * k - 1.70158);
Expand Down
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 @@ -2345,7 2345,7 @@
else {
var d1 = Math.sqrt(d2), b0 = (w1 * w1 - w0 * w0 4 * d2) / (2 * w0 * 2 * d1), b1 = (w1 * w1 - w0 * w0 - 4 * d2) / (2 * w1 * 2 * d1), r0 = Math.log(Math.sqrt(b0 * b0 1) - b0);
S = (Math.log(Math.sqrt(b1 * b1 1) - b1) - r0) / rho, i = function(t) {
var x, x1, s = t * S, coshr0 = cosh(r0), u = w0 / (2 * d1) * (((x = Math.exp(2 * (x = rho * s r0))) - 1) / (x 1) * coshr0 - ((x1 = Math.exp(x1 = r0)) - 1 / x1) / 2);
var x, x1, s = t * S, coshr0 = cosh(r0), u = w0 / (2 * d1) * (coshr0 * (((x = Math.exp(2 * (x = rho * s r0))) - 1) / (x 1)) - ((x1 = Math.exp(x1 = r0)) - 1 / x1) / 2);
return [
ux0 u * dx,
uy0 u * dy,
Expand Down Expand Up @@ -22643,7 22643,7 @@
firstChild && "stack" === firstChild.type.role && (barWidth = (firstChild.props.children && firstChild.props.children[0]).props.barWidth, dataLength = firstChild.props.children.length);
var width = barWidth || this.getWidth(props, children.length, dataLength);
return {
x: width * children.length / 2 (offset - (children.length - 1) / 2 * width)
x: width * children.length / 2 (offset - width * ((children.length - 1) / 2))
};
}
}
Expand Down Expand Up @@ -29100,7 29100,7 @@
x: function() {
if (!textAnchor || "middle" === textAnchor) return flyoutCenter.x;
var sign = "end" === textAnchor ? -1 : 1;
return flyoutCenter.x - labelSize.width / 2 * sign;
return flyoutCenter.x - sign * (labelSize.width / 2);
}() (flyoutPadding.left - flyoutPadding.right) / 2,
y: flyoutCenter.y (flyoutPadding.top - flyoutPadding.bottom) / 2,
verticalAnchor: "middle",
Expand Down
47 changes: 47 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8923/config.json
Original file line number Diff line number Diff line change
@@ -0,0 1,47 @@
{
"defaults": true,
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true
}
6 changes: 6 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8923/input.js
Original file line number Diff line number Diff line change
@@ -0,0 1,6 @@
"use strict";
const k = (() => {
let x = '';
x *= x-- / x;
return x;
})();
5 changes: 5 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8923/output.js
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
"use strict";
const k = (()=>{
let x = '';
return x * (x-- / x);
})();

0 comments on commit 4d4a7a9

Please sign in to comment.