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

feature: improve support of ExportNamespaceSpecifiers #1185

Closed
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
12 changes: 6 additions & 6 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3053,12 +3053,12 @@ function printExportDeclaration(path: any, options: any, print: any) {
}
}
} else {
parts.push(
shouldPrintSpaces ? "{ " : "{",
fromString(", ").join(path.map(print, "specifiers")),
shouldPrintSpaces ? " }" : "}",
);
}
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 @@ -53,6 +53,8 @@
"@babel/core": "7.20.5",
"@babel/parser": "7.20.5",
"@babel/preset-env": "7.20.2",
"@types/babel__template": "^7.4.1",
"@types/babel__traverse": "^7.18.1",
"@types/esprima": "4.0.3",
"@types/glob": "8.0.0",
"@types/mocha": "10.0.1",
Expand Down
40 changes: 40 additions & 0 deletions test/babel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import assert from "assert";
import { EOL as eol } from "os";
import * as recast from "../main";

import traverse, {NodePath, Node} from '@babel/traverse';
import template from '@babel/template';

const n = recast.types.namedTypes;
const b = recast.types.builders;

const nodeMajorVersion = parseInt(process.versions.node, 10);

describe("Babel", function () {
Expand Down Expand Up @@ -455,6 +460,41 @@ describe("Babel", function () {
);
});

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";'
);
});

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";'
);
});


it("should keep braces in !(a && b)", function () {
const code = "(options || !options.bidirectional) ? false : true;";
const ast = recast.parse(code, parseOptions);
Expand Down
Loading