prettier
prettier copied to clipboard
Support for embedded SQL
It would be super cool to see this support prettier's embed API, so it could format SQL strings in JS/TS: https://prettier.io/docs/en/plugins.html#optional-embed (unless it's the TS/JS code that actually needs to support embed in order to invoke prettier-plugin-sql?)
Did you mean something like https://github.com/un-ts/prettier/issues/202#issuecomment-1193296962? I think that's a good feature, and PR welcome.
That looks like it might be the same thing, yes, and I see you say that it actually needs a patch in the other parser, not the SQL plugin. To be clear, what I mean is that the SQL string in code like this:
const query = sql`SELECT * FROM table WHERE foo AND bar AND baz`;
.. would get picked up and reformatted using the SQL plugin.
Not sure how much work this would entail; will have a think about it.
I think another (or additional) option would be to use comment and not tagged template:
const query = /* SQL */ `SELECT * FROM table WHERE foo AND bar AND baz`;
Prettier supports this for /* HTML */ comment out of the box (here's a test for it) and IMO this is a little better solution as you don't need to have actual tag function.
PR welcome to add such support, I don't have enough knowledge about it. 🤣
Hi, I've tried to open a PR for this. Unfortunatelly, currently, embeds in JS files are hardcoded, as you can see here (3.0.0-alpha-6):
https://github.com/prettier/prettier/blob/2bd131fbd29e94e989e64904f1a628877d38ae3d/src/language-js/embed/index.js#L20-L25
Related issue: https://github.com/prettier/prettier/pull/12139
For anyone finding this and looking for another alternative for formatting SQL in embedded sql tagged template literals:
Prettier can be used with the combination of the prettier-plugin-embed and prettier-plugin-sql plugins:
- https://github.com/Sec-ant/prettier-plugin-embed/issues/20
@JounQin would you be open to adding this configuration + code example from my issue above to the prettier-plugin-sql readme as a way for users to already enable this today? (before Prettier supports it out of the box)
@karlhorky Sure! Thanks for letting me know that amazing prettier plugin!
Great, maybe someone can make a PR for this! 🙌
Yeah kudos to @Sec-ant, amazing plugin!
I'm going to close this issue in favor of https://github.com/Sec-ant/prettier-plugin-embed
@JounQin can we rename the issue + leave it open for adding documentation?
I may be able to get around to this in the next weeks, but if someone else sees it before then, would be also great!