From 1549936e6879d2cf4bad0e266054a910159a3b46 Mon Sep 17 00:00:00 2001 From: Benjamin Tan Date: Wed, 11 Jan 2017 21:56:10 +0800 Subject: [PATCH 1/2] Break long `exports` into multiple lines. Ref #19. --- src/printer.js | 22 ++++++++++++++++--- .../__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++ .../__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++ .../prettier/__snapshots__/jsfmt.spec.js.snap | 13 +++++++++++ tests/prettier/exports.js | 1 + 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/prettier/exports.js diff --git a/src/printer.js b/src/printer.js index 3e3389087..d2372cce8 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1866,9 +1866,25 @@ function printExportDeclaration(path, options, print) { } else { parts.push( decl.exportKind === "type" ? "type " : "", - shouldPrintSpaces ? "{ " : "{", - join(", ", path.map(print, "specifiers")), - shouldPrintSpaces ? " }" : "}" + conditionalGroup([ + concat([ + shouldPrintSpaces ? "{ " : "{", + join(", ", path.map(print, "specifiers")), + shouldPrintSpaces ? " }" : "}" + ]), + concat([ + "{", + indent( + options.tabWidth, + concat([ + hardline, + join(concat([",", hardline]), path.map(print, "specifiers")) + ]) + ), + hardline, + "}" + ]) + ]) ); } diff --git a/tests/declare_export/__snapshots__/jsfmt.spec.js.snap b/tests/declare_export/__snapshots__/jsfmt.spec.js.snap index aa2d614d4..e524afbc0 100644 --- a/tests/declare_export/__snapshots__/jsfmt.spec.js.snap +++ b/tests/declare_export/__snapshots__/jsfmt.spec.js.snap @@ -352,8 +352,15 @@ declare export { * @flow */ +<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 declare export { numberValue1, numberValue2 as numberValue2_renamed } from \"ES6_ExportFrom_Source1\"; " +======= +declare export { + numberValue1, + numberValue2 as numberValue2_renamed +} from \"ES6_ExportFrom_Source1\";" +>>>>>>> Break long `exports` into multiple lines. `; exports[`test ES6_ExportFrom_Intermediary2.js 1`] = ` @@ -372,8 +379,15 @@ declare export { * @flow */ +<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 declare export { numberValue1, numberValue2 as numberValue2_renamed2 } from \"ES6_ExportFrom_Source2\"; " +======= +declare export { + numberValue1, + numberValue2 as numberValue2_renamed2 +} from \"ES6_ExportFrom_Source2\";" +>>>>>>> Break long `exports` into multiple lines. `; exports[`test ES6_ExportFrom_Source1.js 1`] = ` diff --git a/tests/es6modules/__snapshots__/jsfmt.spec.js.snap b/tests/es6modules/__snapshots__/jsfmt.spec.js.snap index 2bc7d5b35..a9449b820 100644 --- a/tests/es6modules/__snapshots__/jsfmt.spec.js.snap +++ b/tests/es6modules/__snapshots__/jsfmt.spec.js.snap @@ -429,8 +429,15 @@ export { * @flow */ +<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 export { numberValue1, numberValue2 as numberValue2_renamed } from \"ES6_ExportFrom_Source1\"; " +======= +export { + numberValue1, + numberValue2 as numberValue2_renamed +} from \"ES6_ExportFrom_Source1\";" +>>>>>>> Break long `exports` into multiple lines. `; exports[`test ES6_ExportFrom_Intermediary2.js 1`] = ` @@ -449,8 +456,15 @@ export { * @flow */ +<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 export { numberValue1, numberValue2 as numberValue2_renamed2 } from \"ES6_ExportFrom_Source2\"; " +======= +export { + numberValue1, + numberValue2 as numberValue2_renamed2 +} from \"ES6_ExportFrom_Source2\";" +>>>>>>> Break long `exports` into multiple lines. `; exports[`test ES6_ExportFrom_Source1.js 1`] = ` diff --git a/tests/prettier/__snapshots__/jsfmt.spec.js.snap b/tests/prettier/__snapshots__/jsfmt.spec.js.snap index 8fb274d19..1bcf18dce 100644 --- a/tests/prettier/__snapshots__/jsfmt.spec.js.snap +++ b/tests/prettier/__snapshots__/jsfmt.spec.js.snap @@ -1,3 +1,4 @@ +<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 exports[`test directives.js 1`] = ` "\"use strict\"; @@ -11,6 +12,18 @@ function fn() { \"use strict\"; } " +======= +exports[`test exports.js 1`] = ` +"export { value1, value2 as value2_renamed, value3, value4 as value4_renamed, value5 } from \"exports\"; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +export { + value1, + value2 as value2_renamed, + value3, + value4 as value4_renamed, + value5 +} from \"exports\";" +>>>>>>> Break long `exports` into multiple lines. `; exports[`test optional-type-name.js 1`] = ` diff --git a/tests/prettier/exports.js b/tests/prettier/exports.js new file mode 100644 index 000000000..e3699c9fc --- /dev/null +++ b/tests/prettier/exports.js @@ -0,0 +1 @@ +export { value1, value2 as value2_renamed, value3, value4 as value4_renamed, value5 } from "exports"; From aab300844f67851f48edb3deaac69bb24b26ca5a Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 12 Jan 2017 14:53:12 -0500 Subject: [PATCH 2/2] Use group instead of conditionalGroup --- src/printer.js | 32 ++++++++----------- .../__snapshots__/jsfmt.spec.js.snap | 16 +++------- .../__snapshots__/jsfmt.spec.js.snap | 16 +++------- .../prettier/__snapshots__/jsfmt.spec.js.snap | 8 ++--- 4 files changed, 25 insertions(+), 47 deletions(-) diff --git a/src/printer.js b/src/printer.js index d2372cce8..5fd4d66f8 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1866,25 +1866,19 @@ function printExportDeclaration(path, options, print) { } else { parts.push( decl.exportKind === "type" ? "type " : "", - conditionalGroup([ - concat([ - shouldPrintSpaces ? "{ " : "{", - join(", ", path.map(print, "specifiers")), - shouldPrintSpaces ? " }" : "}" - ]), - concat([ - "{", - indent( - options.tabWidth, - concat([ - hardline, - join(concat([",", hardline]), path.map(print, "specifiers")) - ]) - ), - hardline, - "}" - ]) - ]) + group(concat([ + "{", + indent( + options.tabWidth, + concat([ + options.bracketSpacing ? line : softline, + join(concat([",", options.bracketSpacing ? line : softline]), + path.map(print, "specifiers")) + ]) + ), + options.bracketSpacing ? line : softline, + "}" + ])) ); } diff --git a/tests/declare_export/__snapshots__/jsfmt.spec.js.snap b/tests/declare_export/__snapshots__/jsfmt.spec.js.snap index e524afbc0..d9a5c75e7 100644 --- a/tests/declare_export/__snapshots__/jsfmt.spec.js.snap +++ b/tests/declare_export/__snapshots__/jsfmt.spec.js.snap @@ -352,15 +352,11 @@ declare export { * @flow */ -<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 -declare export { numberValue1, numberValue2 as numberValue2_renamed } from \"ES6_ExportFrom_Source1\"; -" -======= declare export { numberValue1, numberValue2 as numberValue2_renamed -} from \"ES6_ExportFrom_Source1\";" ->>>>>>> Break long `exports` into multiple lines. +} from \"ES6_ExportFrom_Source1\"; +" `; exports[`test ES6_ExportFrom_Intermediary2.js 1`] = ` @@ -379,15 +375,11 @@ declare export { * @flow */ -<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 -declare export { numberValue1, numberValue2 as numberValue2_renamed2 } from \"ES6_ExportFrom_Source2\"; -" -======= declare export { numberValue1, numberValue2 as numberValue2_renamed2 -} from \"ES6_ExportFrom_Source2\";" ->>>>>>> Break long `exports` into multiple lines. +} from \"ES6_ExportFrom_Source2\"; +" `; exports[`test ES6_ExportFrom_Source1.js 1`] = ` diff --git a/tests/es6modules/__snapshots__/jsfmt.spec.js.snap b/tests/es6modules/__snapshots__/jsfmt.spec.js.snap index a9449b820..f5c5abbef 100644 --- a/tests/es6modules/__snapshots__/jsfmt.spec.js.snap +++ b/tests/es6modules/__snapshots__/jsfmt.spec.js.snap @@ -429,15 +429,11 @@ export { * @flow */ -<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 -export { numberValue1, numberValue2 as numberValue2_renamed } from \"ES6_ExportFrom_Source1\"; -" -======= export { numberValue1, numberValue2 as numberValue2_renamed -} from \"ES6_ExportFrom_Source1\";" ->>>>>>> Break long `exports` into multiple lines. +} from \"ES6_ExportFrom_Source1\"; +" `; exports[`test ES6_ExportFrom_Intermediary2.js 1`] = ` @@ -456,15 +452,11 @@ export { * @flow */ -<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 -export { numberValue1, numberValue2 as numberValue2_renamed2 } from \"ES6_ExportFrom_Source2\"; -" -======= export { numberValue1, numberValue2 as numberValue2_renamed2 -} from \"ES6_ExportFrom_Source2\";" ->>>>>>> Break long `exports` into multiple lines. +} from \"ES6_ExportFrom_Source2\"; +" `; exports[`test ES6_ExportFrom_Source1.js 1`] = ` diff --git a/tests/prettier/__snapshots__/jsfmt.spec.js.snap b/tests/prettier/__snapshots__/jsfmt.spec.js.snap index 1bcf18dce..287f35f05 100644 --- a/tests/prettier/__snapshots__/jsfmt.spec.js.snap +++ b/tests/prettier/__snapshots__/jsfmt.spec.js.snap @@ -1,4 +1,3 @@ -<<<<<<< bbe2524dab164d43febc1a8d549a76e26eb9d483 exports[`test directives.js 1`] = ` "\"use strict\"; @@ -12,7 +11,8 @@ function fn() { \"use strict\"; } " -======= +`; + exports[`test exports.js 1`] = ` "export { value1, value2 as value2_renamed, value3, value4 as value4_renamed, value5 } from \"exports\"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -22,8 +22,8 @@ export { value3, value4 as value4_renamed, value5 -} from \"exports\";" ->>>>>>> Break long `exports` into multiple lines. +} from \"exports\"; +" `; exports[`test optional-type-name.js 1`] = `