Skip to content

Commit

Permalink
fix(es/renamer): Check preserved in normal renaming mode (#9666)
Browse files Browse the repository at this point in the history
**Description:**

Check `preserved` in normal renaming mode

**Related issue:**

 - Closes #9663
  • Loading branch information
unbyte authored Oct 22, 2024
1 parent d38f9fc commit 87b4e10
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/sixty-ladybugs-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_transforms_base: patch
---

fix(es/renamer): check `preserved` in normal renaming mode
9 changes: 9 additions & 0 deletions crates/swc/tests/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,15 @@ fn issue_1203() {
assert!(!f.contains("return //"))
}

#[test]
fn issue_9663() {
let f = file("tests/projects/issue-9663/input.js").unwrap();
println!("{}", f);

assert!(f.contains("set = Reflect.set"));
assert!(!f.contains("function set1("));
}

#[test]
fn codegen_1() {
let f = file("tests/projects/codegen-1/input.js").unwrap();
Expand Down
9 changes: 9 additions & 0 deletions crates/swc/tests/projects/issue-9663/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"externalHelpers": false,
"target": "es5"
}
}
15 changes: 15 additions & 0 deletions crates/swc/tests/projects/issue-9663/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class A {
world = false
}

eval("hello")

class B extends A {
set hello(v) {
super.world = v
}

get hello() {
return super.world
}
}
10 changes: 9 additions & 1 deletion crates/swc_ecma_transforms_base/src/rename/analyzer/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl Scope {
to: &mut RenameMap,
previous: &RenameMap,
reverse: &mut ReverseMap,
preserved: &FxHashSet<Id>,
preserved_symbols: &FxHashSet<Atom>,
) where
R: Renamer,
Expand All @@ -113,6 +114,7 @@ impl Scope {
previous,
reverse,
queue,
preserved,
preserved_symbols,
);

Expand All @@ -122,6 +124,7 @@ impl Scope {
to,
&Default::default(),
reverse,
preserved,
preserved_symbols,
);
}
Expand All @@ -134,14 +137,19 @@ impl Scope {
previous: &RenameMap,
reverse: &mut ReverseMap,
queue: Vec<Id>,
preserved: &FxHashSet<Id>,
preserved_symbols: &FxHashSet<Atom>,
) where
R: Renamer,
{
let mut n = 0;

for id in queue {
if to.get(&id).is_some() || previous.get(&id).is_some() || id.0 == "eval" {
if preserved.contains(&id)
|| to.get(&id).is_some()
|| previous.get(&id).is_some()
|| id.0 == "eval"
{
continue;
}

Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_base/src/rename/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ where
&mut map,
&self.previous_cache,
&mut Default::default(),
&self.preserved,
&unresolved,
);
}
Expand Down

0 comments on commit 87b4e10

Please sign in to comment.