dprint / dprint-plugin-typescript

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

How can I configurate formatting of ternary operator? #432

Open doox911 opened 1 year ago

doox911 commented 1 year ago

For example:

const value = is_prod 
  ? do() 
  :  is_laptop 
    ? do2()
    : do3();

Language: Javascript, Typescript, Files: Vue, React, jsx and tsx.

dsherret commented 1 year ago

Sorry for my delay. Do you mean the operator position? You can use the "conditionalExpression.operatorPosition" configuration for that (see https://dprint.dev/plugins/typescript/config/ and search for "operatorPosition").

Or are you wondering how to indent the second conditional expression? It's not possible to do that at the moment.

doox911 commented 1 year ago

Yes, it is nesting(second conditional expression) that interests me. Then let it be a wish for improvement)!

unicornware commented 10 months ago

@dsherret what option can i use to maintain the formatting below?

/**
 * @file Type Definitions - Writable
 * @module tutils/types/Writable
 */

/**
 * Construct a type where all properties of `T` are writable.
 *
 * This is the opposite of {@linkcode Readonly}.
 *
 * @todo examples
 *
 * @template T - Type to evaluate
 */
type Writable<T> = T extends unknown
  ? { -readonly [K in keyof T]: T[K] }
  : never

export type { Writable as default }

i tried changing operatorPosition (and its variants) to maintain, but the new formatting is still ugly. the only workaround is adding dprint-ignore comments, but that of course ignores more than what i am intending to. in my case, i have to add 80+ comments for one project.

edit: i updated the playground link to include more than just the above example. i'm trying to migrate from prettier, but i'm finding that all conditional type formatting results are less than desirable. line break/indentation removal is making my code less readable.

edit 2: i read through #450. from what i understand, that pr may actually fix my issue. is that correct?