From 6355b7ca70109017f10472699ad840e092cc1581 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Tue, 24 Dec 2024 09:17:51 +0000 Subject: [PATCH] feat(codegen): minify `export { 's' as 's' }` -> `export { 's' }` (#8093) --- crates/oxc_codegen/src/gen.rs | 14 ++++++-------- crates/oxc_codegen/tests/integration/unit.rs | 2 ++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 98996590a00b6..034fc420c6f69 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -914,7 +914,7 @@ impl Gen for ImportDeclaration<'_> { spec.imported.print(p, ctx); let local_name = p.get_binding_identifier_name(&spec.local); let imported_name = get_module_export_name(&spec.imported, p); - if imported_name.is_none() || imported_name != Some(local_name) { + if imported_name != local_name { p.print_str(" as "); spec.local.print(p, ctx); } @@ -1060,13 +1060,11 @@ impl Gen for TSNamespaceExportDeclaration<'_> { fn get_module_export_name<'a>( module_export_name: &ModuleExportName<'a>, p: &Codegen<'a>, -) -> Option<&'a str> { +) -> &'a str { match module_export_name { - ModuleExportName::IdentifierName(ident) => Some(ident.name.as_str()), - ModuleExportName::IdentifierReference(ident) => { - Some(p.get_identifier_reference_name(ident)) - } - ModuleExportName::StringLiteral(_) => None, + ModuleExportName::IdentifierName(ident) => ident.name.as_str(), + ModuleExportName::IdentifierReference(ident) => p.get_identifier_reference_name(ident), + ModuleExportName::StringLiteral(s) => s.value.as_str(), } } @@ -1078,7 +1076,7 @@ impl Gen for ExportSpecifier<'_> { self.local.print(p, ctx); let local_name = get_module_export_name(&self.local, p); let exported_name = get_module_export_name(&self.exported, p); - if exported_name.is_none() || local_name != exported_name { + if local_name != exported_name { p.print_str(" as "); self.exported.print(p, ctx); } diff --git a/crates/oxc_codegen/tests/integration/unit.rs b/crates/oxc_codegen/tests/integration/unit.rs index 24b7ce697ee10..0a94081d4f7e0 100644 --- a/crates/oxc_codegen/tests/integration/unit.rs +++ b/crates/oxc_codegen/tests/integration/unit.rs @@ -15,6 +15,8 @@ fn module_decl() { test("import x from './foo.js' with {}", "import x from \"./foo.js\" with {};\n"); test("import {} from './foo.js' with {}", "import {} from \"./foo.js\" with {};\n"); test("export * from './foo.js' with {}", "export * from \"./foo.js\" with {};\n"); + test_minify("export { '☿' } from 'mod';", "export{\"☿\"}from\"mod\";"); + test_minify("export { '☿' as '☿' } from 'mod';", "export{\"☿\"}from\"mod\";"); } #[test]