String interpolations in %%raw

Hi Folks!
Looking at rescript for some components at work.
I have put my js code into a %%raw block as directed but the compiler seems to find the back ticks of my existing string interpolations as the end of the raw block and freaks out. Am I missing something?
Im sure you all Just went over this but a different escape character, at first thought?

TIA
Alex

Ah, other escapes but seemingly only the other javascript quotes? a unicode hard angle quote or something would be nice. ❯

Can you show some example code? And the error message? Difficult to help otherwise :wink:

%%raw(`
// look ma, regular JavaScript!
var message = `${"hello"}`;
function greet(m) {
  console.log(m)
}
`)

Syntax error!

[highlights $]

Hmm, I have no idea what this character means…

Interesting! Looks like it’s parsing upto this part:

%%raw(`
// look ma, regular JavaScript!
var message = `

…and treating that as the string payload of the raw block. After that it hits the $ and gets confused.

To escape a backtick in a template literal, put a backslash \ before the backtick.
Otherwise if you use the ` inside, it would be picked up as “close this template string”.

Same situation for ${

I assume you meant to write:

%%raw(`
// look ma, regular JavaScript!
var message = \`\${"hello"}\`;
function greet(m) {
  console.log(m)
}
`)

which compiles to the following JS

// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';


// look ma, regular JavaScript!
var message = `${"hello"}`;
function greet(m) {
  console.log(m)
}

https://rescript-lang.org/try?code=KTBOEMHcAoAMCgD0iAEAbA9hg1igtuADQqgCmA5gK5rigoBS4AbuAMoDGoAlgA4AuAQngs6eUgGdx4cqRQBeFAB1YigCQBvAEQALUmkyaAvsoDc8AGaUAduz5cMVlOTKk+0PAEoU6+ChTsHcQw0UgA6THJ3D3hDeFgPIA

2 Likes

Yep fully aware of that workaround and I have done that.

But as the comment states [from the intro documentation] “regular JavaScript”. If i have go and escape the code every time I do this, I think that claim is unfounded. All common javascript will have single quotes and string interp quotes so think this experience will be quite common.

Anyways Im done with it now but for future I found this as a pain point in the tutorial path for rescript, and I think it can be better.

Thanks
A

2 Likes

This seems to be a regression in v10 of rescript.

https://rescript-lang.org/try?code=KTBOEMHcAoAMCgD0iAEAbA9hg1igtuADQqgCmA5gK5rigoBS4AbuAMoDGoAlgA4AuAQngs6eUgGdx4cqRQBeFAB1YigCQBvAEQALUmkyaAvsoDc8AGaUAduz5cMVlOTKk+0PAEoU6+ChTsHcQw0UgA6THJ3D3hDeFgPIA

What’s the difference between backtick and double quotes?
I tried embedding js code in double quotes instead backticks. Seems it works.
But I had to change double quotes around ‘hello’ to single quotes.