From 26183e5be2c07eeb392b3a6b7822517cf6f43fc6 Mon Sep 17 00:00:00 2001 From: mathieulj Date: Sat, 4 May 2019 10:18:55 -0400 Subject: [PATCH] Use same print width for range formatting as normal formatting. (#6050) * Use same print width for range formatting as normal formatting. Fixes an issue were a file would end up formatted differently with ranged formatting (--range-start & --range-end) versus normal whole file formatting. * Document range bugfix in changelog. --- CHANGELOG.unreleased.md | 38 ++++++++++++++++++++ src/main/core.js | 1 - tests/range/__snapshots__/jsfmt.spec.js.snap | 22 ++++++++++++ tests/range/nested-print-width.js | 5 +++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/range/nested-print-width.js diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index ac9339ff..40ef23de 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -42,6 +42,44 @@ Examples: --> +- Range: Fix ranged formatting not using the correct line width ([#6050] by [@mathieulj]) + + + ```js + // Input + function f() { + if (true) { + call("this line is 79 chars", "long", "it should", "stay as single line"); + } + } + + // Output (Prettier stable run with --range-start 30 --range-end 110) + function f() { + if (true) { + call( + "this line is 79 chars", + "long", + "it should", + "stay as single line" + ); + } + } + + // Output (Prettier stable run without range) + function f() { + if (true) { + call("this line is 79 chars", "long", "it should", "stay as single line"); + } + } + + // Output (Prettier master with and without range) + function f() { + if (true) { + call("this line is 79 chars", "long", "it should", "stay as single line"); + } + } + ``` + - JavaScript: Fix closure compiler typecasts ([#5947] by [@jridgewell]) If a closing parenthesis follows after a typecast in an inner expression, the typecast would wrap everything to the that following parenthesis. diff --git a/src/main/core.js b/src/main/core.js index 556f0a3a..0f37afe6 100644 --- a/src/main/core.js +++ b/src/main/core.js @@ -210,7 +210,6 @@ function formatRange(text, opts) { Object.assign({}, opts, { rangeStart: 0, rangeEnd: Infinity, - printWidth: opts.printWidth - alignmentSize, // track the cursor offset only if it's within our range cursorOffset: opts.cursorOffset >= rangeStart && opts.cursorOffset < rangeEnd diff --git a/tests/range/__snapshots__/jsfmt.spec.js.snap b/tests/range/__snapshots__/jsfmt.spec.js.snap index adcd96db..b1932a1f 100644 --- a/tests/range/__snapshots__/jsfmt.spec.js.snap +++ b/tests/range/__snapshots__/jsfmt.spec.js.snap @@ -334,6 +334,28 @@ try { ================================================================================ `; +exports[`nested-print-width.js 1`] = ` +====================================options===================================== +parsers: ["flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function f() { + if (true) { + call("this line is 79 chars", "long", "it should", "stay as single line"); + } +} + +=====================================output===================================== +function f() { + if (true) { + call("this line is 79 chars", "long", "it should", "stay as single line"); + } +} + +================================================================================ +`; + exports[`nested2.js 1`] = ` ====================================options===================================== parsers: ["flow", "typescript"] diff --git a/tests/range/nested-print-width.js b/tests/range/nested-print-width.js new file mode 100644 index 00000000..194d03f2 --- /dev/null +++ b/tests/range/nested-print-width.js @@ -0,0 +1,5 @@ +function f() { + if (true) {<<>> + call("this line is 79 chars", "long", "it should", "stay as single line"); + <<>>} +}