Skip to content

Commit

Permalink
Merge pull request #1391 from benjamn/fix-1390
Browse files Browse the repository at this point in the history
fix: print export namespace specifiers without braces
  • Loading branch information
eventualbuddha authored Mar 9, 2024
2 parents 34ca956 + 88a2bae commit 51d5c89
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
10 changes: 8 additions & 2 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Ben Newman <bn@cs.stanford.edu>",
"name": "recast",
"version": "0.23.5",
"version": "0.23.6",
"description": "JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator",
"keywords": [
"ast",
Expand Down
50 changes: 50 additions & 0 deletions test/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 51d5c89

Please sign in to comment.