-
-
Notifications
You must be signed in to change notification settings - Fork 251
Syntax lookup for various decorators #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ryyppy
merged 61 commits into
rescript-lang:syntax-lookup-tool
from
kevanstannard:syntax-lookup-decorators
Jan 9, 2021
Merged
Changes from all commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
8baa22d
Add get decorator syntax
4987d0a
Add set decorator syntax
f1f36e8
Add set index decorator syntax
916b5d0
Add get index decorator syntax
f288353
Add val decorator syntax
622dd12
Add new decorator syntax
dfe1cda
Add send decorator syntax
33a7370
Add scope decorator syntax
46a97e8
Add string decorator syntax
1cb11a3
Add int decorator syntax
3557fa6
Add unwrap decorator syntax
49a7b7d
Add as decorator syntax
55d7ff3
Add module decorator syntax
c5332f5
Tweak as decorator syntax
f9d4ea3
Tweak get decorator syntax
c5bd2f9
Tweak get index decorator syntax
37ca04e
Tweak get index decorator syntax
d60db1f
Tweak int decorator syntax
f24d471
Tweak string decorator syntax
f73643e
Tweak module decorator syntax
c674341
Tweak new decorator syntax
43b6e34
Tweak scope decorator syntax
b6502fb
Tweak send decorator syntax
6f53ab3
Tweak set index decorator syntax
ff5f05e
Tweak set decorator syntax
f424dd0
Tweak unwrap decorator syntax
ae11d2a
Tweak val decorator syntax
3af0c98
Add deriving decorator syntax
00e08c8
Tweak as decorator syntax
ff60735
Tweak string decorator syntax
0f08e50
Add variadic decorator syntax
a225816
Add inline decorator syntax
1218319
Add meth decorator syntax
fc454f5
Add return decorator syntax
1c43255
Enable wrapping of tags
5e51a46
Update syntax lookup meta
e8c8ba0
Add unboxed decorator syntax
520155f
Apply suggestions from code review
kevanstannard 2bb206e
Replace "re" with "res" in code snippet types
6cc597e
Tweak as decorator
037a8a1
Tweak as decorator
bf26f7e
Simplify the deriving decorator
b5ece09
Tweak as decorator formatting
8de8283
Update get and set decorators
3a2f7f4
Update inline decorator
0684575
Update int decorator
98b316d
Simplify meth decorator
e248f6d
Update module decorator
f634206
Simplify new decorator
8d58c6c
Simplify return decorator
c764884
Update scope decorator
0be5313
Update send decorator
7598d86
Update string and int decorators
3a779bf
Update unboxed
43e81dc
Update unwrap decorator
1d02953
Update val decorator
9be40cf
Update variadic decorator
5141b49
Clean up, standardise, simplify
f0bc375
Update val decorator
97ddb3a
Add obj decorator
f8b7a78
Fix deriving typo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,36 @@ | ||
--- | ||
test: "foo" | ||
id: "as-decorator" | ||
keywords: ["as", "decorator"] | ||
name: "@as" | ||
summary: "This is the `@as` decorator." | ||
category: "decorators" | ||
--- | ||
|
||
Use this decorator on record types to alias record names to a different JS attribute name. | ||
The `@as` decorator is commonly used on record types to alias record field names to a different JavaScript attribute name. | ||
|
||
This is mostly useful to map to JS attribute names that cannot be expressed in ReScript (such as keywords). | ||
This is useful to map to JavaScript attribute names that cannot be expressed in ReScript (such as keywords). | ||
|
||
### Example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```reason | ||
```res | ||
type action = { | ||
[@bs.as "type"] _type: string, | ||
}; | ||
@as("type") type_: string | ||
} | ||
|
||
let action = {type_: "ADD_USER"} | ||
``` | ||
|
||
```js | ||
var action = { | ||
type: "ADD_USER" | ||
}; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
Refer to the [Records as Objects](/docs/manual/latest/bind-to-js-object#bind-using-rescript-record) section for a more detailed explanation. | ||
### References | ||
|
||
* [Constrain Arguments Better](/docs/manual/latest/bind-to-js-function#constrain-arguments-better) | ||
* [Fixed Arguments](/docs/manual/latest/bind-to-js-function#fixed-arguments) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
id: "deriving-decorator" | ||
keywords: ["deriving", "decorator"] | ||
name: "@deriving" | ||
summary: "This is the `@deriving` decorator." | ||
category: "decorators" | ||
--- | ||
|
||
When the `@deriving` decorator is applied to a **record** type, | ||
it expands the type into a factory function plus a set of | ||
getter/setter functions for its fields. | ||
|
||
> Note that this is an outdated decorator and you may no longer need to use it. | ||
> See [Convert Record Type to Abstract Record](/docs/manual/latest/generate-converters-accessors#convert-record-type-to-abstract-record) for more details. | ||
### Example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
@deriving(abstract) | ||
type person = { | ||
name: string, | ||
age: int, | ||
job: string, | ||
} | ||
let joe = person(~name="Joe", ~age=20, ~job="teacher") | ||
let joeName = nameGet(joe) | ||
let joeAge = ageGet(joe) | ||
let joeJob = jobGet(joe) | ||
``` | ||
|
||
```js | ||
var joe = { | ||
name: "Joe", | ||
age: 20, | ||
job: "teacher" | ||
}; | ||
|
||
var joeName = joe.name; | ||
var joeAge = joe.age; | ||
var joeJob = joe.job; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
* [Convert Record Type to Abstract Record](/docs/manual/latest/generate-converters-accessors#convert-record-type-to-abstract-record) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
id: "get-decorator" | ||
keywords: ["get", "decorator"] | ||
name: "@get" | ||
summary: "This is the `@get` decorator." | ||
category: "decorators" | ||
--- | ||
|
||
The `@get` decorator is used to bind to a property of an object. | ||
|
||
### Example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
type window | ||
@bs.val external window: window = "window" | ||
@bs.get external getName: window => string = "name" | ||
let name = getName(window) | ||
``` | ||
|
||
```js | ||
var name = window.name; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
- [Bind using Special `@bs` Getters & Setters](/docs/manual/latest/bind-to-js-object#bind-using-special-bs-getters--setters) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
id: "get-index-decorator" | ||
keywords: ["get", "index", "decorator"] | ||
name: "@get_index" | ||
summary: "This is the `@get_index` decorator." | ||
category: "decorators" | ||
--- | ||
|
||
The `@get_index` decorator is used to access a dynamic property on an object, | ||
or an index of an array. | ||
|
||
### Example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
type t | ||
|
||
@new external create: unit => t = "Object" | ||
@set_index external set: (t, string, int) => unit = "" | ||
@get_index external get: (t, string) => int = "" | ||
|
||
let o = create() | ||
o->set("x", 1) | ||
o->set("y", 3) | ||
o->set("z", 5) | ||
|
||
let value = o->get("y") | ||
``` | ||
|
||
```js | ||
var o = new Object(); | ||
|
||
o["x"] = 1; | ||
o["y"] = 3; | ||
o["z"] = 5; | ||
|
||
var value = o["y"]; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
kevanstannard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
### References | ||
|
||
- [Bind using Special `@bs` Getters & Setters](/docs/manual/latest/bind-to-js-object#bind-using-special-bs-getters--setters) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
id: "inline-decorator" | ||
keywords: ["inline", "decorator"] | ||
name: "@inline" | ||
summary: "This is the `@inline` decorator." | ||
category: "decorators" | ||
--- | ||
|
||
The `@inline` decorator tells the compiler to inline its value | ||
in every place the binding is being used, rather than use a variable. | ||
|
||
### Example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
module Colors = { | ||
@inline | ||
let green = "green" | ||
|
||
@inline | ||
let red = "red" | ||
} | ||
|
||
let allowedColors = [Colors.green, Colors.red] | ||
``` | ||
|
||
```js | ||
var allowedColors = [ | ||
"green", | ||
"red" | ||
]; | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
- [Inlining Constants](/docs/manual/latest/inlining-constants) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
id: "int-decorator" | ||
keywords: ["int", "decorator"] | ||
name: "@int" | ||
summary: "This is the `@int` decorator." | ||
category: "decorators" | ||
--- | ||
|
||
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. | ||
|
||
### Example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
@val external setStatus: @int[ | ||
@as(0) #NotStarted | | ||
@as(1) #Started | | ||
@as(2) #Done | ||
] => unit = "setStatus" | ||
|
||
setStatus(#Done) | ||
``` | ||
|
||
```js | ||
setStatus(2); | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
* [Constrain Arguments Better](/docs/manual/latest/bind-to-js-function#constrain-arguments-better) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
id: "meth-decorator" | ||
keywords: ["meth", "decorator"] | ||
name: "@meth" | ||
summary: "This is the `@meth` decorator." | ||
category: "decorators" | ||
--- | ||
|
||
The `@meth` decorator is used to call a function on a JavaScript object, | ||
and avoid issues with currying. | ||
|
||
### Example | ||
|
||
Suppose we have the following JavaScript: | ||
|
||
```js | ||
function say (a, b) { | ||
console.log(a, b); | ||
}; | ||
|
||
var john = { | ||
say | ||
}; | ||
``` | ||
|
||
We can model and bind to this object as follows. | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
type person = {@meth "say": (string, string) => unit} | ||
|
||
@val external john: person = "john" | ||
|
||
john["say"]("hey", "jude") | ||
``` | ||
|
||
```js | ||
john.say("hey", "jude"); | ||
``` | ||
|
||
</CodeTab> | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,32 @@ | ||
used for [this](https://google.com) and [that](https://google.com) | ||
--- | ||
id: "module-decorator" | ||
keywords: ["module", "decorator"] | ||
name: "@module" | ||
summary: "This is the `@module` decorator." | ||
category: "decorators" | ||
--- | ||
|
||
The `@module` decorator is used to bind to a JavaScript module. | ||
|
||
### Example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
@module("path") | ||
external dirname: string => string = "dirname" | ||
|
||
let root = dirname("/User/github") | ||
``` | ||
|
||
```js | ||
var Path = require("path"); | ||
|
||
var root = Path.dirname("/User/github"); | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
* [Import from JavaScript](/docs/manual/latest/import-from-export-to-js#import-from-javascript) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
This decorator is used whenever you need to bind to a JS class constructor that requires the `new` keword for instantiation. | ||
--- | ||
id: "new-decorator" | ||
keywords: ["new", "decorator"] | ||
name: "@new" | ||
summary: "This is the `@new` decorator." | ||
category: "decorators" | ||
--- | ||
|
||
The `@new` decorator is used whenever you need to bind to a JavaScript | ||
class constructor that requires the `new` keword for instantiation. | ||
|
||
### Example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
type t | ||
@bs.new external create: unit => t = "Date" | ||
|
||
create(); | ||
@new external create: unit => t = "Date" | ||
|
||
let now = create() | ||
``` | ||
|
||
```js | ||
new Date(); | ||
var now = new Date(); | ||
``` | ||
|
||
</CodeTab> | ||
|
||
When the object is not available on the global scope, combine it with `@module`: | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res | ||
type t; | ||
@module @bs.new external book: unit => t = "Book"; | ||
### References | ||
|
||
let myBook = book(); | ||
``` | ||
|
||
```js | ||
var Book = require("Book"); | ||
var myBook = new Book(); | ||
``` | ||
|
||
</CodeTab> | ||
* [Bind to a JS Object That's a Class](/docs/manual/latest/bind-to-js-object#bind-to-a-js-object-thats-a-class) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.