Skip to content

Incorrectly calculated line width #18148

@Dunqing

Description

@Dunqing

Prettier 3.6.2
Playground link

--parser typescript
--print-width 78

Input:

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 width

Expected 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 width

Why?

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.

Metadata

Metadata

Assignees

Labels

area:doc printerdifficulty:hardIssues that might take an entire weekend, or require a tough decision to fix

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions