Skip to content

Commit 111de26

Browse files
authored
fix(es/minifier): Abort seq inliner if b can short-circuit (#8128)
**Related issue:** - Closes #8119
1 parent 736831c commit 111de26

File tree

13 files changed

+464
-3
lines changed

13 files changed

+464
-3
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "ecmascript",
5+
"jsx": false
6+
},
7+
"target": "es2022",
8+
"loose": false,
9+
"minify": {
10+
"compress": {
11+
"arguments": false,
12+
"arrows": true,
13+
"booleans": true,
14+
"booleans_as_integers": false,
15+
"collapse_vars": true,
16+
"comparisons": true,
17+
"computed_props": true,
18+
"conditionals": true,
19+
"dead_code": true,
20+
"directives": true,
21+
"drop_console": false,
22+
"drop_debugger": true,
23+
"evaluate": true,
24+
"expression": false,
25+
"hoist_funs": false,
26+
"hoist_props": true,
27+
"hoist_vars": false,
28+
"if_return": true,
29+
"join_vars": true,
30+
"keep_classnames": false,
31+
"keep_fargs": true,
32+
"keep_fnames": false,
33+
"keep_infinity": false,
34+
"loops": true,
35+
"negate_iife": true,
36+
"properties": true,
37+
"reduce_funcs": false,
38+
"reduce_vars": false,
39+
"side_effects": true,
40+
"switches": true,
41+
"typeofs": true,
42+
"unsafe": false,
43+
"unsafe_arrows": false,
44+
"unsafe_comps": false,
45+
"unsafe_Function": false,
46+
"unsafe_math": false,
47+
"unsafe_symbols": false,
48+
"unsafe_methods": false,
49+
"unsafe_proto": false,
50+
"unsafe_regexp": false,
51+
"unsafe_undefined": false,
52+
"unused": true,
53+
"const_to_let": true,
54+
"pristine_globals": true,
55+
"passes": 2
56+
},
57+
"mangle": false
58+
}
59+
},
60+
"module": {
61+
"type": "es6"
62+
},
63+
"minify": false,
64+
"isModule": true
65+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const myArr = [];
2+
// function with side effect
3+
function foo(arr) {
4+
arr.push('foo');
5+
return 'foo';
6+
}
7+
let a;
8+
9+
if (Math.random() > 1.00000) {
10+
a = true;
11+
}
12+
13+
// the function call below should always run
14+
// regardless of whether `a` is `undefined`
15+
let b = foo(myArr);
16+
17+
// const seems to keep this line here instead of
18+
// moving it behind the logitcal nullish assignment
19+
// const b = foo(myArr);
20+
21+
a ??= b;
22+
23+
console.log(a);
24+
console.log(myArr);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const myArr = [];
2+
// function with side effect
3+
function foo(arr) {
4+
arr.push('foo');
5+
return 'foo';
6+
}
7+
let a;
8+
9+
if (Math.random() > -0.1) {
10+
a = true;
11+
}
12+
13+
// the function call below should always run
14+
// regardless of whether `a` is `undefined`
15+
let b = foo(myArr);
16+
17+
// const seems to keep this line here instead of
18+
// moving it behind the logitcal nullish assignment
19+
// const b = foo(myArr);
20+
21+
a ??= b;
22+
23+
console.log(a);
24+
console.log(myArr);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "ecmascript",
5+
"jsx": false
6+
},
7+
"target": "es2022",
8+
"loose": false,
9+
"minify": {
10+
"compress": {
11+
"arguments": false,
12+
"arrows": true,
13+
"booleans": true,
14+
"booleans_as_integers": false,
15+
"collapse_vars": true,
16+
"comparisons": true,
17+
"computed_props": true,
18+
"conditionals": true,
19+
"dead_code": true,
20+
"directives": true,
21+
"drop_console": false,
22+
"drop_debugger": true,
23+
"evaluate": true,
24+
"expression": false,
25+
"hoist_funs": false,
26+
"hoist_props": true,
27+
"hoist_vars": false,
28+
"if_return": true,
29+
"join_vars": true,
30+
"keep_classnames": false,
31+
"keep_fargs": true,
32+
"keep_fnames": false,
33+
"keep_infinity": false,
34+
"loops": true,
35+
"negate_iife": true,
36+
"properties": true,
37+
"reduce_funcs": false,
38+
"reduce_vars": false,
39+
"side_effects": true,
40+
"switches": true,
41+
"typeofs": true,
42+
"unsafe": false,
43+
"unsafe_arrows": false,
44+
"unsafe_comps": false,
45+
"unsafe_Function": false,
46+
"unsafe_math": false,
47+
"unsafe_symbols": false,
48+
"unsafe_methods": false,
49+
"unsafe_proto": false,
50+
"unsafe_regexp": false,
51+
"unsafe_undefined": false,
52+
"unused": true,
53+
"const_to_let": true,
54+
"pristine_globals": true,
55+
"passes": 2
56+
},
57+
"mangle": false
58+
}
59+
},
60+
"module": {
61+
"type": "es6"
62+
},
63+
"minify": false,
64+
"isModule": true
65+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const myArr = [];
2+
// function with side effect
3+
function foo(arr) {
4+
arr.push('foo');
5+
return 'foo';
6+
}
7+
let a;
8+
9+
if (Math.random() > 0.5) {
10+
a = true;
11+
}
12+
13+
// the function call below should always run
14+
// regardless of whether `a` is `undefined`
15+
let b = foo(myArr);
16+
17+
// const seems to keep this line here instead of
18+
// moving it behind the logitcal nullish assignment
19+
// const b = foo(myArr);
20+
21+
a ??= b;
22+
23+
console.log(a);
24+
console.log(myArr);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let a;
2+
const myArr = [];
3+
Math.random() > 0.5 && (a = !0);
4+
// the function call below should always run
5+
// regardless of whether `a` is `undefined`
6+
let b = (myArr.push('foo'), 'foo');
7+
console.log(// const seems to keep this line here instead of
8+
// moving it behind the logitcal nullish assignment
9+
// const b = foo(myArr);
10+
a ??= b), console.log(myArr);

crates/swc_ecma_minifier/src/compress/optimize/sequences.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,10 @@ impl Optimizer<'_> {
24892489
/// 1, arr[i]`
24902490
//
24912491
fn should_not_check_rhs_of_assign(&self, a: &Mergable, b: &mut AssignExpr) -> Result<bool, ()> {
2492+
if b.op.may_short_circuit() {
2493+
return Ok(true);
2494+
}
2495+
24922496
if let Some(a_id) = a.id() {
24932497
match a {
24942498
Mergable::Expr(Expr::Assign(AssignExpr { op: op!("="), .. })) => {}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy