Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Add support for tagged template strings #471

Conversation

kevinbarabash
Copy link
Contributor

@kevinbarabash kevinbarabash commented Dec 19, 2021

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:

  • add tests that verify the parse tree (these should show that tagged templates get parsed as Pexp_apply nodes with the appropriate attribute)
  • add location data to parsed nodes when available
  • remove commented out code (I left it in for now since I want to be able to easily reference it for to help with the previous bullet point)
  • figure out why there's an extra carriage return in the printer out
  • fix the tests/printer/other/ocamlString.ml roundtrip tests (essential we don't want to use the "res.taggedTemplate" when parsing/printing ocaml files)
  • recreate on top of Merge syntax repo rescript#5347
  • update codegen to output tagged template syntax instead of function call when generating ES6 code

@kevinbarabash
Copy link
Contributor Author

cc @tsnobip

@jfrolich
Copy link

Huge if true 🤯

Copy link
Member

@tsnobip tsnobip left a 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 :)

@kevinbarabash
Copy link
Contributor Author

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.

@kevinbarabash
Copy link
Contributor Author

@tsnobip thanks for the style suggestions. I'll make those changes this evening as well.

@@ -2217,59 +2228,84 @@ and parseBinaryExpr ?(context=OrdinaryExpr) ?a p prec =
(* ) *)

and parseTemplateExpr ?(prefix="js") p =
Copy link
Contributor Author

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.

@kevinbarabash kevinbarabash changed the title Add support for tagged template strings [WIP] Add support for tagged template strings Dec 23, 2021
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`
Copy link
Contributor Author

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.

@jfrolich
Copy link

jfrolich commented Dec 24, 2021

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 graphql-ppx, not having this is the largest barrier for outputting the right code.

@kevinbarabash
Copy link
Contributor Author

kevinbarabash commented Dec 24, 2021

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

@kevinbarabash
Copy link
Contributor Author

I've created rescript-lang/rescript#5347 to merge the syntax repo into rescript-compiler. I'll work on re-creating this PR on top of that change over the weekend.

@kevinbarabash
Copy link
Contributor Author

Closing this in preference of kevinbarabash/rescript-compiler#2.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants