Skip to content

Commit

Permalink
feat(codegen): minify export { 's' as 's' } -> export { 's' } (#8093
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Boshen committed Dec 24, 2024
1 parent fccfda9 commit 6355b7c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 6 additions & 8 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(),
}
}

Expand All @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 6355b7c

Please sign in to comment.