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

support quote-props configuration

Open funn1est opened this issue 4 years ago • 4 comments

support Prettier JavaScript quote-props configuration:

  • as-needed
  • consistent
  • preserve

Prettier source code

funn1est avatar Jun 04 '21 16:06 funn1est

I'd like to make an attempt at implementing this.

Is this the correct place to implement the change?

https://github.com/dprint/dprint-plugin-typescript/blob/64064984dc24339249c6425a1401f93d94887967/src/generation/parser.rs#L3467-L3472

I see that this code uses contextual information about the location of the string literal to decide how it's emitted, checking if it is within a JSX attribute. I'll need to additionally check if the string literal is being used as a property name. If it is, check if quotes are necessary according to JavaScript / TypeScript rules for identifiers. If they are unnecessary, emit an identifier. Otherwise emit using current behavior.

This is sufficient for prettier's 'as-needed' and 'preserve' options. https://prettier.io/docs/en/options.html#quote-props But for 'consistent' will need some additional bookkeeping. I don't personally need the 'consistent' option for my projects; is it ok to implement only 'as-needed' and 'preserve' to start?

cspotcode avatar Dec 14 '21 21:12 cspotcode

@cspotcode that would be great! Yeah, those two options would be good enough for now and I think it could all be done in that function. You could match on node.parent().

dsherret avatar Dec 16 '21 17:12 dsherret

Out of curiosity, what's needed to implement consistent?

azuline avatar Feb 27 '22 17:02 azuline

You'll need to write code to figure out if quotes are necessary per-object, save that value somewhere, and apply that formatting to each property name of the object.

Currently, the logic only needs to convert from string literals into identifiers. With consistent, you might need to convert in the opposite direction.

cspotcode avatar Feb 27 '22 22:02 cspotcode