From 3f89bba2314d0b69eae6e290f243e3e2a848d9fa Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Wed, 11 Sep 2019 18:10:34 +0900 Subject: [PATCH] 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 --- CHANGELOG.unreleased.md | 18 +++++++ src/language-js/printer-estree.js | 50 +++++++++++++------ .../__snapshots__/jsfmt.spec.js.snap | 11 ++++ tests/export_extension/export.js | 3 ++ 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index a9a21e3e..5ea0f757 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -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 diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 3c50af84..39aed235 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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("{}"); diff --git a/tests/export_extension/__snapshots__/jsfmt.spec.js.snap b/tests/export_extension/__snapshots__/jsfmt.spec.js.snap index bdc199f0..3f4f650c 100644 --- a/tests/export_extension/__snapshots__/jsfmt.spec.js.snap +++ b/tests/export_extension/__snapshots__/jsfmt.spec.js.snap @@ -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"; ================================================================================ `; diff --git a/tests/export_extension/export.js b/tests/export_extension/export.js index adfd44b3..2626fe4a 100644 --- a/tests/export_extension/export.js +++ b/tests/export_extension/export.js @@ -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";