From 81224b5d67a92c7154a870f07f28d9802be2106e Mon Sep 17 00:00:00 2001 From: "Yongwook Choi (Leo)" Date: Tue, 29 Nov 2022 16:01:30 +0900 Subject: [PATCH] fix(es/minifier): Don't convert a signed integer literal key to a numeric literal (#6529) **Related issue:** - Closes https://github.com/swc-project/swc/issues/6528. --- .../src/compress/pure/properties.rs | 2 +- crates/swc_ecma_minifier/tests/exec.rs | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/pure/properties.rs b/crates/swc_ecma_minifier/src/compress/pure/properties.rs index 2a7bad6b5637..2388f6debfe5 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/properties.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/properties.rs @@ -95,7 +95,7 @@ impl Pure<'_> { return; } - if !s.value.starts_with('0') || s.value.len() <= 1 { + if (!s.value.starts_with('0') && !s.value.starts_with('+')) || s.value.len() <= 1 { if let Ok(v) = s.value.parse::() { self.changed = true; report_change!("misc: Optimizing numeric property name"); diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index 5508fa9fc641..a00ad7941c36 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -10374,3 +10374,23 @@ fn issue_6463_1() { "###, ); } + +#[test] +fn issue_6528() { + run_default_exec_test( + r###" + const foo = { + "+1": 1, + "2": 2, + "-3": 3, + } + + console.log(foo[1]); + console.log(foo["+1"]); + console.log(foo["2"]); + console.log(foo[2]); + console.log(foo[-3]); + console.log(foo["-3"]); + "###, + ) +}