From 8f683e3f77fe9f4fd84a5bf64c067ae9526fb330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 13 Jan 2023 15:55:04 +0900 Subject: [PATCH] feat(es/minifier): Remove noop spreads (#6803) **Related issue:** - Closes https://github.com/swc-project/swc/issues/6788. --- crates/swc_ecma_minifier/src/compress/pure/mod.rs | 11 +++++++++++ .../tests/pass-1/issue-6788/1/input.js | 4 ++++ .../tests/pass-1/issue-6788/1/output.js | 1 + 3 files changed, 16 insertions(+) create mode 100644 crates/swc_ecma_minifier/tests/pass-1/issue-6788/1/input.js create mode 100644 crates/swc_ecma_minifier/tests/pass-1/issue-6788/1/output.js diff --git a/crates/swc_ecma_minifier/src/compress/pure/mod.rs b/crates/swc_ecma_minifier/src/compress/pure/mod.rs index bba00a7f202..88cc235051f 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/mod.rs @@ -12,6 +12,7 @@ use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith}; use tracing::{debug, span, Level}; use self::{ctx::Ctx, misc::DropOpts}; +use super::util::is_pure_undefined_or_null; #[cfg(feature = "debug")] use crate::debug::dump; use crate::{ @@ -760,6 +761,16 @@ impl VisitMut for Pure<'_> { fn visit_mut_prop_or_spreads(&mut self, exprs: &mut Vec) { self.visit_par(exprs); + + exprs.retain(|e| { + if let PropOrSpread::Spread(spread) = e { + if is_pure_undefined_or_null(&self.expr_ctx, &spread.expr) { + return false; + } + } + + true + }) } fn visit_mut_return_stmt(&mut self, s: &mut ReturnStmt) { diff --git a/crates/swc_ecma_minifier/tests/pass-1/issue-6788/1/input.js b/crates/swc_ecma_minifier/tests/pass-1/issue-6788/1/input.js new file mode 100644 index 00000000000..46ea2c7eee4 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/pass-1/issue-6788/1/input.js @@ -0,0 +1,4 @@ +const foo = { ...(null && {}) }; +const bar = { ...null }; + +console.log(foo, bar); diff --git a/crates/swc_ecma_minifier/tests/pass-1/issue-6788/1/output.js b/crates/swc_ecma_minifier/tests/pass-1/issue-6788/1/output.js new file mode 100644 index 00000000000..555a3c0eadb --- /dev/null +++ b/crates/swc_ecma_minifier/tests/pass-1/issue-6788/1/output.js @@ -0,0 +1 @@ +console.log({}, {});