From da872c60c483f119f10d93b6157c4402dcaeb494 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 8 Sep 2022 16:18:25 +0300 Subject: [PATCH 1/2] feature: improve support of ExportNamespaceSpecifiers --- lib/printer.ts | 12 ++++++++---- package.json | 2 ++ test/babel.ts | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/printer.ts b/lib/printer.ts index 93e217f4..002b3590 100644 --- a/lib/printer.ts +++ b/lib/printer.ts @@ -2960,13 +2960,17 @@ function printExportDeclaration(path: any, options: any, print: any) { parts.push("{", lines, "}"); } } - } else { + } else if (decl.specifiers[0].type === 'ExportNamespaceSpecifier') { parts.push( - shouldPrintSpaces ? "{ " : "{", fromString(", ").join(path.map(print, "specifiers")), - shouldPrintSpaces ? " }" : "}", ); - } + } else { + parts.push( + shouldPrintSpaces ? "{ " : "{", + fromString(", ").join(path.map(print, "specifiers")), + shouldPrintSpaces ? " }" : "}", + ); + } if (decl.source) { parts.push( diff --git a/package.json b/package.json index 5c8511fb..ccde1f70 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,8 @@ "@babel/core": "7.14.8", "@babel/parser": "7.14.8", "@babel/preset-env": "7.16.11", + "@types/babel__template": "^7.4.1", + "@types/babel__traverse": "^7.18.1", "@types/esprima": "4.0.3", "@types/glob": "7.2.0", "@types/mocha": "8.2.0", diff --git a/test/babel.ts b/test/babel.ts index 72be5c87..775fe9a7 100644 --- a/test/babel.ts +++ b/test/babel.ts @@ -3,6 +3,9 @@ import * as recast from "../main"; const n = recast.types.namedTypes; const b = recast.types.builders; import { EOL as eol } from "os"; +import traverse, {NodePath, Node} from '@babel/traverse'; +import template from '@babel/template'; + const nodeMajorVersion = parseInt(process.versions.node, 10); describe("Babel", function () { @@ -454,4 +457,21 @@ describe("Babel", function () { ["class A {", " declare public readonly x;", "}"].join(eol), ); }); + + 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); + + traverse(ast, { + ExportNamedDeclaration(path: NodePath) { + path.replaceWith(template.ast('export * as fs from "xx"') as Node); + path.stop(); + } + }) + + assert.strictEqual( + recast.print(ast).code, + 'export * as fs from "xx";' + ); + }); }); From 4ee87ac8f73db72c14fda21eab3f944b9a4f3d66 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 8 Sep 2022 17:27:46 +0300 Subject: [PATCH 2/2] feature: ExportNamedDeclaration: add support of multiple specifiers --- lib/printer.ts | 8 ++------ test/babel.ts | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/printer.ts b/lib/printer.ts index 002b3590..695b41f9 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 775fe9a7..366f4c67 100644 --- a/test/babel.ts +++ b/test/babel.ts @@ -474,4 +474,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";' + ); + }); });