Skip to content

[TypeScript] Allow function parameters to share the next newline #6573

Open

Description

@niedzielski

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.

Playground link

--parser typescript

Input:

// 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

  1. added
    lang:javascript
    lang:typescript
    status:needs discussion
    on Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:javascriptlang:typescriptstatus:needs discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions