-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Incorrectly calculated line width #18148
Copy link
Copy link
Labels
area:doc printerdifficulty:hardIssues that might take an entire weekend, or require a tough decision to fixIssues that might take an entire weekend, or require a tough decision to fix
Description
Prettier 3.6.2
Playground link
--parser typescript
--print-width 78Input:
const firstItem1 = editorPicker.items.find((item) => item.type === 'item') as string | number;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 77 width
const firstItem22 = editorPicker.items.find((item) => item.type === 'item') as string | number;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 78 width
Output:
const firstItem1 = editorPicker.items.find((item) => item.type === "item") as
| string
| number;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 77 width
const firstItem22 = editorPicker.items.find(
(item) => item.type === "item",
) as string | number;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 78 widthExpected output:
const firstItem1 = editorPicker.items.find((item) => item.type === "item") as
| string
| number;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 77 width
const firstItem22 = editorPicker.items.find((item) => item.type === "item") as
| string
| number;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 78 widthWhy?
The printWidth is 78, and the const firstItem22 = editorPicker.items.find((item) => item.type === "item") is the same as 78 width, so the code should be printed on the same line because it is sufficient. However, it incorrectly calculated the current line width, which caused the parent group to break.
This issue is quite complex, so I have not looked into the Prettier implementation and debugging, and I am not sure if it is a bug in Prettier or if it is just by design.
If it is a bug, I suspect the issue is in calculating the trailing comma of arguments, which does not exist when the arguments don't break, as we can see in the current firstItem22 output.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area:doc printerdifficulty:hardIssues that might take an entire weekend, or require a tough decision to fixIssues that might take an entire weekend, or require a tough decision to fix