prettier icon indicating copy to clipboard operation
prettier copied to clipboard

Support for embedded SQL

Open ConradHughes opened this issue 3 years ago • 2 comments

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?)

ConradHughes avatar Aug 17 '22 14:08 ConradHughes

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.

JounQin avatar Aug 17 '22 14:08 JounQin

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.

ConradHughes avatar Aug 17 '22 14:08 ConradHughes

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.

TimShilov avatar Nov 01 '22 13:11 TimShilov

PR welcome to add such support, I don't have enough knowledge about it. 🤣

JounQin avatar Nov 01 '22 14:11 JounQin

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

GerkinDev avatar Jun 28 '23 17:06 GerkinDev

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

karlhorky avatar Oct 26 '23 16:10 karlhorky

@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 avatar Oct 26 '23 16:10 karlhorky

@karlhorky Sure! Thanks for letting me know that amazing prettier plugin!

JounQin avatar Oct 27 '23 01:10 JounQin

Great, maybe someone can make a PR for this! 🙌

Yeah kudos to @Sec-ant, amazing plugin!

karlhorky avatar Oct 27 '23 07:10 karlhorky

I'm going to close this issue in favor of https://github.com/Sec-ant/prettier-plugin-embed

JounQin avatar Nov 27 '23 13:11 JounQin

@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!

karlhorky avatar Nov 27 '23 16:11 karlhorky