diff --git a/.changeset/real-bugs-double.md b/.changeset/real-bugs-double.md new file mode 100644 index 000000000000..9d0320d1c2b6 --- /dev/null +++ b/.changeset/real-bugs-double.md @@ -0,0 +1,6 @@ +--- +swc_ecma_transforms_optimization: patch +swc_core: patch +--- + +fix(es/minifier): Iterate object properties in reverse direction while inlining property access diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index 4d4acb59c1ea..750c79b59493 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -11364,3 +11364,14 @@ fn issue_9499() { fn issue_9356() { run_default_exec_test("console.log((function ({ } = 42) { }).length)"); } + +#[test] +fn isssue_9498() { + run_default_exec_test( + " + const x = {a: 1}; + const y = {...x, a: 2}; + console.log(y.a); +", + ) +} diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs index 4ec5016bcb22..289cdcaf47f5 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs @@ -1734,7 +1734,7 @@ fn get_key_value(key: &str, props: &mut Vec) -> Option> return None; } - for (i, prop) in props.iter_mut().enumerate() { + for (i, prop) in props.iter_mut().enumerate().rev() { let prop = match prop { PropOrSpread::Prop(x) => &mut **x, PropOrSpread::Spread(_) => unreachable!(),