Skip to content

Commit

Permalink
feature: improve support of ExportNamespaceSpecifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Sep 8, 2022
1 parent e3bdfe0 commit da872c6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions test/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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";'
);
});
});

0 comments on commit da872c6

Please sign in to comment.