Skip to content

Commit e13614f

Browse files
authored
Correct fit() for fill() (#16899)
* add test for #16897 this test fails at this commit. snapshots are taken at e3b8a16 * Fix #16897 * add changelog * omit redundant test case
1 parent 10db357 commit e13614f

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#### Fix non-idempotent formatting (#16899 by @seiyab)
2+
3+
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.
4+
5+
<!-- prettier-ignore -->
6+
```jsx
7+
// Input
8+
<div>
9+
foo
10+
<span>longlonglonglonglonglonglonglonglonglonglonglonglonglonglongl foo</span>
11+
, abc
12+
</div>;
13+
14+
// Prettier stable (first)
15+
<div>
16+
foo
17+
<span>
18+
longlonglonglonglonglonglonglonglonglonglonglonglonglonglongl foo
19+
</span>, abc
20+
</div>;
21+
22+
// Prettier stable (second)
23+
<div>
24+
foo
25+
<span>longlonglonglonglonglonglonglonglonglonglonglonglonglonglongl foo</span>
26+
, abc
27+
</div>;
28+
29+
// Prettier main
30+
<div>
31+
foo
32+
<span>longlonglonglonglonglonglonglonglonglonglonglonglonglonglongl foo</span>
33+
, abc
34+
</div>;
35+
```

src/document/printer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ function fits(
234234
case DOC_TYPE_ARRAY:
235235
case DOC_TYPE_FILL: {
236236
const parts = docType === DOC_TYPE_ARRAY ? doc : doc.parts;
237-
for (let i = parts.length - 1; i >= 0; i--) {
237+
const end = doc[DOC_FILL_PRINTED_LENGTH] ?? 0;
238+
for (let i = parts.length - 1; i >= end; i--) {
238239
cmds.push({ mode, doc: parts[i] });
239240
}
240241
break;

tests/format/jsx/text-wrap/__snapshots__/format.test.js.snap

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`issue-16897.js format 1`] = `
4+
====================================options=====================================
5+
parsers: ["flow", "typescript"]
6+
printWidth: 80
7+
| printWidth
8+
=====================================input======================================
9+
function HelloWorld( ) {
10+
return (
11+
<div>
12+
<div>
13+
foo
14+
<br />
15+
bar{' '}
16+
<span className="font-semibold">
17+
foobar foobar foobar foobar 12345
18+
</span>, foobar foobar foobar
19+
</div>
20+
</div>
21+
)
22+
}
23+
24+
=====================================output=====================================
25+
function HelloWorld() {
26+
return (
27+
<div>
28+
<div>
29+
foo
30+
<br />
31+
bar{" "}
32+
<span className="font-semibold">foobar foobar foobar foobar 12345</span>
33+
, foobar foobar foobar
34+
</div>
35+
</div>
36+
);
37+
}
38+
39+
================================================================================
40+
`;
41+
342
exports[`test.js format 1`] = `
443
====================================options=====================================
544
parsers: ["flow", "typescript"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function HelloWorld( ) {
2+
return (
3+
<div>
4+
<div>
5+
foo
6+
<br />
7+
bar{' '}
8+
<span className="font-semibold">
9+
foobar foobar foobar foobar 12345
10+
</span>, foobar foobar foobar
11+
</div>
12+
</div>
13+
)
14+
}

0 commit comments

Comments
 (0)