Skip to content

Commit abced23

Browse files
authoredNov 2, 2023
fix(es/typescript): Preserve const enum for named export (#8208)
**Related issue:** - Closes: #8204
1 parent f721814 commit abced23

11 files changed

+84
-17
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const enum Bar { VALUE = 1 }
2+
export default Bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var Bar;
2+
(function(Bar) {
3+
Bar[Bar["VALUE"] = 1] = "VALUE";
4+
})(Bar || (Bar = {}));
5+
export default Bar;

‎crates/swc/tests/tsc-references/enums.1.normal.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ var SyntaxKind;
55
SyntaxKind[SyntaxKind["ExportDeclaration"] = 1] = "ExportDeclaration";
66
})(SyntaxKind || (SyntaxKind = {}));
77
var SymbolFlags;
8+
(function(SymbolFlags) {
9+
SymbolFlags["Type"] = "Type";
10+
SymbolFlags["Value"] = "Value";
11+
})(SymbolFlags || (SymbolFlags = {}));
812
export { SymbolFlags };
913
//// [/b.ts]
1014
SyntaxKind.ImportClause;

‎crates/swc/tests/tsc-references/enums.2.minified.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//// [/a.ts]
2-
var SyntaxKind, SymbolFlags, SyntaxKind1;
3-
(SyntaxKind1 = SyntaxKind || (SyntaxKind = {}))[SyntaxKind1.ImportClause = 0] = "ImportClause", SyntaxKind1[SyntaxKind1.ExportDeclaration = 1] = "ExportDeclaration";
2+
var SyntaxKind, SymbolFlags, SyntaxKind1, SymbolFlags1;
3+
(SyntaxKind1 = SyntaxKind || (SyntaxKind = {}))[SyntaxKind1.ImportClause = 0] = "ImportClause", SyntaxKind1[SyntaxKind1.ExportDeclaration = 1] = "ExportDeclaration", (SymbolFlags1 = SymbolFlags || (SymbolFlags = {})).Type = "Type", SymbolFlags1.Value = "Value";
44
export { SymbolFlags };
55
//// [/b.ts]
66
SyntaxKind.ImportClause, SymbolFlags.Type;

‎crates/swc/tests/tsc-references/exportsAndImports1-amd.1.normal.js

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ define([
5656
E[E["C"] = 2] = "C";
5757
})(E || (E = {}));
5858
var D;
59+
(function(D) {
60+
D[D["A"] = 0] = "A";
61+
D[D["B"] = 1] = "B";
62+
D[D["C"] = 2] = "C";
63+
})(D || (D = {}));
5964
var M;
6065
(function(M) {})(M || (M = {}));
6166
var a = M.x;

‎crates/swc/tests/tsc-references/exportsAndImports1-amd.2.minified.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ define([
2020
return C;
2121
},
2222
D: function() {
23-
return D;
23+
return D1;
2424
},
2525
E: function() {
2626
return E1;
@@ -38,12 +38,12 @@ define([
3838
return v;
3939
}
4040
});
41-
var E, E1, D, M, v = 1;
41+
var E, D, E1, D1, M, v = 1;
4242
function f() {}
4343
var C = function C() {
4444
_class_call_check._(this, C);
4545
};
46-
(E = E1 || (E1 = {}))[E.A = 0] = "A", E[E.B = 1] = "B", E[E.C = 2] = "C", M || (M = {});
46+
(E = E1 || (E1 = {}))[E.A = 0] = "A", E[E.B = 1] = "B", E[E.C = 2] = "C", (D = D1 || (D1 = {}))[D.A = 0] = "A", D[D.B = 1] = "B", D[D.C = 2] = "C", M || (M = {});
4747
var a = M.x;
4848
});
4949
//// [t2.ts]

‎crates/swc/tests/tsc-references/exportsAndImports1-es6.1.normal.js

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ var E;
4545
E[E["C"] = 2] = "C";
4646
})(E || (E = {}));
4747
var D;
48+
(function(D) {
49+
D[D["A"] = 0] = "A";
50+
D[D["B"] = 1] = "B";
51+
D[D["C"] = 2] = "C";
52+
})(D || (D = {}));
4853
var M;
4954
(function(M) {})(M || (M = {}));
5055
const a = M.x;

‎crates/swc/tests/tsc-references/exportsAndImports1-es6.2.minified.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", {
1212
return C;
1313
},
1414
D: function() {
15-
return D;
15+
return D1;
1616
},
1717
E: function() {
1818
return E1;
@@ -30,11 +30,11 @@ Object.defineProperty(exports, "__esModule", {
3030
return v;
3131
}
3232
});
33-
var E, E1, D, M, v = 1;
33+
var E, D, E1, D1, M, v = 1;
3434
function f() {}
3535
class C {
3636
}
37-
(E = E1 || (E1 = {}))[E.A = 0] = "A", E[E.B = 1] = "B", E[E.C = 2] = "C", M || (M = {});
37+
(E = E1 || (E1 = {}))[E.A = 0] = "A", E[E.B = 1] = "B", E[E.C = 2] = "C", (D = D1 || (D1 = {}))[D.A = 0] = "A", D[D.B = 1] = "B", D[D.C = 2] = "C", M || (M = {});
3838
const a = M.x;
3939
//// [t2.ts]
4040
Object.defineProperty(exports, "__esModule", {

‎crates/swc/tests/tsc-references/exportsAndImports1.1.normal.js

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ var E;
4848
E[E["C"] = 2] = "C";
4949
})(E || (E = {}));
5050
var D;
51+
(function(D) {
52+
D[D["A"] = 0] = "A";
53+
D[D["B"] = 1] = "B";
54+
D[D["C"] = 2] = "C";
55+
})(D || (D = {}));
5156
var M;
5257
(function(M) {})(M || (M = {}));
5358
var a = M.x;

‎crates/swc/tests/tsc-references/exportsAndImports1.2.minified.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", {
1212
return C;
1313
},
1414
D: function() {
15-
return D;
15+
return D1;
1616
},
1717
E: function() {
1818
return E1;
@@ -30,12 +30,12 @@ Object.defineProperty(exports, "__esModule", {
3030
return v;
3131
}
3232
});
33-
var E, E1, D, M, _class_call_check = require("@swc/helpers/_/_class_call_check"), v = 1;
33+
var E, D, E1, D1, M, _class_call_check = require("@swc/helpers/_/_class_call_check"), v = 1;
3434
function f() {}
3535
var C = function C() {
3636
_class_call_check._(this, C);
3737
};
38-
(E = E1 || (E1 = {}))[E.A = 0] = "A", E[E.B = 1] = "B", E[E.C = 2] = "C", M || (M = {});
38+
(E = E1 || (E1 = {}))[E.A = 0] = "A", E[E.B = 1] = "B", E[E.C = 2] = "C", (D = D1 || (D1 = {}))[D.A = 0] = "A", D[D.B = 1] = "B", D[D.C = 2] = "C", M || (M = {});
3939
var a = M.x;
4040
//// [t2.ts]
4141
Object.defineProperty(exports, "__esModule", {

‎crates/swc_ecma_transforms_typescript/src/transform.rs

+47-6
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,18 @@ impl Transform {
287287
}
288288

289289
let id = stmt.get_decl_id();
290-
stmt = self.fold_stmt(stmt);
290+
stmt = self.fold_stmt(stmt, false);
291291
decl_id_record.extend(id);
292292
if !stmt.is_empty() {
293293
n.push(stmt);
294294
}
295295
}
296296
}
297297

298-
fn fold_stmt(&mut self, n: Stmt) -> Stmt {
298+
fn fold_stmt(&mut self, n: Stmt, is_export: bool) -> Stmt {
299299
match n {
300300
Stmt::Decl(Decl::TsModule(ts_module)) => self.transform_ts_module(*ts_module, false),
301-
Stmt::Decl(Decl::TsEnum(ts_enum)) => self.transform_ts_enum(*ts_enum, false),
301+
Stmt::Decl(Decl::TsEnum(ts_enum)) => self.transform_ts_enum(*ts_enum, is_export),
302302
stmt => stmt,
303303
}
304304
}
@@ -312,6 +312,8 @@ impl Transform {
312312
return;
313313
}
314314

315+
let export_names = self.collect_named_export(n);
316+
315317
let mut decl_id_record = AHashSet::<Id>::default();
316318

317319
for mut module_item in n.take().drain(..) {
@@ -342,15 +344,19 @@ impl Transform {
342344
}
343345

344346
let id = module_item.get_decl_id();
345-
module_item = self.fold_module_item(module_item);
347+
let is_export = id
348+
.as_ref()
349+
.map(|id| export_names.contains(id))
350+
.unwrap_or_default();
351+
module_item = self.fold_module_item(module_item, is_export);
346352
decl_id_record.extend(id);
347353
if !matches!(module_item, ModuleItem::Stmt(Stmt::Empty(..))) {
348354
n.push(module_item);
349355
}
350356
}
351357
}
352358

353-
fn fold_module_item(&mut self, n: ModuleItem) -> ModuleItem {
359+
fn fold_module_item(&mut self, n: ModuleItem, is_export: bool) -> ModuleItem {
354360
match n {
355361
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
356362
decl: Decl::TsModule(ts_module),
@@ -360,7 +366,7 @@ impl Transform {
360366
decl: Decl::TsEnum(ts_enum),
361367
..
362368
})) => self.transform_ts_enum(*ts_enum, true).into(),
363-
ModuleItem::Stmt(stmt @ Stmt::Decl(..)) => self.fold_stmt(stmt).into(),
369+
ModuleItem::Stmt(stmt @ Stmt::Decl(..)) => self.fold_stmt(stmt, is_export).into(),
364370
module_item => module_item,
365371
}
366372
}
@@ -879,6 +885,41 @@ impl Transform {
879885

880886
Factory::function(vec![ident.into()], body).as_call(DUMMY_SP, vec![init_arg.into()])
881887
}
888+
889+
fn collect_named_export(&self, n: &[ModuleItem]) -> AHashSet<Id> {
890+
let mut names = AHashSet::default();
891+
892+
if self.namespace_id.is_some() {
893+
return names;
894+
}
895+
896+
for item in n {
897+
match item {
898+
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
899+
specifiers,
900+
src: None,
901+
..
902+
})) => {
903+
names.extend(specifiers.iter().map(|s| match s {
904+
ExportSpecifier::Named(ExportNamedSpecifier {
905+
orig: ModuleExportName::Ident(export_id),
906+
..
907+
}) => export_id.to_id(),
908+
_ => unreachable!("only named export is allowed for src = None"),
909+
}));
910+
}
911+
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(ExportDefaultExpr {
912+
expr,
913+
..
914+
})) if expr.is_ident() => {
915+
names.extend(expr.as_ident().map(Ident::to_id));
916+
}
917+
_ => {}
918+
}
919+
}
920+
921+
names
922+
}
882923
}
883924

884925
impl Transform {

0 commit comments

Comments
 (0)
Please sign in to comment.