Skip to content

Commit afa3e0c

Browse files
committed
document tagged templates from rescript-lang/rescript#6250
1 parent 2fd1043 commit afa3e0c

File tree

4 files changed

+95
-6
lines changed

4 files changed

+95
-6
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
type res
18+
@module("bun") @taggedTemplate
19+
external sh: (array<string>, array<string>) => promise<res> = "$"
20+
21+
let filename = "index.res"
22+
let res = await sh`ls ${filename}`
23+
```
24+
25+
```js
26+
import * as $$Bun from "bun";
27+
var filename = "index.res"
28+
var res = await $$Bun.$`ls ${filename}`
29+
```
30+
31+
</CodeTab>
32+
33+
### References
34+
35+
* [Tagged template functions](/docs/manual/latest/bind-to-js-function#tagged_template-functions)

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,34 @@ 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.
430+
All you need to do is defining a binding to a function that has two arrays as arguments,
431+
the first one being an array of strings and the second can be an array of anything.
432+
You add the `@taggedTemplate` annotation and you're good to go!
433+
434+
<CodeTab labels={["ReScript", "JS Output"]}>
435+
436+
```res example
437+
type res
438+
@module("bun") @taggedTemplate
439+
external sh: (array<string>, array<string>) => promise<res> = "$"
440+
441+
let filename = "index.res"
442+
let res = await sh`ls ${filename}`
443+
```
444+
445+
```js
446+
import * as $$Bun from "bun";
447+
var filename = "index.res"
448+
var res = await $$Bun.$`ls ${filename}`
449+
```
450+
451+
</CodeTab>
452+
453+
Notice that it gets compiled to tagged template literals in JS, which allows
454+
to use JS tools that only work on the literals and not by calling directly the tag function.

pages/docs/manual/latest/interop-cheatsheet.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This is a glossary with examples. All the features are described by later pages.
3838
- [`@uncurry`](bind-to-js-function#extra-solution)
3939
- [`@unwrap`](bind-to-js-function#trick-2-polymorphic-variant--bsunwrap)
4040
- [`@val`](bind-to-global-js-values#global-modules)
41+
- [`@taggedTemplate`](bind-to-js-function#tagged_template-functions)
4142

4243
- [`@deprecated`](attribute#usage)
4344
- [`genType`](https://github.com/reason-association/genType)
@@ -193,6 +194,27 @@ external join: array<string> => string = "join"
193194

194195
</CodeTab>
195196

197+
### Tagged template functions
198+
199+
<CodeTab labels={["ReScript", "JS Output"]}>
200+
201+
```res example
202+
type res
203+
@module("bun") @taggedTemplate
204+
external sh: (array<string>, array<string>) => promise<res> = "$"
205+
206+
let filename = "index.res"
207+
let res = await sh`ls ${filename}`
208+
```
209+
210+
```js
211+
import * as $$Bun from "bun";
212+
var filename = "index.res"
213+
var res = await $$Bun.$`ls ${filename}`
214+
```
215+
216+
</CodeTab>
217+
196218
### Polymorphic Function
197219

198220
<CodeTab labels={["ReScript", "JS Output"]}>

pages/docs/manual/latest/overview.mdx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ canonical: "/docs/manual/latest/overview"
3434

3535
### String & Character
3636

37-
| JavaScript | ReScript |
38-
| --------------------------| --------------------- |
39-
| `"Hello world!"` | Same |
40-
| `'Hello world!'` | Strings must use `"` |
41-
| `"hello " + "world"` | `"hello " ++ "world"` |
42-
| `` `hello ${message}` `` | Same |
37+
| JavaScript | ReScript |
38+
| ----------------------------- | --------------------- |
39+
| `"Hello world!"` | Same |
40+
| `'Hello world!'` | Strings must use `"` |
41+
| `"hello " + "world"` | `"hello " ++ "world"` |
42+
| `` `hello ${message}` `` | Same |
43+
| `` sql`select ${fnName};` `` | Same |
4344

4445
### Boolean
4546

0 commit comments

Comments
 (0)