Skip to content

Commit 3aa8029

Browse files
authored
Merge pull request #177 from kevanstannard/syntax-lookup-decorators
Syntax lookup for various decorators
2 parents c5551ab + f8b7a78 commit 3aa8029

23 files changed

+706
-37
lines changed

misc_docs/syntax/decorator_as.mdx

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
---
2-
test: "foo"
2+
id: "as-decorator"
3+
keywords: ["as", "decorator"]
4+
name: "@as"
5+
summary: "This is the `@as` decorator."
6+
category: "decorators"
37
---
48

5-
Use this decorator on record types to alias record names to a different JS attribute name.
9+
The `@as` decorator is commonly used on record types to alias record field names to a different JavaScript attribute name.
610

7-
This is mostly useful to map to JS attribute names that cannot be expressed in ReScript (such as keywords).
11+
This is useful to map to JavaScript attribute names that cannot be expressed in ReScript (such as keywords).
812

913
### Example
1014

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

13-
```reason
17+
```res
1418
type action = {
15-
[@bs.as "type"] _type: string,
16-
};
19+
@as("type") type_: string
20+
}
21+
22+
let action = {type_: "ADD_USER"}
1723
```
1824

1925
```js
2026
var action = {
2127
type: "ADD_USER"
2228
};
2329
```
30+
2431
</CodeTab>
2532

26-
Refer to the [Records as Objects](/docs/manual/latest/bind-to-js-object#bind-using-rescript-record) section for a more detailed explanation.
33+
### References
34+
35+
* [Constrain Arguments Better](/docs/manual/latest/bind-to-js-function#constrain-arguments-better)
36+
* [Fixed Arguments](/docs/manual/latest/bind-to-js-function#fixed-arguments)
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
id: "deriving-decorator"
3+
keywords: ["deriving", "decorator"]
4+
name: "@deriving"
5+
summary: "This is the `@deriving` decorator."
6+
category: "decorators"
7+
---
8+
9+
When the `@deriving` decorator is applied to a **record** type,
10+
it expands the type into a factory function plus a set of
11+
getter/setter functions for its fields.
12+
13+
> Note that this is an outdated decorator and you may no longer need to use it.
14+
> See [Convert Record Type to Abstract Record](/docs/manual/latest/generate-converters-accessors#convert-record-type-to-abstract-record) for more details.
15+
16+
### Example
17+
18+
<CodeTab labels={["ReScript", "JS Output"]}>
19+
20+
```res
21+
@deriving(abstract)
22+
type person = {
23+
name: string,
24+
age: int,
25+
job: string,
26+
}
27+
28+
let joe = person(~name="Joe", ~age=20, ~job="teacher")
29+
30+
let joeName = nameGet(joe)
31+
let joeAge = ageGet(joe)
32+
let joeJob = jobGet(joe)
33+
```
34+
35+
```js
36+
var joe = {
37+
name: "Joe",
38+
age: 20,
39+
job: "teacher"
40+
};
41+
42+
var joeName = joe.name;
43+
var joeAge = joe.age;
44+
var joeJob = joe.job;
45+
```
46+
47+
</CodeTab>
48+
49+
### References
50+
51+
* [Convert Record Type to Abstract Record](/docs/manual/latest/generate-converters-accessors#convert-record-type-to-abstract-record)
52+

misc_docs/syntax/decorator_get.mdx

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
id: "get-decorator"
3+
keywords: ["get", "decorator"]
4+
name: "@get"
5+
summary: "This is the `@get` decorator."
6+
category: "decorators"
7+
---
8+
9+
The `@get` decorator is used to bind to a property of an object.
10+
11+
### Example
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res
16+
type window
17+
@bs.val external window: window = "window"
18+
@bs.get external getName: window => string = "name"
19+
20+
let name = getName(window)
21+
```
22+
23+
```js
24+
var name = window.name;
25+
```
26+
27+
</CodeTab>
28+
29+
### References
30+
31+
- [Bind using Special `@bs` Getters & Setters](/docs/manual/latest/bind-to-js-object#bind-using-special-bs-getters--setters)
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: "get-index-decorator"
3+
keywords: ["get", "index", "decorator"]
4+
name: "@get_index"
5+
summary: "This is the `@get_index` decorator."
6+
category: "decorators"
7+
---
8+
9+
The `@get_index` decorator is used to access a dynamic property on an object,
10+
or an index of an array.
11+
12+
### Example
13+
14+
<CodeTab labels={["ReScript", "JS Output"]}>
15+
16+
```res
17+
type t
18+
19+
@new external create: unit => t = "Object"
20+
@set_index external set: (t, string, int) => unit = ""
21+
@get_index external get: (t, string) => int = ""
22+
23+
let o = create()
24+
o->set("x", 1)
25+
o->set("y", 3)
26+
o->set("z", 5)
27+
28+
let value = o->get("y")
29+
```
30+
31+
```js
32+
var o = new Object();
33+
34+
o["x"] = 1;
35+
o["y"] = 3;
36+
o["z"] = 5;
37+
38+
var value = o["y"];
39+
```
40+
41+
</CodeTab>
42+
43+
### References
44+
45+
- [Bind using Special `@bs` Getters & Setters](/docs/manual/latest/bind-to-js-object#bind-using-special-bs-getters--setters)

misc_docs/syntax/decorator_inline.mdx

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
id: "inline-decorator"
3+
keywords: ["inline", "decorator"]
4+
name: "@inline"
5+
summary: "This is the `@inline` decorator."
6+
category: "decorators"
7+
---
8+
9+
The `@inline` decorator tells the compiler to inline its value
10+
in every place the binding is being used, rather than use a variable.
11+
12+
### Example
13+
14+
<CodeTab labels={["ReScript", "JS Output"]}>
15+
16+
```res
17+
module Colors = {
18+
@inline
19+
let green = "green"
20+
21+
@inline
22+
let red = "red"
23+
}
24+
25+
let allowedColors = [Colors.green, Colors.red]
26+
```
27+
28+
```js
29+
var allowedColors = [
30+
"green",
31+
"red"
32+
];
33+
```
34+
35+
</CodeTab>
36+
37+
### References
38+
39+
- [Inlining Constants](/docs/manual/latest/inlining-constants)

misc_docs/syntax/decorator_int.mdx

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
id: "int-decorator"
3+
keywords: ["int", "decorator"]
4+
name: "@int"
5+
summary: "This is the `@int` decorator."
6+
category: "decorators"
7+
---
8+
9+
The `@int` decorator can be used with [polymorphic variants](/docs/manual/latest/polymorphic-variant) and the `@as` decorator on *externals* to modify the compiled JavaScript to use integers for the values instead of strings.
10+
11+
### Example
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res
16+
@val external setStatus: @int[
17+
@as(0) #NotStarted |
18+
@as(1) #Started |
19+
@as(2) #Done
20+
] => unit = "setStatus"
21+
22+
setStatus(#Done)
23+
```
24+
25+
```js
26+
setStatus(2);
27+
```
28+
29+
</CodeTab>
30+
31+
### References
32+
33+
* [Constrain Arguments Better](/docs/manual/latest/bind-to-js-function#constrain-arguments-better)
34+
35+

misc_docs/syntax/decorator_meth.mdx

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: "meth-decorator"
3+
keywords: ["meth", "decorator"]
4+
name: "@meth"
5+
summary: "This is the `@meth` decorator."
6+
category: "decorators"
7+
---
8+
9+
The `@meth` decorator is used to call a function on a JavaScript object,
10+
and avoid issues with currying.
11+
12+
### Example
13+
14+
Suppose we have the following JavaScript:
15+
16+
```js
17+
function say (a, b) {
18+
console.log(a, b);
19+
};
20+
21+
var john = {
22+
say
23+
};
24+
```
25+
26+
We can model and bind to this object as follows.
27+
28+
<CodeTab labels={["ReScript", "JS Output"]}>
29+
30+
```res
31+
type person = {@meth "say": (string, string) => unit}
32+
33+
@val external john: person = "john"
34+
35+
john["say"]("hey", "jude")
36+
```
37+
38+
```js
39+
john.say("hey", "jude");
40+
```
41+
42+
</CodeTab>
43+
44+
45+

misc_docs/syntax/decorator_module.mdx

+32-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
1-
used for [this](https://google.com) and [that](https://google.com)
1+
---
2+
id: "module-decorator"
3+
keywords: ["module", "decorator"]
4+
name: "@module"
5+
summary: "This is the `@module` decorator."
6+
category: "decorators"
7+
---
8+
9+
The `@module` decorator is used to bind to a JavaScript module.
10+
11+
### Example
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res
16+
@module("path")
17+
external dirname: string => string = "dirname"
18+
19+
let root = dirname("/User/github")
20+
```
21+
22+
```js
23+
var Path = require("path");
24+
25+
var root = Path.dirname("/User/github");
26+
```
27+
28+
</CodeTab>
29+
30+
### References
31+
32+
* [Import from JavaScript](/docs/manual/latest/import-from-export-to-js#import-from-javascript)

misc_docs/syntax/decorator_new.mdx

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
This decorator is used whenever you need to bind to a JS class constructor that requires the `new` keword for instantiation.
1+
---
2+
id: "new-decorator"
3+
keywords: ["new", "decorator"]
4+
name: "@new"
5+
summary: "This is the `@new` decorator."
6+
category: "decorators"
7+
---
8+
9+
The `@new` decorator is used whenever you need to bind to a JavaScript
10+
class constructor that requires the `new` keword for instantiation.
11+
12+
### Example
213

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

516
```res
617
type t
7-
@bs.new external create: unit => t = "Date"
818
9-
create();
19+
@new external create: unit => t = "Date"
20+
21+
let now = create()
1022
```
1123

1224
```js
13-
new Date();
25+
var now = new Date();
1426
```
1527

1628
</CodeTab>
1729

18-
When the object is not available on the global scope, combine it with `@module`:
19-
20-
<CodeTab labels={["ReScript", "JS Output"]}>
21-
22-
```res
23-
type t;
24-
@module @bs.new external book: unit => t = "Book";
30+
### References
2531

26-
let myBook = book();
27-
```
28-
29-
```js
30-
var Book = require("Book");
31-
var myBook = new Book();
32-
```
33-
34-
</CodeTab>
32+
* [Bind to a JS Object That's a Class](/docs/manual/latest/bind-to-js-object#bind-to-a-js-object-thats-a-class)

0 commit comments

Comments
 (0)