dprint / dprint-plugin-typescript

TypeScript and JavaScript code formatting plugin for dprint.
https://dprint.dev/plugins/typescript
MIT License
254 stars 57 forks source link

Add option not to inline a single-line functions #673

Open farwayer opened 1 week ago

farwayer commented 1 week ago

It would be great to have an option not to inline a single-line function, even if the line length allows it.

This will be useful, for example, when a single-line arrow functions are not used in place, but are moved to a separate file. You want to keep it visually clean (no braces and returns), but at the same time keep a uniform style for all exported functions for quick visual search (the signature and body of the function on different lines).

export let type = (app: AppState, id: MediaId) =>
  app.medias.get(id)?.type

export let status = (app: AppState, id: MediaId) =>
  app.medias.get(id)?.status

export let videoIds = (app: AppState) =>
  map
    .values(app.medias)
    .filter(media => media.type === 'video')
    .map(media => media.id)

export let audio = (app: AppState, id: MediaId) => {
  let media = app.medias.get(id)
  if (!media) return
  ...
  return media
}

The first two functions will become one-line functions, which I would like to avoid:

export let type = (app: AppState, id: MediaId) => app.medias.get(id)?.type

export let status = (app: AppState, id: MediaId) => app.medias.get(id)?.status

It can be singleBodyPosition.arrowFunction option or maybe something else

farwayer commented 1 week ago

Found many issues about the position of the body of a single-line arrow function. If you look at the usage, it becomes clear that in some cases it is better to move the function body to the next line, and in some cases it is not. So the maintain parameter will be very useful!