Skip to content

Commit

Permalink
fix(es/renamer): Fix renaming of default-exported declarations (#9135)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9129
  • Loading branch information
kdy1 authored Jul 4, 2024
1 parent 0a919ce commit 45f671d
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 10 deletions.
28 changes: 28 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": true
},
"target": "es2022",
"transform": {
"decoratorMetadata": true,
"react": {
"refresh": true,
"development": true
}
},
"externalHelpers": true,
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": "unknown"
}
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/1/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eval('')
export default function Foo() {
}

Foo()
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/1/input/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eval('')
export function Foo() {
}

Foo()
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/1/input/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eval('')
export default class Foo {
}

new Foo()
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/1/input/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eval('')
export class Foo {
}

new Foo()
6 changes: 6 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/1/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
eval('');
export default function Foo() {}
_c = Foo;
Foo();
var _c;
$RefreshReg$(_c, "Foo");
6 changes: 6 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/1/output/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
eval('');
export function Foo() {}
_c = Foo;
Foo();
var _c;
$RefreshReg$(_c, "Foo");
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/1/output/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eval('');
export default class Foo {
}
new Foo();
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/1/output/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eval('');
export class Foo {
}
new Foo();
25 changes: 25 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/2/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": true
},
"target": "es2022",
"transform": {
"decoratorMetadata": true,
"react": {}
},
"externalHelpers": true,
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": "unknown"
}
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/2/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eval('')
export default function Foo() {
}

Foo()
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/2/input/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eval('')
export function Foo() {
}

Foo()
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/2/input/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eval('')
export default class Foo {
}

new Foo()
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/2/input/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eval('')
export class Foo {
}

new Foo()
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/2/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eval('');
export default function Foo() {}
Foo();
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/2/output/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eval('');
export function Foo() {}
Foo();
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/2/output/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eval('');
export default class Foo {
}
new Foo();
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9129/2/output/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eval('');
export class Foo {
}
new Foo();
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function x() {
(class Baz {
});
let Foo = class Foo {
};
class Foo {
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function foo() {
let Bar = class Bar {
};
class Bar {
}
}
4 changes: 2 additions & 2 deletions crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl Visit for Analyzer {
self.add_decl(id.to_id(), true);
}

f.function.visit_with(self)
f.visit_with(self);
}
DefaultDecl::TsInterfaceDecl(_) => {}
}
Expand All @@ -240,7 +240,7 @@ impl Visit for Analyzer {
maybe_grow_default(|| e.visit_children_with(self));

if let Expr::Ident(i) = e {
self.add_usage(i.to_id())
self.add_usage(i.to_id());
}

self.is_pat_decl = old_is_pat_decl;
Expand Down
30 changes: 26 additions & 4 deletions crates/swc_ecma_transforms_base/src/rename/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ macro_rules! unit {
} else {
let map = self.get_map(n, false, false, false);

n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
if !map.is_empty() {
n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}
}
}
};
Expand All @@ -214,7 +216,9 @@ macro_rules! unit {
} else {
let map = self.get_map(n, true, false, false);

n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
if !map.is_empty() {
n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}
}
}
};
Expand Down Expand Up @@ -246,6 +250,20 @@ where

unit!(visit_mut_class_decl, ClassDecl, true);

fn visit_mut_default_decl(&mut self, n: &mut DefaultDecl) {
match n {
DefaultDecl::Class(n) => {
n.visit_mut_children_with(self);
}
DefaultDecl::Fn(n) => {
n.visit_mut_children_with(self);
}
DefaultDecl::TsInterfaceDecl(n) => {
n.visit_mut_children_with(self);
}
}
}

fn visit_mut_expr(&mut self, n: &mut Expr) {
maybe_grow_default(|| n.visit_mut_children_with(self));
}
Expand Down Expand Up @@ -284,7 +302,9 @@ where
m.visit_mut_children_with(self);
}

m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
if !map.is_empty() {
m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}
}

fn visit_mut_script(&mut self, m: &mut Script) {
Expand All @@ -300,7 +320,9 @@ where
m.visit_mut_children_with(self);
}

m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
if !map.is_empty() {
m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}
}
}

Expand Down

0 comments on commit 45f671d

Please sign in to comment.