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.
This commit is contained in:
Yongwook Choi (Leo) 2022-11-29 16:01:30 +09:00 committed by GitHub
parent 6ca36c198b
commit 81224b5d67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -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::<u32>() {
self.changed = true;
report_change!("misc: Optimizing numeric property name");

View File

@ -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"]);
"###,
)
}