From bc87a950bb01eab7ba2e77e80914e7f4ba63f014 Mon Sep 17 00:00:00 2001 From: Brian Donovan <1938+eventualbuddha@users.noreply.github.com> Date: Sun, 3 Mar 2024 11:46:01 -0800 Subject: [PATCH] fix: print export namespace specifiers without braces Fixes #1390 --- lib/printer.ts | 10 ++++++++-- test/printer.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/printer.ts b/lib/printer.ts index 807e0765..5b3f9618 100644 --- a/lib/printer.ts +++ b/lib/printer.ts @@ -3005,13 +3005,19 @@ function printExportDeclaration(path: any, options: any, print: any) { parts.push("*"); } else if (decl.specifiers.length === 0) { parts.push("{}"); - } else if (decl.specifiers[0].type === "ExportDefaultSpecifier") { + } else if ( + decl.specifiers[0].type === "ExportDefaultSpecifier" || + decl.specifiers[0].type === "ExportNamespaceSpecifier" + ) { const unbracedSpecifiers: any[] = []; const bracedSpecifiers: any[] = []; path.each(function (specifierPath: any) { const spec = specifierPath.getValue(); - if (spec.type === "ExportDefaultSpecifier") { + if ( + spec.type === "ExportDefaultSpecifier" || + spec.type === "ExportNamespaceSpecifier" + ) { unbracedSpecifiers.push(print(specifierPath)); } else { bracedSpecifiers.push(print(specifierPath)); diff --git a/test/printer.ts b/test/printer.ts index 0a5705c9..e25a961d 100644 --- a/test/printer.ts +++ b/test/printer.ts @@ -508,6 +508,56 @@ describe("printer", function () { assert.strictEqual(printer.printGenerically(ast).code, code); }); + it("export namespace", function () { + const printer = new Printer(); + + assert.strictEqual( + printer.print({ + type: "ExportNamedDeclaration", + exportKind: "value", + specifiers: [ + { + type: "ExportNamespaceSpecifier", + exported: { + type: "Identifier", + name: "Foobar", + }, + }, + ], + source: { + type: "StringLiteral", + value: "./foo", + }, + }).code, + `export * as Foobar from "./foo";`, + ); + }); + + it("export type namespace", function () { + const printer = new Printer(); + + assert.strictEqual( + printer.print({ + type: "ExportNamedDeclaration", + exportKind: "type", + specifiers: [ + { + type: "ExportNamespaceSpecifier", + exported: { + type: "Identifier", + name: "Foobar", + }, + }, + ], + source: { + type: "StringLiteral", + value: "./foo", + }, + }).code, + `export type * as Foobar from "./foo";`, + ); + }); + it("export default of IIFE", function () { const printer = new Printer(); let ast = b.exportDefaultDeclaration(