diff --git a/lib/printer.ts b/lib/printer.ts index 002b3590c..695b41f9c 100644 --- a/lib/printer.ts +++ b/lib/printer.ts @@ -2919,13 +2919,13 @@ 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 (/^Export(Default|Namespace)Specifier$/.test(decl.specifiers[0].type)) { const unbracedSpecifiers: any[] = []; const bracedSpecifiers: any[] = []; path.each(function (specifierPath: any) { const spec = specifierPath.getValue(); - if (spec.type === "ExportDefaultSpecifier") { + if (/^Export(Namespace|Default)Specifier$/.test(spec.type)) { unbracedSpecifiers.push(print(specifierPath)); } else { bracedSpecifiers.push(print(specifierPath)); @@ -2960,10 +2960,6 @@ function printExportDeclaration(path: any, options: any, print: any) { parts.push("{", lines, "}"); } } - } else if (decl.specifiers[0].type === 'ExportNamespaceSpecifier') { - parts.push( - fromString(", ").join(path.map(print, "specifiers")), - ); } else { parts.push( shouldPrintSpaces ? "{ " : "{", diff --git a/test/babel.ts b/test/babel.ts index 775fe9a79..b57f6c7a6 100644 --- a/test/babel.ts +++ b/test/babel.ts @@ -458,6 +458,29 @@ describe("Babel", function () { ); }); + it("should keep braces in !(a && b)", function () { + const code = '(options || !options.bidirectional) ? false : true;'; + const ast = recast.parse(code, parseOptions); + + ast.program.body[0].expression = b.unaryExpression('!', ast.program.body[0].expression.test); + + assert.strictEqual( + recast.print(ast).code, + '!((options || !options.bidirectional));', + ); + }); + it("should use single quotes", function () { + const code = 'const a = 1;'; + const ast = recast.parse(code, parseOptions); + + ast.program.body.unshift(b.expressionStatement(b.stringLiteral('use strict'))); + + assert.strictEqual( + recast.print(ast, {quote: 'single'}).code, + `'use strict';\nconst a = 1;`, + ); + }); + it("should not add curly braces in ExportNamedDeclaration used with ExportNamespaceSpecifier", function () { const code = 'export * as fs2 from "fs/promises"'; const ast = recast.parse(code, parseOptions); @@ -474,4 +497,21 @@ describe("Babel", function () { 'export * as fs from "xx";' ); }); + + it("should not add curly braces in ExportNamedDeclaration used with ExportNamespaceSpecifier: couple specifiers", function () { + const code = 'export * as fs2, {x, y} from "fs/promises"'; + const ast = recast.parse(code, parseOptions); + + traverse(ast, { + ExportNamedDeclaration(path: NodePath) { + path.replaceWith(template.ast('export * as fs, {x, y} from "xx"') as Node); + path.stop(); + } + }) + + assert.strictEqual( + recast.print(ast).code, + 'export * as fs, { x, y } from "xx";' + ); + }); });