Skip to content

Commit

Permalink
feature: ExportNamedDeclaration: add support of multiple specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Sep 8, 2022
1 parent da872c6 commit 5d66e84
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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 ? "{ " : "{",
Expand Down
40 changes: 40 additions & 0 deletions test/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";'
);
});
});

0 comments on commit 5d66e84

Please sign in to comment.