dprint-plugin-typescript icon indicating copy to clipboard operation
dprint-plugin-typescript copied to clipboard

conditionals: add support for nested indentation

Open declanvong opened this issue 3 years ago • 14 comments

We've had this in our fork for over a year now, and I noticed https://github.com/dprint/dprint-plugin-typescript/issues/432 pop up.

This PR adds support for nested indentation in conditional types and expressions, so that it formats as a tree.

declanvong avatar Nov 01 '22 03:11 declanvong

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 01 '22 03:11 CLAassistant

Ah, I think I accidentally made the commit on my personal account so the CLA assistant thinks I haven't signed it...

edit: yep, all fixed, just signed it on my personal account.

declanvong avatar Nov 01 '22 03:11 declanvong

Cool. Thanks @declanvong! I'll try to take a look on the weekend. Very busy next few weeks because of work work.

dsherret avatar Nov 03 '22 01:11 dsherret

Bump on this one! I also have a couple more PRs - would you rather I just raise them all immediately? and then we can comment / discuss there? Most of them have linkable issues as well.

Otherwise I'm happy to raise them one by one if that makes it less stressful, lol

bitnimble avatar Dec 02 '22 02:12 bitnimble

Ah yep, I can take a look!

Note: I've literally just started back at work today and I might be busy with onboarding, so I might take some time as well :)

declanvong avatar Dec 06 '22 05:12 declanvong

I checked out the new tests; I think that I will have to add something like conditionalType.linePerExpression in order to achieve the same thing that we have right now for conditional expr.

Does that sound okay?

declanvong avatar Dec 20 '22 10:12 declanvong

Updates:

  • Added conditionalType.linePerExpression. Although name tbd, since it's not an expression... so maybe linePerBranch or linePerType might be might accurate? Not sure.
  • I lifted all of the conditional logic up into the head section instead of being in the body, so the same force_cons_alt_newline that's currently there will drive the forced newline.

declanvong avatar Dec 20 '22 11:12 declanvong

Hopefully this PR can be resurrected, the lack of this feature is one of my only gripes with dprint!

b0o avatar Nov 01 '23 05:11 b0o

@declanvong @dsherret Hi guys. Researching the code formatter for our company as ESLint announced their stylistic rule deprecation. DPrint not supporting conditional types syntax is a blocker for us (we consider Prettier and Biome, but DPrint seems to be the best). What is the status of this PR?

chekrd avatar Dec 13 '23 10:12 chekrd

Hey, @dsherret can we do something to help move this along? We would love to see it merged.

todor-a avatar May 15 '24 13:05 todor-a

would like to also see this hopefully soon

pano9000 avatar Mar 02 '25 22:03 pano9000

I've forked this fork and made a wasm release if anyone wants to use this now you can reference it directly in your plugins block.

I'm planning on using dprint for the new version of semantic to enforce styles and this was a blocker for me i noticed on my first codebase wide format run.

  "plugins": [
    "https://github.com/jlukic/dprint-plugin-typescript/releases/download/0.94.0-tern/dprint_plugin_typescript.wasm",
  ]

jlukic avatar Mar 13 '25 15:03 jlukic

Error when formatting const generics in functions.

function foo<const T>(x: T){
  return x
}
Error formatting .../index.ts. Message: Line 1, column 27: Expected ',', got 'T'

   function foo<const T>(x: T){

Huliiiiii avatar Mar 13 '25 19:03 Huliiiiii

I've forked this fork and made a wasm release if anyone wants to use this now you can reference it directly in your plugins block.

I'm planning on using dprint for the new version of semantic to enforce styles and this was a blocker for me i noticed on my first codebase wide format run.

  "plugins": [
    "https://github.com/jlukic/dprint-plugin-typescript/releases/download/0.94.0-tern/dprint_plugin_typescript.wasm",
  ]

Hi @jlukic! thanks for doing this, and sorry if its a dumb question but when attempting to use your fork and explicitly setting the rules

{
        "conditionalExpression.linePerExpression": true,
        "conditionalType.linePerExpression": true,
        "conditionalExpression.useNestedIndentation": true,
        "conditionalType.useNestedIndentation": true,
}

I get an error for these configs not being valid. Do you have any pointers here for how to resolve this?

shaunak-thea avatar Jun 09 '25 19:06 shaunak-thea

I get an error for these configs not being valid. Do you have any pointers here for how to resolve this?

@shaunak-thea I built a version of this that seems to be working so far. this is my first time interacting with dprint and rust. YMMV.

https://github.com/NtTestAlert/dprint-plugin-typescript/releases/tag/0.95.11-tern

    "plugins": [
        "https://github.com/NtTestAlert/dprint-plugin-typescript/releases/download/0.95.11-tern/dprint_plugin_typescript.wasm",
    ]

(I have no idea what implications of just merging origin to the fork and using the resulting wasm are because I didn't read almost any of the code but it works.)


would be nice to have this in the main library because the original behavior is unreadable. especially since with out it I need to patch the ben_12/eslint-plugin-dprint package to use a locally stored .wasm and schema.json :(

NtTestAlert avatar Sep 05 '25 00:09 NtTestAlert