Skip to content

Commit 45f671d

Browse files
authored
fix(es/renamer): Fix renaming of default-exported declarations (#9135)
**Related issue:** - Closes #9129
1 parent 0a919ce commit 45f671d

File tree

22 files changed

+159
-10
lines changed

22 files changed

+159
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": true,
6+
"decorators": true
7+
},
8+
"target": "es2022",
9+
"transform": {
10+
"decoratorMetadata": true,
11+
"react": {
12+
"refresh": true,
13+
"development": true
14+
}
15+
},
16+
"externalHelpers": true,
17+
"loose": false,
18+
"minify": {
19+
"compress": false,
20+
"mangle": false
21+
}
22+
},
23+
"module": {
24+
"type": "es6"
25+
},
26+
"minify": false,
27+
"isModule": "unknown"
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eval('')
2+
export default function Foo() {
3+
}
4+
5+
Foo()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eval('')
2+
export function Foo() {
3+
}
4+
5+
Foo()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eval('')
2+
export default class Foo {
3+
}
4+
5+
new Foo()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eval('')
2+
export class Foo {
3+
}
4+
5+
new Foo()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
eval('');
2+
export default function Foo() {}
3+
_c = Foo;
4+
Foo();
5+
var _c;
6+
$RefreshReg$(_c, "Foo");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
eval('');
2+
export function Foo() {}
3+
_c = Foo;
4+
Foo();
5+
var _c;
6+
$RefreshReg$(_c, "Foo");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eval('');
2+
export default class Foo {
3+
}
4+
new Foo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eval('');
2+
export class Foo {
3+
}
4+
new Foo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": true,
6+
"decorators": true
7+
},
8+
"target": "es2022",
9+
"transform": {
10+
"decoratorMetadata": true,
11+
"react": {}
12+
},
13+
"externalHelpers": true,
14+
"loose": false,
15+
"minify": {
16+
"compress": false,
17+
"mangle": false
18+
}
19+
},
20+
"module": {
21+
"type": "es6"
22+
},
23+
"minify": false,
24+
"isModule": "unknown"
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eval('')
2+
export default function Foo() {
3+
}
4+
5+
Foo()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eval('')
2+
export function Foo() {
3+
}
4+
5+
Foo()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eval('')
2+
export default class Foo {
3+
}
4+
5+
new Foo()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eval('')
2+
export class Foo {
3+
}
4+
5+
new Foo()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eval('');
2+
export default function Foo() {}
3+
Foo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eval('');
2+
export function Foo() {}
3+
Foo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eval('');
2+
export default class Foo {
3+
}
4+
new Foo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eval('');
2+
export class Foo {
3+
}
4+
new Foo();
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function x() {
22
(class Baz {
33
});
4-
let Foo = class Foo {
5-
};
4+
class Foo {
5+
}
66
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
function foo() {
2-
let Bar = class Bar {
3-
};
2+
class Bar {
3+
}
44
}

crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl Visit for Analyzer {
218218
self.add_decl(id.to_id(), true);
219219
}
220220

221-
f.function.visit_with(self)
221+
f.visit_with(self);
222222
}
223223
DefaultDecl::TsInterfaceDecl(_) => {}
224224
}
@@ -240,7 +240,7 @@ impl Visit for Analyzer {
240240
maybe_grow_default(|| e.visit_children_with(self));
241241

242242
if let Expr::Ident(i) = e {
243-
self.add_usage(i.to_id())
243+
self.add_usage(i.to_id());
244244
}
245245

246246
self.is_pat_decl = old_is_pat_decl;

crates/swc_ecma_transforms_base/src/rename/mod.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ macro_rules! unit {
202202
} else {
203203
let map = self.get_map(n, false, false, false);
204204

205-
n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
205+
if !map.is_empty() {
206+
n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
207+
}
206208
}
207209
}
208210
};
@@ -214,7 +216,9 @@ macro_rules! unit {
214216
} else {
215217
let map = self.get_map(n, true, false, false);
216218

217-
n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
219+
if !map.is_empty() {
220+
n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
221+
}
218222
}
219223
}
220224
};
@@ -246,6 +250,20 @@ where
246250

247251
unit!(visit_mut_class_decl, ClassDecl, true);
248252

253+
fn visit_mut_default_decl(&mut self, n: &mut DefaultDecl) {
254+
match n {
255+
DefaultDecl::Class(n) => {
256+
n.visit_mut_children_with(self);
257+
}
258+
DefaultDecl::Fn(n) => {
259+
n.visit_mut_children_with(self);
260+
}
261+
DefaultDecl::TsInterfaceDecl(n) => {
262+
n.visit_mut_children_with(self);
263+
}
264+
}
265+
}
266+
249267
fn visit_mut_expr(&mut self, n: &mut Expr) {
250268
maybe_grow_default(|| n.visit_mut_children_with(self));
251269
}
@@ -284,7 +302,9 @@ where
284302
m.visit_mut_children_with(self);
285303
}
286304

287-
m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
305+
if !map.is_empty() {
306+
m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
307+
}
288308
}
289309

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

303-
m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
323+
if !map.is_empty() {
324+
m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
325+
}
304326
}
305327
}
306328

0 commit comments

Comments
 (0)