Skip to content

Commit 444f600

Browse files
Merge pull request #803 from tsnobip/tagged_templates
Document tagged templates
2 parents 8f4d7a3 + 137e18d commit 444f600

16 files changed

+400
-391
lines changed

compilers/package-lock.json

Lines changed: 132 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compilers/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
"license": "MIT",
77
"dependencies": {
88
"@rescript/core": "^0.6.0",
9+
"@rescript/react": "^0.12.0",
910
"rescript-1000": "npm:[email protected]",
1011
"rescript-1010": "npm:[email protected]",
1112
"rescript-1100": "npm:[email protected]",
13+
"rescript-1110": "npm:[email protected]",
1214
"rescript-820": "npm:[email protected]",
1315
"rescript-902": "npm:[email protected]",
1416
"rescript-912": "npm:[email protected]"

compilers/rescript.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "dummy",
3+
"jsx": { "version": 4 },
34
"sources": {
45
"dir": "dummy",
56
"subdirs": true
67
},
78
"bs-dependencies": [
8-
"@rescript/core"
9+
"@rescript/core",
10+
"@rescript/react"
911
],
1012
"bsc-flags": [
1113
"-open RescriptCore"

data/sidebar_manual_latest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"lazy-values",
2929
"promise",
3030
"async-await",
31+
"tagged-templates",
3132
"module",
3233
"import-export",
3334
"attribute",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
id: "taggedTemplate-decorator"
3+
keywords: ["taggedTemplate", "tagged", "template", "decorator"]
4+
name: "@taggedTemplate"
5+
summary: "This is the `@taggedTemplate` decorator."
6+
category: "decorators"
7+
---
8+
**Since 11.1**
9+
10+
The `@taggedTemplate` decorator is used to bind to JavaScript tag functions.
11+
12+
### Example
13+
14+
<CodeTab labels={["ReScript", "JS Output"]}>
15+
16+
```res example
17+
// see https://bun.sh/docs/runtime/shell
18+
type result = {exitCode: int}
19+
@module("bun") @taggedTemplate
20+
external sh: (array<string>, array<string>) => promise<result> = "$"
21+
22+
let filename = "index.res"
23+
let result = await sh`ls ${filename}`
24+
```
25+
26+
```js
27+
import * as $$Bun from "bun";
28+
var filename = "index.res";
29+
var result = await $$Bun.$`ls ${filename}`;
30+
```
31+
32+
</CodeTab>
33+
34+
### References
35+
36+
* [Tagged template functions](/docs/manual/latest/bind-to-js-function#tagged_template-functions)

package-lock.json

Lines changed: 19 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"gentype": "^3.44.0",
2727
"glob": "^7.1.4",
2828
"gray-matter": "^4.0.3",
29-
"highlight.js": "^10.5.0",
29+
"highlight.js": "^11.9.0",
30+
"highlightjs-rescript": "^0.1.2",
3031
"lz-string": "^1.4.4",
3132
"next": "^13.1.1",
3233
"next-mdx-remote": "^4.4.1",
@@ -42,8 +43,8 @@
4243
"remark-parse": "^10.0.2",
4344
"remark-slug": "^5.1.2",
4445
"remark-stringify": "^7.0.3",
45-
"rescript": "^11.0.0",
4646
"request": "^2.88.0",
47+
"rescript": "^11.0.0",
4748
"stringify-object": "^3.3.0",
4849
"unified": "^8.4.0"
4950
},

pages/docs/manual/latest/bind-to-js-function.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,39 @@ Currently 4 directives are supported: `null_to_opt`, `undefined_to_opt`, `nullab
421421
<!-- When the return type is unit: the compiler will append its return value with an OCaml unit literal to make sure it does return unit. Its main purpose is to make the user consume FFI in idiomatic OCaml code, the cost is very very small and the compiler will do smart optimizations to remove it when the returned value is not used (mostly likely). -->
422422

423423
`identity` will make sure that compiler will do nothing about the returned value. It is rarely used, but introduced here for debugging purpose.
424+
425+
## Tagged template functions
426+
427+
**Since 11.1**
428+
429+
**Experimental** You can easily bind to [JS tagged template functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates).
430+
Tag functions in JS expect as input an array of strings and variadic parameters for the arguments of the interpolation.
431+
To bind to those functions in ReScript, the binding signature must have two arrays as arguments,
432+
the first one being an array of strings and the second can be an array of anything.
433+
You add the `@taggedTemplate` annotation and you're good to go!
434+
435+
<CodeTab labels={["ReScript", "JS Output"]}>
436+
437+
```res example
438+
// see https://bun.sh/docs/runtime/shell
439+
type result = {exitCode: int}
440+
@module("bun") @taggedTemplate
441+
external sh: (array<string>, array<string>) => promise<result> = "$"
442+
443+
let filename = "index.res"
444+
let result = await sh`ls ${filename}`
445+
```
446+
447+
```js
448+
import * as $$Bun from "bun";
449+
var filename = "index.res";
450+
var result = await $$Bun.$`ls ${filename}`;
451+
```
452+
453+
</CodeTab>
454+
455+
Notice that it gets compiled to tagged template literals in JS, which allows
456+
to use JS tools that only work on the literals and not by calling directly the tag function.
457+
458+
There are plenty of useful JS tools you can bind to, like [`gql`](https://github.com/apollographql/graphql-tag),
459+
[`sql`](https://github.com/porsager/postgres), [`css`](https://github.com/mayank99/ecsstatic) and a lot others!

0 commit comments

Comments
 (0)