Sec-ant / prettier-plugin-embed

A configurable Prettier plugin to format embedded languages in JS/TS Files.
https://www.npmjs.com/package/prettier-plugin-embed/v/latest
MIT License
59 stars 2 forks source link

Can't define varying tags for `embeddedSqlTags` #137

Open janpaepke opened 3 days ago

janpaepke commented 3 days ago

This is an issue, which is likely concerning other parsers as well, but since my usecase is SQL, I will focus on that here.

Background Information

In our backend we're using slonik for communication with our PSQL DB.

slonik uses zod for runtime validation and type security of query return types.

This means a typical query will look like this:

sql.type(querySchema)`select ...`

querySchema references a zod type here. For details see https://github.com/gajus/slonik?tab=readme-ov-file#example-use-of-sqltype

The Problem

With the above code I can't just configure embeddedSqlTags to include sql.type(querySchema) because it only works for this specific case. The var name of the schema will differ between queries or sometimes directly include the zod type (i.e. sql.type(z.object({test: z.number()}))`SELECT 1 as test`).

Potential Solutions

Without having checked the source code, a potential solution might be to support for regexes in embeddedSqlTags.

It might also be worth looking into https://github.com/thebearingedge/vscode-sql-lit and how they solved this issue, as they used to have the same problem. In older versions the extension only recognized sql`...`, but now also works well with sql.type(...)`...`.

Sec-ant commented 3 days ago

First, thanks for the detailed issue description, so I can understand the usecase well. Supporting varying tags is a reasonable request.

Overall, I'm not in favor of regexes, as AST trees are already adopted in matching the tags, but in no doubt it's a tempting easy solution. I'll take a look at vscode-sql-lit.

The bad news is currently I have little bandwidth to maintain this package. I still plan to keep this maintained but it's not my top priority, as of now.

A (cumbersome) workaround for now is to use the block comments: sql.type(querySchema) /* sql */ `select ...`.

janpaepke commented 2 days ago

Hi there, thanks for the quick reply.

if you're using AST there might be a way to support these tags as well. One could make the case that defining sql.type in embeddedSqlTags should match sql.type(...) tags as well, which might be a way to avoid regexes.

That being said I, there's no rush and I feel your pain regarding bandwidth. ;) Unfortunately I lack the experience with prettier plugins to look into this myself...