dprint-plugin-typescript
dprint-plugin-typescript copied to clipboard
support quote-props configuration
support Prettier JavaScript quote-props configuration:
as-neededconsistentpreserve
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 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().
Out of curiosity, what's needed to implement consistent?
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.