-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(es/minifier): Prevent removing side effects from accessing getter (…
…#9530) Closes #9500 Caused by https://github.com/swc-project/swc/blob/c7fdd6b69b129a11465125d4e11a898326b7e884/crates/swc_ecma_minifier/src/compress/pure/misc.rs#L1547. When the object with getters pass to `self.ignore_return_value`, https://github.com/swc-project/swc/blob/c7fdd6b69b129a11465125d4e11a898326b7e884/crates/swc_ecma_minifier/src/compress/pure/misc.rs#L966 converts the object to `0` because the object is side-effect-free according to https://github.com/swc-project/swc/blob/c7fdd6b69b129a11465125d4e11a898326b7e884/crates/swc_ecma_utils/src/lib.rs#L1496 We should skip this process to fix the issue. As is known only accessing getters and setters may cause side effect, we can safely do the transformation when none of them appears in the object. More precision is possible if comparing the lit prop names. I also collect computed keys of getters and setters in the object, is there any bad case? The reason why only numeric (string) key removes the statement is that string key (`Computed`) is converted to `Ident` in other phases, e.g. `{}['a']` => `{}.a`, which does not matching the pattern.
- Loading branch information
Showing
6 changed files
with
95 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
swc_core: patch | ||
swc_ecma_minifier: patch | ||
--- | ||
|
||
fix(es/minifier): prevent removing side effects from accessing getter with numeric string key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
crates/swc_ecma_minifier/tests/fixture/issues/9500/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
let foo = 1; | ||
const obj = { | ||
get 1() { | ||
// same with get "1"() | ||
foo = 2; | ||
return 40; | ||
}, | ||
}; | ||
obj["1"]; // same with obj[1] | ||
console.log(foo); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
let foo = 1; | ||
({ | ||
get 1 () { | ||
return(// same with get "1"() | ||
foo = 2, 40); | ||
} | ||
})["1"], console.log(foo); |
3 changes: 3 additions & 0 deletions
3
crates/swc_ecma_minifier/tests/fixture/member_expr/undetermined_prop/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
({ | ||
a: 1, | ||
})[undetermined()]; |
1 change: 1 addition & 0 deletions
1
crates/swc_ecma_minifier/tests/fixture/member_expr/undetermined_prop/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
undetermined(); |