From e13614ffd806ee39bdb1ed8fb1026b538de025af Mon Sep 17 00:00:00 2001 From: Seiya <20365512+seiyab@users.noreply.github.com> Date: Sun, 1 Dec 2024 19:00:35 +0900 Subject: [PATCH] Correct fit() for fill() (#16899) * add test for #16897 this test fails at this commit. snapshots are taken at e3b8a16c7fa24 * Fix #16897 * add changelog * omit redundant test case --- changelog_unreleased/javascript/16899.md | 35 +++++++++++++++++ src/document/printer.js | 3 +- .../__snapshots__/format.test.js.snap | 39 +++++++++++++++++++ tests/format/jsx/text-wrap/issue-16897.js | 14 +++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/javascript/16899.md create mode 100644 tests/format/jsx/text-wrap/issue-16897.js diff --git a/changelog_unreleased/javascript/16899.md b/changelog_unreleased/javascript/16899.md new file mode 100644 index 000000000..5d0515bcb --- /dev/null +++ b/changelog_unreleased/javascript/16899.md @@ -0,0 +1,35 @@ +#### Fix non-idempotent formatting (#16899 by @seiyab) + +This bug fix is not language-specific. You may see similar change in any languages. This fixes regression in 3.4.0 so change caused by it should yield same formatting as 3.3.3. + + +```jsx +// Input +
+ foo + longlonglonglonglonglonglonglonglonglonglonglonglonglonglongl foo + , abc +
; + +// Prettier stable (first) +
+ foo + + longlonglonglonglonglonglonglonglonglonglonglonglonglonglongl foo + , abc +
; + +// Prettier stable (second) +
+ foo + longlonglonglonglonglonglonglonglonglonglonglonglonglonglongl foo + , abc +
; + +// Prettier main +
+ foo + longlonglonglonglonglonglonglonglonglonglonglonglonglonglongl foo + , abc +
; +``` diff --git a/src/document/printer.js b/src/document/printer.js index 196691b42..23a268fed 100644 --- a/src/document/printer.js +++ b/src/document/printer.js @@ -234,7 +234,8 @@ function fits( case DOC_TYPE_ARRAY: case DOC_TYPE_FILL: { const parts = docType === DOC_TYPE_ARRAY ? doc : doc.parts; - for (let i = parts.length - 1; i >= 0; i--) { + const end = doc[DOC_FILL_PRINTED_LENGTH] ?? 0; + for (let i = parts.length - 1; i >= end; i--) { cmds.push({ mode, doc: parts[i] }); } break; diff --git a/tests/format/jsx/text-wrap/__snapshots__/format.test.js.snap b/tests/format/jsx/text-wrap/__snapshots__/format.test.js.snap index beb5c870f..fd8dec060 100644 --- a/tests/format/jsx/text-wrap/__snapshots__/format.test.js.snap +++ b/tests/format/jsx/text-wrap/__snapshots__/format.test.js.snap @@ -1,5 +1,44 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`issue-16897.js format 1`] = ` +====================================options===================================== +parsers: ["flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function HelloWorld( ) { + return ( +
+
+ foo +
+ bar{' '} + + foobar foobar foobar foobar 12345 + , foobar foobar foobar +
+
+ ) +} + +=====================================output===================================== +function HelloWorld() { + return ( +
+
+ foo +
+ bar{" "} + foobar foobar foobar foobar 12345 + , foobar foobar foobar +
+
+ ); +} + +================================================================================ +`; + exports[`test.js format 1`] = ` ====================================options===================================== parsers: ["flow", "typescript"] diff --git a/tests/format/jsx/text-wrap/issue-16897.js b/tests/format/jsx/text-wrap/issue-16897.js new file mode 100644 index 000000000..8410de1d1 --- /dev/null +++ b/tests/format/jsx/text-wrap/issue-16897.js @@ -0,0 +1,14 @@ +function HelloWorld( ) { + return ( +
+
+ foo +
+ bar{' '} + + foobar foobar foobar foobar 12345 + , foobar foobar foobar +
+
+ ) +}