Skip to content

Commit

Permalink
fix(es/typescript): Preserve const enum for named export (#8208)
Browse files Browse the repository at this point in the history
**Related issue:**

- Closes: #8204
  • Loading branch information
magic-akari committed Nov 2, 2023
1 parent f721814 commit abced23
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 17 deletions.
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8204/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const enum Bar { VALUE = 1 }
export default Bar;
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8204/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var Bar;
(function(Bar) {
Bar[Bar["VALUE"] = 1] = "VALUE";
})(Bar || (Bar = {}));
export default Bar;
4 changes: 4 additions & 0 deletions crates/swc/tests/tsc-references/enums.1.normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ var SyntaxKind;
SyntaxKind[SyntaxKind["ExportDeclaration"] = 1] = "ExportDeclaration";
})(SyntaxKind || (SyntaxKind = {}));
var SymbolFlags;
(function(SymbolFlags) {
SymbolFlags["Type"] = "Type";
SymbolFlags["Value"] = "Value";
})(SymbolFlags || (SymbolFlags = {}));
export { SymbolFlags };
//// [/b.ts]
SyntaxKind.ImportClause;
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/tsc-references/enums.2.minified.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//// [/a.ts]
var SyntaxKind, SymbolFlags, SyntaxKind1;
(SyntaxKind1 = SyntaxKind || (SyntaxKind = {}))[SyntaxKind1.ImportClause = 0] = "ImportClause", SyntaxKind1[SyntaxKind1.ExportDeclaration = 1] = "ExportDeclaration";
var SyntaxKind, SymbolFlags, SyntaxKind1, SymbolFlags1;
(SyntaxKind1 = SyntaxKind || (SyntaxKind = {}))[SyntaxKind1.ImportClause = 0] = "ImportClause", SyntaxKind1[SyntaxKind1.ExportDeclaration = 1] = "ExportDeclaration", (SymbolFlags1 = SymbolFlags || (SymbolFlags = {})).Type = "Type", SymbolFlags1.Value = "Value";
export { SymbolFlags };
//// [/b.ts]
SyntaxKind.ImportClause, SymbolFlags.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ define([
E[E["C"] = 2] = "C";
})(E || (E = {}));
var D;
(function(D) {
D[D["A"] = 0] = "A";
D[D["B"] = 1] = "B";
D[D["C"] = 2] = "C";
})(D || (D = {}));
var M;
(function(M) {})(M || (M = {}));
var a = M.x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ define([
return C;
},
D: function() {
return D;
return D1;
},
E: function() {
return E1;
Expand All @@ -38,12 +38,12 @@ define([
return v;
}
});
var E, E1, D, M, v = 1;
var E, D, E1, D1, M, v = 1;
function f() {}
var C = function C() {
_class_call_check._(this, C);
};
(E = E1 || (E1 = {}))[E.A = 0] = "A", E[E.B = 1] = "B", E[E.C = 2] = "C", M || (M = {});
(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 = {});
var a = M.x;
});
//// [t2.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ var E;
E[E["C"] = 2] = "C";
})(E || (E = {}));
var D;
(function(D) {
D[D["A"] = 0] = "A";
D[D["B"] = 1] = "B";
D[D["C"] = 2] = "C";
})(D || (D = {}));
var M;
(function(M) {})(M || (M = {}));
const a = M.x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", {
return C;
},
D: function() {
return D;
return D1;
},
E: function() {
return E1;
Expand All @@ -30,11 +30,11 @@ Object.defineProperty(exports, "__esModule", {
return v;
}
});
var E, E1, D, M, v = 1;
var E, D, E1, D1, M, v = 1;
function f() {}
class C {
}
(E = E1 || (E1 = {}))[E.A = 0] = "A", E[E.B = 1] = "B", E[E.C = 2] = "C", M || (M = {});
(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 = {});
const a = M.x;
//// [t2.ts]
Object.defineProperty(exports, "__esModule", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ var E;
E[E["C"] = 2] = "C";
})(E || (E = {}));
var D;
(function(D) {
D[D["A"] = 0] = "A";
D[D["B"] = 1] = "B";
D[D["C"] = 2] = "C";
})(D || (D = {}));
var M;
(function(M) {})(M || (M = {}));
var a = M.x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", {
return C;
},
D: function() {
return D;
return D1;
},
E: function() {
return E1;
Expand All @@ -30,12 +30,12 @@ Object.defineProperty(exports, "__esModule", {
return v;
}
});
var E, E1, D, M, _class_call_check = require("@swc/helpers/_/_class_call_check"), v = 1;
var E, D, E1, D1, M, _class_call_check = require("@swc/helpers/_/_class_call_check"), v = 1;
function f() {}
var C = function C() {
_class_call_check._(this, C);
};
(E = E1 || (E1 = {}))[E.A = 0] = "A", E[E.B = 1] = "B", E[E.C = 2] = "C", M || (M = {});
(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 = {});
var a = M.x;
//// [t2.ts]
Object.defineProperty(exports, "__esModule", {
Expand Down
53 changes: 47 additions & 6 deletions crates/swc_ecma_transforms_typescript/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,18 @@ impl Transform {
}

let id = stmt.get_decl_id();
stmt = self.fold_stmt(stmt);
stmt = self.fold_stmt(stmt, false);
decl_id_record.extend(id);
if !stmt.is_empty() {
n.push(stmt);
}
}
}

fn fold_stmt(&mut self, n: Stmt) -> Stmt {
fn fold_stmt(&mut self, n: Stmt, is_export: bool) -> Stmt {
match n {
Stmt::Decl(Decl::TsModule(ts_module)) => self.transform_ts_module(*ts_module, false),
Stmt::Decl(Decl::TsEnum(ts_enum)) => self.transform_ts_enum(*ts_enum, false),
Stmt::Decl(Decl::TsEnum(ts_enum)) => self.transform_ts_enum(*ts_enum, is_export),
stmt => stmt,
}
}
Expand All @@ -312,6 +312,8 @@ impl Transform {
return;
}

let export_names = self.collect_named_export(n);

let mut decl_id_record = AHashSet::<Id>::default();

for mut module_item in n.take().drain(..) {
Expand Down Expand Up @@ -342,15 +344,19 @@ impl Transform {
}

let id = module_item.get_decl_id();
module_item = self.fold_module_item(module_item);
let is_export = id
.as_ref()
.map(|id| export_names.contains(id))
.unwrap_or_default();
module_item = self.fold_module_item(module_item, is_export);
decl_id_record.extend(id);
if !matches!(module_item, ModuleItem::Stmt(Stmt::Empty(..))) {
n.push(module_item);
}
}
}

fn fold_module_item(&mut self, n: ModuleItem) -> ModuleItem {
fn fold_module_item(&mut self, n: ModuleItem, is_export: bool) -> ModuleItem {
match n {
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
decl: Decl::TsModule(ts_module),
Expand All @@ -360,7 +366,7 @@ impl Transform {
decl: Decl::TsEnum(ts_enum),
..
})) => self.transform_ts_enum(*ts_enum, true).into(),
ModuleItem::Stmt(stmt @ Stmt::Decl(..)) => self.fold_stmt(stmt).into(),
ModuleItem::Stmt(stmt @ Stmt::Decl(..)) => self.fold_stmt(stmt, is_export).into(),
module_item => module_item,
}
}
Expand Down Expand Up @@ -879,6 +885,41 @@ impl Transform {

Factory::function(vec![ident.into()], body).as_call(DUMMY_SP, vec![init_arg.into()])
}

fn collect_named_export(&self, n: &[ModuleItem]) -> AHashSet<Id> {
let mut names = AHashSet::default();

if self.namespace_id.is_some() {
return names;
}

for item in n {
match item {
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
specifiers,
src: None,
..
})) => {
names.extend(specifiers.iter().map(|s| match s {
ExportSpecifier::Named(ExportNamedSpecifier {
orig: ModuleExportName::Ident(export_id),
..
}) => export_id.to_id(),
_ => unreachable!("only named export is allowed for src = None"),
}));
}
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(ExportDefaultExpr {
expr,
..
})) if expr.is_ident() => {
names.extend(expr.as_ident().map(Ident::to_id));
}
_ => {}
}
}

names
}
}

impl Transform {
Expand Down

0 comments on commit abced23

Please sign in to comment.