Skip to content

Commit 09bb722

Browse files
Syntax Lookup: Extension Points (#444)
* Add Syntax Lookup Extension Points * Add Syntax Lookup Private Let Binding * Rename Syntax Lookup Raw JS * Add top level raw import example * Update misc_docs/syntax/extension_identity.mdx Co-authored-by: Patrick Ecker <[email protected]> * Update misc_docs/syntax/extension_regular_expression.mdx Co-authored-by: Patrick Ecker <[email protected]> * Update misc_docs/syntax/extension_raw_top_level.mdx Co-authored-by: Patrick Ecker <[email protected]> * Update misc_docs/syntax/extension_raw_top_level.mdx Co-authored-by: Patrick Ecker <[email protected]> * Update misc_docs/syntax/extension_raw_expression.mdx Co-authored-by: Patrick Ecker <[email protected]> * Update misc_docs/syntax/extension_raw_expression.mdx Co-authored-by: Patrick Ecker <[email protected]> * Rename raw top level expression file Co-authored-by: Patrick Ecker <[email protected]>
1 parent f9a17d9 commit 09bb722

8 files changed

+241
-7
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
id: "debugger"
3+
keywords: ["javascript", "embed", "raw", "debugger"]
4+
name: "%debugger"
5+
summary: "This is the `debugger` extension point."
6+
category: "extensionpoints"
7+
---
8+
9+
`%debugger` is used to insert a JavaScript `debugger` statement.
10+
11+
<CodeTab labels={["ReScript", "JS Output"]}>
12+
13+
```res
14+
let f = (x, y) => {
15+
%debugger
16+
x + y
17+
}
18+
```
19+
20+
```js
21+
function f(x, y) {
22+
debugger;
23+
return (x + y) | 0;
24+
}
25+
```
26+
27+
</CodeTab>
28+
29+
### References
30+
31+
* [Embed Raw JavaScript: Debugger](/docs/manual/latest/embed-raw-javascript#debugger)
32+
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
id: "identity"
3+
keywords: ["identity", "external", "type", "convert"]
4+
name: "%identity"
5+
summary: "This is the `identity` extension point."
6+
category: "extensionpoints"
7+
---
8+
9+
`%identity` is used with `external` to do an **unsafe conversion** of a value from one type to another type.
10+
11+
<CodeTab labels={["ReScript", "JS Output"]}>
12+
13+
```res
14+
external convertToFloat: int => float = "%identity"
15+
let age = 10
16+
let gpa = 2.1 +. convertToFloat(age)
17+
```
18+
19+
```js
20+
var gpa = 2.1 + 10;
21+
var age = 10;
22+
```
23+
24+
</CodeTab>
25+
26+
### References
27+
28+
* [Type Escape Hatch](/docs/manual/latest/type#type-escape-hatch)
29+
* [Dangerous Type Cast](/docs/manual/latest/interop-cheatsheet#dangerous-type-cast)
30+
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point)
31+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
id: "private-let"
3+
keywords: ["private", "let"]
4+
name: "%%private"
5+
summary: "This is the `private let binding` extension point."
6+
category: "extensionpoints"
7+
---
8+
9+
`%%private` is used to make let bindings private.
10+
11+
<CodeTab labels={["ReScript", "JS Output"]}>
12+
13+
```res
14+
module Calc = {
15+
%%private(let mult = (x, y) => x * y)
16+
17+
let double = x => mult(x, 2)
18+
let triple = x => mult(x, 3)
19+
}
20+
```
21+
22+
```js
23+
function $$double(x) {
24+
return x << 1;
25+
}
26+
27+
function triple(x) {
28+
return Math.imul(x, 3);
29+
}
30+
31+
var Calc = {
32+
$$double: $$double,
33+
triple: triple,
34+
};
35+
```
36+
37+
</CodeTab>
38+
39+
### References
40+
41+
* [Private Let Bindings](/docs/manual/latest/let-binding#private-let-bindings)
42+
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point)
43+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
id: "raw-expression"
3+
keywords: ["javascript", "raw", "expression"]
4+
name: "%raw"
5+
summary: "This is the `raw expression` extension point."
6+
category: "extensionpoints"
7+
---
8+
9+
`%raw` is used to embed "raw JavaScript expressions".
10+
11+
<CodeTab labels={["ReScript", "JS Output"]}>
12+
13+
```res
14+
let canUseCanvas: unit => bool = %raw(`
15+
function canUseCanvas() {
16+
return !!document.createElement('canvas').getContext;
17+
}
18+
`)
19+
```
20+
21+
```js
22+
var canUseCanvas = function canUseCanvas() {
23+
return !!document.createElement("canvas").getContext;
24+
};
25+
```
26+
27+
</CodeTab>
28+
29+
See `%%raw` for embedding top level blocks of JavaScript code rather than expressions.
30+
31+
### References
32+
33+
* [Embed Raw JavaScript](/docs/manual/latest/embed-raw-javascript)
34+
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
id: "raw-top-level-expression"
3+
keywords: ["javascript", "raw"]
4+
name: "%%raw"
5+
summary: "This is the `raw top level expression` extension point."
6+
category: "extensionpoints"
7+
---
8+
9+
`%%raw` is used to embed top level JavaScript code.
10+
11+
<CodeTab labels={["ReScript", "JS Output"]}>
12+
13+
```res
14+
%%raw(`
15+
const message = "hello";
16+
17+
function greet(m) {
18+
console.log(m)
19+
}
20+
21+
greet(message)
22+
`)
23+
```
24+
25+
```js
26+
const message = "hello";
27+
28+
function greet(m) {
29+
console.log(m);
30+
}
31+
32+
greet(message);
33+
```
34+
35+
</CodeTab>
36+
37+
It's also very useful to do imports with side-effects like this:
38+
39+
<CodeTab labels={["ReScript", "JS Output"]}>
40+
41+
```res
42+
%%raw(`import "main.css"`)
43+
```
44+
45+
```js
46+
import "main.css";
47+
```
48+
49+
</CodeTab>
50+
51+
See `%raw` for embedding JavaScript expressions rather than top level blocks of code.
52+
53+
### References
54+
55+
* [Embed Raw JavaScript](/docs/manual/latest/embed-raw-javascript)
56+
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point)
57+
* [Converting from JS](/docs/manual/latest/converting-from-js)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
id: "regular-expression"
3+
keywords: ["regular", "expression", "re"]
4+
name: "%re"
5+
summary: "This is the `regular expression` extension point."
6+
category: "extensionpoints"
7+
---
8+
9+
`%re` is used to create JavaScript regular expressions.
10+
11+
<CodeTab labels={["ReScript", "JS Output"]}>
12+
13+
```res
14+
let regex = %re("/^hello/")
15+
let result = regex->Js.Re.test_("hello world")
16+
```
17+
18+
```js
19+
var regex = /^hello/;
20+
var result = regex.test("hello world");
21+
```
22+
23+
</CodeTab>
24+
25+
### References
26+
27+
* [Regular Expressions](/docs/manual/latest/primitive-types#regular-expression)
28+
* [Extension Point Attributes](/docs/manual/latest/attribute#extension-point)
29+
* [Js.Re API](/docs/manual/latest/api/js/re)

src/SyntaxLookup.mjs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ function toString(t) {
3030
return "Operators";
3131
case /* LanguageConstructs */2 :
3232
return "Language Constructs";
33-
case /* SpecialValues */3 :
33+
case /* ExtensionPoints */3 :
34+
return "Extension Points";
35+
case /* SpecialValues */4 :
3436
return "Special Values";
35-
case /* Other */4 :
37+
case /* Other */5 :
3638
return "Other";
3739

3840
}
@@ -42,14 +44,16 @@ function fromString(s) {
4244
switch (s) {
4345
case "decorators" :
4446
return /* Decorators */0;
47+
case "extensionpoints" :
48+
return /* ExtensionPoints */3;
4549
case "languageconstructs" :
4650
return /* LanguageConstructs */2;
4751
case "operators" :
4852
return /* Operators */1;
4953
case "specialvalues" :
50-
return /* SpecialValues */3;
54+
return /* SpecialValues */4;
5155
default:
52-
return /* Other */4;
56+
return /* Other */5;
5357
}
5458
}
5559

@@ -244,8 +248,9 @@ function SyntaxLookup(Props) {
244248
/* Decorators */0,
245249
/* Operators */1,
246250
/* LanguageConstructs */2,
247-
/* SpecialValues */3,
248-
/* Other */4
251+
/* ExtensionPoints */3,
252+
/* SpecialValues */4,
253+
/* Other */5
249254
], (function (cat) {
250255
return [
251256
toString(cat),

src/SyntaxLookup.res

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ let requireSyntaxFile: string => MdxComp.t = %raw(`
3131
`)
3232

3333
module Category = {
34-
type t = Decorators | Operators | LanguageConstructs | SpecialValues | Other
34+
type t = Decorators | Operators | LanguageConstructs | ExtensionPoints | SpecialValues | Other
3535

3636
let toString = t =>
3737
switch t {
3838
| Decorators => "Decorators"
3939
| Operators => "Operators"
40+
| ExtensionPoints => "Extension Points"
4041
| LanguageConstructs => "Language Constructs"
4142
| SpecialValues => "Special Values"
4243
| Other => "Other"
@@ -48,6 +49,7 @@ module Category = {
4849
| "specialvalues" => SpecialValues
4950
| "operators" => Operators
5051
| "languageconstructs" => LanguageConstructs
52+
| "extensionpoints" => ExtensionPoints
5153
| _ => Other
5254
}
5355
}
@@ -241,6 +243,7 @@ let make = () => {
241243
Decorators,
242244
Operators,
243245
LanguageConstructs,
246+
ExtensionPoints,
244247
SpecialValues,
245248
Other,
246249
]->Belt.Array.map(cat => {

0 commit comments

Comments
 (0)