diff --git a/crates/swc_ecma_minifier/src/compress/pure/dead_code.rs b/crates/swc_ecma_minifier/src/compress/pure/dead_code.rs index c42f7819a720..a85eca749d6d 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/dead_code.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/dead_code.rs @@ -269,15 +269,7 @@ impl Pure<'_> { ); } - match stmts[idx].as_stmt() { - Some(Stmt::Return(ReturnStmt { arg: None, .. })) => { - // Exclude return - new_stmts.extend(stmts.drain(..idx)); - } - _ => { - new_stmts.extend(stmts.drain(..=idx)); - } - } + new_stmts.extend(stmts.drain(..=idx)); new_stmts.extend(hoisted_fns); diff --git a/crates/swc_ecma_minifier/tests/pass-1/issue-6405/1/input.js b/crates/swc_ecma_minifier/tests/pass-1/issue-6405/1/input.js new file mode 100644 index 000000000000..8a499b96319a --- /dev/null +++ b/crates/swc_ecma_minifier/tests/pass-1/issue-6405/1/input.js @@ -0,0 +1,14 @@ +export const fn = () => { + let val; + + if (!val) { + return undefined; + // works as expected if comment out below line + throw new Error('first'); + } + + if (val.a?.b !== true) { // Uncaught TypeError: Cannot read properties of undefined (reading 'a') + throw new Error('second'); + } + return val; +} \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/pass-1/issue-6405/1/output.js b/crates/swc_ecma_minifier/tests/pass-1/issue-6405/1/output.js new file mode 100644 index 000000000000..c81cb88a27ad --- /dev/null +++ b/crates/swc_ecma_minifier/tests/pass-1/issue-6405/1/output.js @@ -0,0 +1,7 @@ +export const fn = ()=>{ + let val; + if (!!val) { + if (val.a?.b !== !0) throw Error('second'); + return val; + } +};