Skip to content

Commit e99226d

Browse files
committed
Enable compiler checks of most doc snippets
Fixes rescript-lang/syntax#126
1 parent 68d1d64 commit e99226d

33 files changed

+218
-202
lines changed

pages/docs/manual/latest/array-and-list.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Arrays are our primary ordered data structure. They work the same way as JavaScr
1212

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

15-
```res
15+
```res example
1616
let myArray = ["hello", "world", "how are you"]
1717
```
1818
```js
@@ -31,7 +31,7 @@ Access & update an array item like so:
3131

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

34-
```res
34+
```res example
3535
let myArray = ["hello", "world", "how are you"]
3636
3737
let firstItem = myArray[0] // "hello"
@@ -59,7 +59,7 @@ ReScript provides a singly linked list too. Lists are:
5959

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

62-
```res
62+
```res example
6363
let myList = list{1, 2, 3}
6464
```
6565
```js
@@ -93,7 +93,7 @@ Use the spread syntax:
9393

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

96-
```res
96+
```res prelude
9797
let myList = list{1, 2, 3}
9898
let anotherList = list{0, ...myList}
9999
```
@@ -129,7 +129,7 @@ Updating an arbitrary item in the middle of a list is also discouraged, since it
129129

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

132-
```res
132+
```res example
133133
let message =
134134
switch myList {
135135
| list{} => "This list is empty"

pages/docs/manual/latest/bind-to-global-js-values.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Some JS values, like `setTimeout`, live in the global scope. You can bind to the
1212

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

15-
```res
15+
```res example
1616
@bs.val external setTimeout: (unit => unit, int) => float = "setTimeout"
1717
@bs.val external clearTimeout: float => unit = "clearTimeout"
1818
```
@@ -38,7 +38,7 @@ We're in a language with a great type system now! Let's leverage a popular featu
3838

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

41-
```res
41+
```res example
4242
type timerId
4343
@bs.val external setTimeout: (unit => unit, int) => timerId = "setTimeout"
4444
@bs.val external clearTimeout: timerId => unit = "clearTimeout"
@@ -66,7 +66,7 @@ If you want to bind to a value inside a global module, e.g. `Math.random`, attac
6666

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

69-
```res
69+
```res example
7070
@bs.scope("Math") @bs.val external random: unit => float = "random"
7171
let someNumber = random()
7272
```
@@ -80,7 +80,7 @@ you can bind to an arbitrarily deep object by passing a tuple to `bs.scope`:
8080

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

83-
```res
83+
```res example
8484
@bs.val @bs.scope(("window", "___location", "ancestorOrigins"))
8585
external length: int = "length"
8686
```
@@ -100,7 +100,7 @@ For these troublesome global values, ReScript provides a special approach: `%ext
100100

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

103-
```res
103+
```res example
104104
switch %external(__DEV__) {
105105
| Some(_) => Js.log("dev mode")
106106
| None => Js.log("production mode")
@@ -124,7 +124,7 @@ Another example:
124124

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

127-
```res
127+
```res example
128128
switch %external(__filename) {
129129
| Some(f) => Js.log(f)
130130
| None => Js.log("non-node environment")

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Binding a JS function is like binding any other value:
1010

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

13-
```res
13+
```res example
1414
// Import nodejs' path.dirname
1515
@bs.module("path") external dirname: string => string = "dirname"
1616
let root = dirname("/User/github") // returns "User"
@@ -42,7 +42,7 @@ It'd be nice if on ReScript's side, we can bind & call `draw` while labeling thi
4242

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

45-
```res
45+
```res example
4646
@bs.module("MyGame")
4747
external draw: (~x: int, ~y: int, ~border: bool=?, unit) => unit = "draw"
4848
@@ -66,7 +66,7 @@ Note that you can change the order of labeled arguments on the ReScript side and
6666

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

69-
```res
69+
```res example
7070
@bs.module("MyGame")
7171
external draw: (~x: int, ~y: int, ~border: bool=?, unit) => unit = "draw"
7272
@@ -86,7 +86,7 @@ Functions attached to a JS objects (other than JS modules) require a special way
8686

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

89-
```res
89+
```res example
9090
type document // abstract type for a document object
9191
@bs.send external getElementById: (document, string) => Dom.element = "getElementById"
9292
@bs.val external doc: document = "document"
@@ -111,7 +111,7 @@ You might have JS functions that take an arbitrary amount of arguments. BuckleSc
111111

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

114-
```res
114+
```res example
115115
@bs.module("path") @bs.variadic
116116
external join: array<string> => string = "join"
117117
@@ -136,7 +136,7 @@ If you can exhaustively enumerate the many forms an overloaded JS function can t
136136

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

139-
```res
139+
```res example
140140
@bs.module("MyGame") external drawCat: unit => unit = "draw"
141141
@bs.module("MyGame") external drawDog: (~giveName: string) => unit = "draw"
142142
@bs.module("MyGame") external draw: (string, ~useRandomAnimal: bool) => unit = "draw"
@@ -169,7 +169,7 @@ Here, `padding` is really conceptually a variant. Let's model it as such.
169169

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

172-
```res
172+
```res example
173173
@bs.val
174174
external padLeft: (
175175
string,
@@ -196,7 +196,7 @@ Consider the Node `fs.readFileSync`'s second argument. It can take a string, but
196196

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

199-
```res
199+
```res example
200200
@bs.module("fs")
201201
external readFileSync: (
202202
~name: string,
@@ -224,7 +224,7 @@ Aside from string, you can also compile an argument to an int, using `bs.int` in
224224

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

227-
```res
227+
```res example
228228
@bs.val
229229
external testIntType: (
230230
@bs.int [
@@ -249,7 +249,7 @@ One last trick with polymorphic variants:
249249

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

252-
```res
252+
```res example
253253
type readline
254254
255255
@bs.send
@@ -287,7 +287,7 @@ Sometimes it's convenient to bind to a function using an `external`, while passi
287287

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

290-
```res
290+
```res example
291291
@bs.val
292292
external processOnExit: (
293293
@bs.as("exit") _,
@@ -314,7 +314,7 @@ Curry is a delicious Indian dish. More importantly, in the context of ReScript (
314314

315315
See the `addFive` intermediate function? `add` takes in 3 arguments but received only 1. It's interpreted as "currying" the argument `5` and waiting for the next 2 arguments to be applied later on. Type signatures:
316316

317-
```resi
317+
```res sig
318318
let add: (int, int, int) => int
319319
let addFive: (int, int) => int
320320
let twelve: int
@@ -344,7 +344,7 @@ ReScript tries to do #1 as much as it can. Even when it bails and uses #2's curr
344344

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

347-
```res
347+
```res example
348348
type timerId
349349
@bs.val external setTimeout: ((. unit) => unit, int) => timerId = "setTimeout"
350350
@@ -372,8 +372,8 @@ Then try `@bs.uncurry`:
372372

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

375-
```res
376-
@bs.send external map: (array<'a>, @bs.uncurry('a => 'b)) => array<'b> = "map"
375+
```res example
376+
@bs.send external map: (array<'a>, @bs.uncurry ('a => 'b)) => array<'b> = "map"
377377
map([1, 2, 3], x => x + 1)
378378
```
379379
```js
@@ -398,7 +398,7 @@ Here, `this` would point to `x` (actually, it depends on how `onload` is called,
398398

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

401-
```res
401+
```res example
402402
type x
403403
@bs.val external x: x = "x"
404404
@bs.set external setOnload: (x, @bs.this ((x, int) => unit)) => unit = "onload"
@@ -422,7 +422,7 @@ For JS functions that return a value that can also be `undefined` or `null`, we
422422

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

425-
```res
425+
```res example
426426
type element
427427
type dom
428428

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If your JavaScript object has fixed fields, then it's conceptually like a ReScri
2525

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

28-
```res
28+
```res example
2929
type person = {
3030
name: string,
3131
friends: array<string>,
@@ -52,7 +52,7 @@ Alternatively, you can use [ReScript object](object.md) to model a JS object too
5252

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

55-
```res
55+
```res example
5656
type person = {
5757
"name": string,
5858
"friends": array<string>,
@@ -77,7 +77,7 @@ Alternatively, you can use `bs.get` and `bs.set` to bind to individual fields of
7777

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

80-
```res
80+
```res example
8181
type textarea
8282
@bs.set external setName: (textarea, string) => unit = "name"
8383
@bs.get external getName: textarea => string = "name"
@@ -91,7 +91,7 @@ You can also use `bs.get_index` and `bs.set_index` to access a dynamic property
9191

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

94-
```res
94+
```res example
9595
type t
9696
@bs.new external create: int => t = "Int32Array"
9797
@bs.get_index external get: (t, int) => int = ""
@@ -124,7 +124,7 @@ Use `bs.new` to emulate e.g. `new Date()`:
124124

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

127-
```res
127+
```res example
128128
type t
129129
@bs.new external createDate: unit => t = "Date"
130130
@@ -140,7 +140,7 @@ You can chain `bs.new` and `bs.module` if the JS module you're importing is itse
140140

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

143-
```res
143+
```res example
144144
type t
145145
@bs.new @bs.module external book: unit => t = "Book"
146146
let myBook = book()

pages/docs/manual/latest/control-flow.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ for(var i = startValueInclusive; i <= endValueInclusive; ++i){
111111

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

114-
```res
114+
```res example
115115
// prints: 1 2 3
116116
for x in 1 to 3 {
117117
Js.log(x)
@@ -146,7 +146,7 @@ for(var i = startValueInclusive; i >= endValueInclusive; --i){
146146

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

149-
```res
149+
```res example
150150
// prints: 3 2 1
151151
for x in 3 downto 1 {
152152
Js.log(x)
@@ -187,7 +187,7 @@ There's no loop-breaking `break` keyword (nor early `return` from functions, for
187187

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

190-
```res
190+
```res example
191191
let break = ref(false)
192192
193193
while !break.contents {

pages/docs/manual/latest/converting-from-js.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ First, copy the entire file content over to a new file called `src/Main.res` by
3939

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

42-
```res
42+
```res example
4343
%%raw(`
4444
const school = require('school');
4545
@@ -94,7 +94,7 @@ Let's turn the `defaultId` variable into a ReScript let-binding:
9494

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

97-
```res
97+
```res example
9898
let defaultId = 10
9999
100100
%%raw(`
@@ -189,7 +189,7 @@ Now this triggers the next type error, that `school` isn't found. Let's use [`ex
189189

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

192-
```res
192+
```res example
193193
@bs.module external school: 'whatever = "school"
194194
195195
let defaultId = 10
@@ -237,7 +237,7 @@ If you prefer more advanced, rigidly typed `payload` and `school`, feel free to
237237

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

240-
```res
240+
```res example
241241
type school
242242
type student
243243
type payload = {

pages/docs/manual/latest/embed-raw-javascript.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ First thing first. If you're ever stuck learning ReScript, remember that you can
1212

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

15-
```res
15+
```res example
1616
%%raw(`
1717
// look ma, regular JavaScript!
1818
var message = "hello";
@@ -39,7 +39,7 @@ While `%%raw` lets you embed top-level raw JS code, `%raw` lets you embed expres
3939

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

42-
```res
42+
```res example
4343
let add = %raw(`
4444
function(a, b) {
4545
console.log("hello from raw JavaScript!");
@@ -73,7 +73,7 @@ You can also drop a `%debugger` expression in a body:
7373

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

76-
```res
76+
```res example
7777
let f = (x, y) => {
7878
%debugger
7979
x + y

0 commit comments

Comments
 (0)