JavaScript: Fix formatting on long named exports (#6446)

* Modify to put specific export inline

* Add tests

* Fix tests

* Modify to support default exports

* Update CHANGELOG.unreleased.md

* Modify to refactor

* Add pr link

* Modify to rename "content" -> "printed"

* Modify to define canBreak helpter

* Fix from linter

* Update CHANGELOG.unreleased.md

* Add tests
master
Sosuke Suzuki 2019-09-11 18:10:34 +09:00 committed by Lipis
parent 9f5bd298db
commit 3f89bba231
4 changed files with 66 additions and 16 deletions

View File

@ -581,6 +581,23 @@ Previously, Prettier formatted parens wrap binary expressions within call expres
)();
```
#### JavaScript: Fix formatting on long named exports ([#6446] by [@sosukesuzuki])
Previously, Prettier formatted long named exports differently than named imports.
```js
// Input
export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooooooooooooooooooooo";
// Prettier (stable)
export {
fooooooooooooooooooooooooooooooooooooooooooooooooo
} from "fooooooooooooooooooooooooooooo";
// Prettier (master)
export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooooooooooooooooooooo";
```
[#5910]: https://github.com/prettier/prettier/pull/5910
[#6186]: https://github.com/prettier/prettier/pull/6186
[#6206]: https://github.com/prettier/prettier/pull/6206
@ -601,6 +618,7 @@ Previously, Prettier formatted parens wrap binary expressions within call expres
[#6411]: https://github.com/prettier/prettier/pull/6411
[#6438]: https://github.com/prettier/prettier/pull/6411
[#6441]: https://github.com/prettier/prettier/pull/6441
[#6446]: https://github.com/prettier/prettier/pull/6446
[@duailibe]: https://github.com/duailibe
[@gavinjoyce]: https://github.com/gavinjoyce
[@sosukesuzuki]: https://github.com/sosukesuzuki

View File

@ -4598,28 +4598,46 @@ function printExportDeclaration(path, options, print) {
defaultSpecifiers.length !== 0 &&
(namespaceSpecifiers.length !== 0 || specifiers.length !== 0);
const canBreak =
specifiers.length > 1 ||
defaultSpecifiers.length > 0 ||
(decl.specifiers && decl.specifiers.some(node => node.comments));
let printed = "";
if (specifiers.length !== 0) {
if (canBreak) {
printed = group(
concat([
"{",
indent(
concat([
options.bracketSpacing ? line : softline,
join(concat([",", line]), specifiers)
])
),
ifBreak(shouldPrintComma(options) ? "," : ""),
options.bracketSpacing ? line : softline,
"}"
])
);
} else {
printed = concat([
"{",
options.bracketSpacing ? " " : "",
concat(specifiers),
options.bracketSpacing ? " " : "",
"}"
]);
}
}
parts.push(
decl.exportKind === "type" ? "type " : "",
concat(defaultSpecifiers),
concat([isDefaultFollowed ? ", " : ""]),
concat(namespaceSpecifiers),
concat([isNamespaceFollowed ? ", " : ""]),
specifiers.length !== 0
? group(
concat([
"{",
indent(
concat([
options.bracketSpacing ? line : softline,
join(concat([",", line]), specifiers)
])
),
ifBreak(shouldPrintComma(options) ? "," : ""),
options.bracketSpacing ? line : softline,
"}"
])
)
: ""
printed
);
} else {
parts.push("{}");

View File

@ -11,6 +11,9 @@ export v from 'mod';
export a, * as b from 'mod';
export c, { foo } from 'mod';
export * as d, { bar } from 'mod';
export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooooooooooooooooooooo";
export Bar, { barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr } from "barrrrrrrrrrrrrrrrrrrrrrrrrrrr";
export { foooooooooooooooooooooooooooooooooooooooooooooo, fooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooooooooooooooooooooo";
=====================================output=====================================
export * as ns from "mod";
@ -18,6 +21,14 @@ export v from "mod";
export a, * as b from "mod";
export c, { foo } from "mod";
export * as d, { bar } from "mod";
export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooooooooooooooooooooo";
export Bar, {
barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
} from "barrrrrrrrrrrrrrrrrrrrrrrrrrrr";
export {
foooooooooooooooooooooooooooooooooooooooooooooo,
fooooooooooooooooooooooooooooooooooooooooooooooo
} from "fooooooooooooooooooooooooooooo";
================================================================================
`;

View File

@ -3,3 +3,6 @@ export v from 'mod';
export a, * as b from 'mod';
export c, { foo } from 'mod';
export * as d, { bar } from 'mod';
export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooooooooooooooooooooo";
export Bar, { barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr } from "barrrrrrrrrrrrrrrrrrrrrrrrrrrr";
export { foooooooooooooooooooooooooooooooooooooooooooooo, fooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooooooooooooooooooooo";