From e4e4d6cf6bbc2645272d590cd6ae83aeb5c19b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 14 Dec 2022 13:45:47 +0900 Subject: [PATCH] fix(es/minifier): Abort sequential inliner on optional chaining (#6637) **Related issue:** - Closes https://github.com/swc-project/swc/issues/6636. --- .../swc_ecma_minifier/src/compress/optimize/sequences.rs | 4 +++- .../tests/fixture/issues/6636/config.json | 7 +++++++ .../swc_ecma_minifier/tests/fixture/issues/6636/input.js | 8 ++++++++ .../swc_ecma_minifier/tests/fixture/issues/6636/output.js | 4 ++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/6636/config.json create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/6636/input.js create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/6636/output.js diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index d91e348f53a..d24c59c6d53 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -1546,7 +1546,9 @@ where } match b { - Expr::Update(..) | Expr::Arrow(..) | Expr::Fn(..) => return Ok(false), + Expr::Update(..) | Expr::Arrow(..) | Expr::Fn(..) | Expr::OptChain(..) => { + return Ok(false) + } Expr::Cond(b) => { trace_op!("seq: Try test of cond"); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6636/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/6636/config.json new file mode 100644 index 00000000000..fbb662f8d15 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6636/config.json @@ -0,0 +1,7 @@ +{ + "passes": 2, + "toplevel": true, + "defaults": true, + "sequences": true, + "collapse_vars": true +} diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6636/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/6636/input.js new file mode 100644 index 00000000000..4317ff13ac0 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6636/input.js @@ -0,0 +1,8 @@ +export function memo(fn, opts) { + let result; + return () => { + result = fn(...newDeps); + opts?.onChange?.(result); + return result; + }; +} \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6636/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/6636/output.js new file mode 100644 index 00000000000..16e888beb4d --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6636/output.js @@ -0,0 +1,4 @@ +export function memo(fn, opts) { + let result; + return ()=>(result = fn(...newDeps), opts?.onChange?.(result), result); +}