Repository navigation
Description
Prettier 1.18.2
This is a formatting behavior change request to reduce verbosity for TypeScript specifically: when a function's parameters cannot fit on the same line as the rest of the signature, allow them to share the next newline if they fit. A simple example follows for structured and destructured parameters.
--parser typescriptInput:
// Structured (exceeds line width).
export function parseCollisionA(frameTag: Aseprite.FrameTag, frameNumber: number, slices: Aseprite.Slice): readonly Rect[] {
return [];
}
// Destructured (exceeds line width).
export function parseCollisionB({frameTag, frameNumber, slices}: Aseprite.File): readonly Rect[] {
return [];
}Output:
// Structured.
export function parseCollisionA(
frameTag: Aseprite.FrameTag,
frameNumber: number,
slices: Aseprite.Slice
): readonly Rect[] {
return [];
}
// Destructured.
export function parseCollisionB({
frameTag,
frameNumber,
slices
}: Aseprite.File): readonly Rect[] {
return [];
}Expected behavior:
// Proposal: allow parameters to share the next newline if they fit.
export function parseCollisionA(
frameTag: Aseprite.FrameTag, frameNumber: number, slices: Aseprite.Slice
): readonly Rect[] {
return [];
}
// Proposal: allow destructured properties to share the next newline if they fit.
export function parseCollisionB(
{frameTag, frameNumber, slices}: Aseprite.File
): readonly Rect[] {
return [];
}Reasoning:
This proposal is less significant for untyped JavaScript. However, TypeScript function signatures are wordy by nature due to argument typing that is usually non-optional. At 80 characters wide, it's hard to avoid function signatures spilling across multiple lines even for simple functions with only a couple parameters or destructured arguments. I think this makes otherwise simple code harder to read than it needs to be.
The proposed new behavior is consistent with the current behavior for function parameters that happen to fit on the same line as the function signatures. That is, function parameters are allowed to share the same line if they fit. Additionally, the new behavior would be consistent with object properties that are allowed to appear on the same line if they fit.
I would guess that most developers are working around the current behavior by increasing their line width to something larger than they'd like.
My apologies if this topic has already been discussed.
Activity