From 911d6c88e54a61ca72bd7712f7e2afd9de321b44 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Mon, 17 Dec 2018 04:59:20 -0800 Subject: [PATCH] Fix formatting with --range-start / --range-end (#5632) This change avoids collapsing an already-formatted range into a single overlong line when --range-start / --range-end are provided. Fixes: #4923 --- src/main/ast-to-doc.js | 2 +- tests/range/__snapshots__/jsfmt.spec.js.snap | 28 ++++++++++++++++++++ tests/range/large-dict.js | 8 ++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/range/large-dict.js diff --git a/src/main/ast-to-doc.js b/src/main/ast-to-doc.js index 8fa5cd65..899b100f 100644 --- a/src/main/ast-to-doc.js +++ b/src/main/ast-to-doc.js @@ -78,7 +78,7 @@ function printAstToDoc(ast, options, alignmentSize = 0) { // Add a hardline to make the indents take effect // It should be removed in index.js format() doc = addAlignmentToDoc( - docUtils.removeLines(concat([hardline, doc])), + concat([hardline, doc]), alignmentSize, options.tabWidth ); diff --git a/tests/range/__snapshots__/jsfmt.spec.js.snap b/tests/range/__snapshots__/jsfmt.spec.js.snap index cae5bdfc..adcd96db 100644 --- a/tests/range/__snapshots__/jsfmt.spec.js.snap +++ b/tests/range/__snapshots__/jsfmt.spec.js.snap @@ -88,6 +88,34 @@ function ugly ( {a=1, b = 2 } ) { ================================================================================ `; +exports[`large-dict.js 1`] = ` +====================================options===================================== +parsers: ["flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function ugly() { + const dictWithSeveralEntries = { + key: "value", + anotherKey: "another value", + firstNumber: 1, + secondNumber: 2 + }; +} + +=====================================output===================================== +function ugly() { + const dictWithSeveralEntries = { + key: "value", + anotherKey: "another value", + firstNumber: 1, + secondNumber: 2 + }; +} + +================================================================================ +`; + exports[`module-export1.js 1`] = ` ====================================options===================================== parsers: ["flow", "typescript"] diff --git a/tests/range/large-dict.js b/tests/range/large-dict.js new file mode 100644 index 00000000..78647b73 --- /dev/null +++ b/tests/range/large-dict.js @@ -0,0 +1,8 @@ +function ugly() { + const dictWithSeveralEntries = { + key: "value", +<<>> anotherKey: "another value", + firstNumber: 1, + secondNumber: 2<<>> + }; +}