rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Preserve tagged template in generated code

Open DZakh opened this issue 11 months ago • 3 comments

I've just tried out tagged template, and it would be great if they stayed as tagged templates instead of turning into arrays like here:

https://rescript-lang.org/try?version=v12.0.0-alpha.7&module=esmodule&code=KYDwLsBOB2CGA2ACAtgTwCqwOYDECu0AxmAJYD20iAXIgBSySSyoA8AzmJCdFgHwA0iBk1YByAA4NYyXgEpEAXl6JRZPGHHrFiAEQA3BDoBQRgFJsAdPDJZaaTLgLFy0AAYAzMmVeyjQA

I use the postgres package and it returns the following error:

image

DZakh avatar Jan 09 '25 13:01 DZakh

You can add @taggedTemplate when it's an external to achieve what you want. What you can't do as of now is have a regular rescript function emitted as a proper tagged template. But I suppose that'd be possible to add too.

zth avatar Jan 09 '25 13:01 zth

Cool, I didn't know about this. But in my case I have a returned type instead of an external: https://rescript-lang.org/try?version=v12.0.0-alpha.7&module=esmodule&code=AIFwhg5hCmAmAq0C2AHANmE0BQICeK0ABAM4COaRAvEQBRgBODYeAPCSAwJYB2EAfABoijZmw7c+-AJTV+RFAwD2SLiWisAUiQB02pTx0h+2XAWIolStAGEDAMy4RqRAN4BfU8CRLYAVzQcaAAPLAYeMEokMABraABlCgAuOgA-AGMHJxTLazseRwhZKnlyShoAIksOCAZoEgrTQJBSZNbyomi4xLRaDKyIKg9pU20dNCUIWjKAA3srGZGgA

DZakh avatar Jan 09 '25 14:01 DZakh

I just want to add this would be a great feature for reading the compiled output. A tagged template could be formatted by Prettier and highlighted by VSCode extensions.

It would also be good for working with linting and AST transformation tools. As-is ReScript would need to do something weird like this:

let init = () => {
  let sql = (_, _) => "example"
  %raw(`globalThis`)["sql"] = sql
}

@taggedTemplate
external sql: (array<string>, array<string>) => string = "sql"

CarlOlson avatar May 01 '25 06:05 CarlOlson

Cool, I didn't know about this. But in my case I have a returned type instead of an external: https://rescript-lang.org/try?version=v12.0.0-alpha.7&module=esmodule&code=AIFwhg5hCmAmAq0C2AHANmE0BQICeK0ABAM4COaRAvEQBRgBODYeAPCSAwJYB2EAfABoijZmw7c+-AJTV+RFAwD2SLiWisAUiQB02pTx0h+2XAWIolStAGEDAMy4RqRAN4BfU8CRLYAVzQcaAAPLAYeMEokMABraABlCgAuOgA-AGMHJxTLazseRwhZKnlyShoAIksOCAZoEgrTQJBSZNbyomi4xLRaDKyIKg9pU20dNCUIWjKAA3srGZGgA

@DZakh Have you solved this problem? I have encountered the same issue as well. https://forum.rescript-lang.org/t/how-to-use-taggedtemplate-generate-js-with-tagged-template-literals/6239

hwywhywl avatar Jul 14 '25 12:07 hwywhywl

I started writing the queries by hand using sql.unsafe, this was actually better for me, since we have a perf critical application. So I'm not using tagged templates now.

DZakh avatar Jul 14 '25 13:07 DZakh