Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: print export namespace specifiers without braces #1391

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>",
"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
Loading