-
Notifications
You must be signed in to change notification settings - Fork 38
Add support for tagged template strings #471
Add support for tagged template strings #471
Conversation
cc @tsnobip |
Huge if true 🤯 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a short generic review mostly about OCaml good practices given you told me you're rather new to it.
I'll try to work on it in the coming days to make some progress :)
I've added location data so I'll push those changes this evening. I'll also try to get some tests for the parse tree generated from tagged templates. |
@tsnobip thanks for the style suggestions. I'll make those changes this evening as well. |
…n data to nodes in parseTemplateExpr
@@ -2217,59 +2228,84 @@ and parseBinaryExpr ?(context=OrdinaryExpr) ?a p prec = | |||
(* ) *) | |||
|
|||
and parseTemplateExpr ?(prefix="js") p = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's best to review the changes to this function using the split view and ignore the left side completely as if its contents have been completely replaced.
let x = `https://www.apple.com` | ||
let x = js`https://www.apple.com` | ||
let x = "https://www.apple.com" | ||
let x = sql`https://www.apple.com` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I ended up converting this file from .ml to .res is that when parsing the .ml file this is parsed as a string (with prefix sql
) where as when it's a .res file we parse it as an apply node with the callExpr being sql
.
While we could have the res
parser work the same as the ml
parser and then do an AST transform to cover the string to a function call, it would be a pretty complicate transform to do since template strings end up getting parsed as nested ^
calls if there are any interpolations.
Also, if ReScript is supposed to be independent of OCaml, I'm not sure what the benefit is of trying to keep the parse trees we generate for the two in sync.
I see you are compiling tagged template to function calls, why not compile to es6 tagged template syntax? (The reason is that this helps with some GraphQL tooling that will pick out these template literals out of the code). The function application would be effectively the same (I think), so it's just different syntax. Let me know if you need help here, because I would love to have this feature in ReScript for the new iteration of |
@jfrolich updating the code generated requires changes to rescript-compiler. I was planning to do what you suggest in a separate PR for that, but having to make changes in separate repos isn't the greatest so I'm going to merge this repo into the compiler repo and then recreate this PR afterwards. |
I've created rescript-lang/rescript#5347 to merge the |
Closing this in preference of kevinbarabash/rescript-compiler#2. |
This PR adds support for compiling tagged template strings to function calls. I've verified the original implementation within the rescript-compiler repo before moving the code over to the syntax repo.
The following TODOs are done now and this is ready for review:
Pexp_apply
nodes with the appropriate attribute)"res.taggedTemplate"
when parsing/printing ocaml files)