Source file server_reason_react_ppx.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
open Ppxlib
open Ast_builder.Default
module List = ListLabels
let repo_url = "https://github.com/ml-in-barcelona/server-reason-react"
let issues_url = repo_url |> Printf.sprintf "%s/issues"
let pexp_list ~loc xs =
List.fold_left (List.rev xs) ~init:[%expr []] ~f:(fun xs x ->
let loc = x.pexp_loc in
[%expr [%e x] :: [%e xs]])
exception Error of expression
let raise_errorf ~loc fmt =
let open Ast_builder.Default in
Printf.ksprintf
(fun msg ->
let expr =
pexp_extension ~loc (Location.error_extensionf ~loc "%s" msg)
in
raise (Error expr))
fmt
let make_string ~loc str =
let open Ast_helper in
Ast_helper.Exp.constant ~loc (Const.string str)
let hasAttr { attr_name; _ } = attr_name.txt = "react.component"
let otherAttrsPure { attr_name; _ } = attr_name.txt <> "react.component"
let hasAttrOnBinding { pvb_attributes } =
List.find_opt ~f:hasAttr pvb_attributes <> None
let rec unwrap_children children = function
| { pexp_desc = Pexp_construct ({ txt = Lident "[]"; _ }, None); _ } ->
List.rev children
| {
pexp_desc =
Pexp_construct
( { txt = Lident "::"; _ },
Some { pexp_desc = Pexp_tuple [ child; next ]; _ } );
_;
} ->
unwrap_children (child :: children) next
| e -> raise_errorf ~loc:e.pexp_loc "jsx: children prop should be a list"
let is_jsx = function
| { attr_name = { txt = "JSX"; _ }; _ } -> true
| _ -> false
let has_jsx_attr attrs = List.exists ~f:is_jsx attrs
let rewrite_component ~loc tag args children =
let component = pexp_ident ~loc tag in
let props =
match children with
| None -> args
| Some [ children ] -> (Labelled "children", children) :: args
| Some children ->
(Labelled "children", [%expr React.list [%e pexp_list ~loc children]])
:: args
in
[%expr
React.Upper_case_component (fun () -> [%e pexp_apply ~loc component props])]
let validate_prop ~loc id name =
match DomProps.findByName id name with
| Ok p -> p
| Error `ElementNotFound ->
raise_errorf ~loc
"jsx: HTML tag '%s' doesn't exist.\n\
If this isn't correct, please open an issue at %s" id issues_url
| Error `AttributeNotFound -> (
match DomProps.find_closest_name name with
| None ->
raise_errorf ~loc
"jsx: prop '%s' isn't valid on a '%s' element.\n\
If this isn't correct, please open an issue at %s." name id
issues_url
| Some suggestion ->
raise_errorf ~loc
"jsx: prop '%s' isn't valid on a '%s' element.\n\
Hint: Maybe you mean '%s'?\n\n\
If this isn't correct, please open an issue at %s." name id
suggestion issues_url)
let make_prop ~is_optional ~prop attribute_name attribute_value =
let loc = attribute_value.pexp_loc in
let open DomProps in
match (prop, is_optional) with
| Attribute { type_ = DomProps.String; _ }, false ->
[%expr
Some
(React.JSX.String
([%e attribute_name], ([%e attribute_value] : string)))]
| Attribute { type_ = DomProps.String; _ }, true ->
[%expr
match ([%e attribute_value] : string option) with
| None -> None
| Some v -> Some (React.JSX.String ([%e attribute_name], v))]
| Attribute { type_ = DomProps.Int; _ }, false ->
[%expr
Some
(React.JSX.String
([%e attribute_name], string_of_int ([%e attribute_value] : int)))]
| Attribute { type_ = DomProps.Int; _ }, true ->
[%expr
match ([%e attribute_value] : int option) with
| None -> None
| Some v ->
Some (React.JSX.String ([%e attribute_name], string_of_int v))]
| Attribute { type_ = DomProps.Bool; _ }, false ->
[%expr
Some
(React.JSX.Bool ([%e attribute_name], ([%e attribute_value] : bool)))]
| Attribute { type_ = DomProps.Bool; _ }, true ->
[%expr
match ([%e attribute_value] : bool option) with
| None -> None
| Some v -> Some (React.JSX.Bool ([%e attribute_name], v))]
| Attribute { type_ = DomProps.BooleanishString; _ }, false ->
[%expr
Some
(React.JSX.String
([%e attribute_name], string_of_bool ([%e attribute_value] : bool)))]
| Attribute { type_ = DomProps.BooleanishString; _ }, true ->
[%expr
match ([%e attribute_value] : bool option) with
| None -> None
| Some v ->
Some (React.JSX.String ([%e attribute_name], string_of_bool v))]
| Attribute { type_ = DomProps.Style; _ }, false ->
[%expr
Some
(React.JSX.Style
(ReactDOM.Style.to_string
([%e attribute_value] : ReactDOM.Style.t)))]
| Attribute { type_ = DomProps.Style; _ }, true ->
[%expr
match ([%e attribute_value] : ReactDOM.Style.t option) with
| None -> None
| Some v -> Some (React.JSX.Style (ReactDOM.Style.to_string v))]
| Attribute { type_ = DomProps.Ref; _ }, false ->
[%expr Some (React.JSX.Ref ([%e attribute_value] : React.domRef))]
| Attribute { type_ = DomProps.Ref; _ }, true ->
[%expr
match ([%e attribute_value] : React.domRef option) with
| None -> None
| Some v -> Some (React.JSX.Ref v)]
| Attribute { type_ = DomProps.InnerHtml; _ }, false ->
[%expr Some (React.JSX.dangerouslyInnerHtml [%e attribute_value])]
| Attribute { type_ = DomProps.InnerHtml; _ }, true ->
[%expr
match [%e attribute_value] with
| None -> None
| Some v -> Some (React.JSX.dangerouslyInnerHtml v)]
| Event { type_ = Mouse; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Mouse
([%e attribute_value] : React.Event.Mouse.t -> unit) ))]
| Event { type_ = Mouse; jsxName }, true ->
[%expr
match ([%e attribute_value] : (React.Event.Mouse.t -> unit) option) with
| None -> None
| Some v ->
Some
(React.JSX.Event ([%e make_string ~loc jsxName], React.JSX.Mouse v))]
| Event { type_ = Selection; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Selection
([%e attribute_value] : React.Event.Mouse.t -> unit) ))]
| Event { type_ = Selection; jsxName }, true ->
[%expr
match
([%e attribute_value] : (React.Event.Selection.t -> unit) option)
with
| None -> None
| Some v ->
Some
(React.JSX.Event
([%e make_string ~loc jsxName], React.JSX.Selection v))]
| Event { type_ = Touch; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Touch
([%e attribute_value] : React.Event.Touch.t -> unit) ))]
| Event { type_ = Touch; jsxName }, true ->
[%expr
match ([%e attribute_value] : (React.Event.Touch.t -> unit) option) with
| None -> None
| Some v ->
Some
(React.JSX.Event ([%e make_string ~loc jsxName], React.JSX.Touch v))]
| Event { type_ = UI; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.UI ([%e attribute_value] : React.Event.UI.t -> unit) ))]
| Event { type_ = UI; jsxName }, true ->
[%expr
match ([%e attribute_value] : (React.Event.UI.t -> unit) option) with
| None -> None
| Some v ->
Some
(React.JSX.Event ([%e make_string ~loc jsxName], React.JSX.UI v))]
| Event { type_ = Wheel; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Wheel
([%e attribute_value] : React.Event.Wheel.t -> unit) ))]
| Event { type_ = Wheel; jsxName }, true ->
[%expr
match ([%e attribute_value] : (React.Event.Wheel.t -> unit) option) with
| None -> None
| Some v ->
Some
(React.JSX.Event ([%e make_string ~loc jsxName], React.JSX.Wheel v))]
| Event { type_ = Clipboard; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Clipboard
([%e attribute_value] : React.Event.Clipboard.t -> unit) ))]
| Event { type_ = Clipboard; jsxName }, true ->
[%expr
match
([%e attribute_value] : (React.Event.Clipboard.t -> unit) option)
with
| None -> None
| Some v ->
Some
(React.JSX.Event
([%e make_string ~loc jsxName], React.JSX.Clipboard v))]
| Event { type_ = Composition; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Composition
([%e attribute_value] : React.Event.Composition.t -> unit) ))]
| Event { type_ = Composition; jsxName }, true ->
[%expr
match
([%e attribute_value] : (React.Event.Composition.t -> unit) option)
with
| None -> None
| Some v ->
Some
(React.JSX.Event
([%e make_string ~loc jsxName], React.JSX.Composition v))]
| Event { type_ = Keyboard; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Keyboard
([%e attribute_value] : React.Event.Keyboard.t -> unit) ))]
| Event { type_ = Keyboard; jsxName }, true ->
[%expr
match
([%e attribute_value] : (React.Event.Keyboard.t -> unit) option)
with
| None -> None
| Some v ->
Some
(React.JSX.Event
([%e make_string ~loc jsxName], React.JSX.Keyboard v))]
| Event { type_ = Focus; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Focus
([%e attribute_value] : React.Event.Focus.t -> unit) ))]
| Event { type_ = Focus; jsxName }, true ->
[%expr
match ([%e attribute_value] : (React.Event.Focus.t -> unit) option) with
| None -> None
| Some v ->
Some
(React.JSX.Event ([%e make_string ~loc jsxName], React.JSX.Focus v))]
| Event { type_ = Form; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Form
([%e attribute_value] : React.Event.Form.t -> unit) ))]
| Event { type_ = Form; jsxName }, true ->
[%expr
match ([%e attribute_value] : (React.Event.Form.t -> unit) option) with
| None -> None
| Some v ->
Some
(React.JSX.Event ([%e make_string ~loc jsxName], React.JSX.Form v))]
| Event { type_ = Media; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Media
([%e attribute_value] : React.Event.Media.t -> unit) ))]
| Event { type_ = Media; jsxName }, true ->
[%expr
match ([%e attribute_value] : (React.Event.Media.t -> unit) option) with
| None -> None
| Some v ->
Some
(React.JSX.Event ([%e make_string ~loc jsxName], React.JSX.Media v))]
| Event { type_ = Inline; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Inline ([%e attribute_value] : string) ))]
| Event { type_ = Inline; jsxName }, true ->
[%expr
match ([%e attribute_value] : string option) with
| None -> None
| Some v ->
Some
(React.JSX.Event
([%e make_string ~loc jsxName], React.JSX.Inline v))]
| Event { type_ = Image; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Image
([%e attribute_value] : (React.Event.Image.t -> unit) option)
))]
| Event { type_ = Image; jsxName }, true ->
[%expr
match ([%e attribute_value] : (React.Event.Image.t -> unit) option) with
| None -> None
| Some v ->
Some
(React.JSX.Event ([%e make_string ~loc jsxName], React.JSX.Image v))]
| Event { type_ = Animation; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Animation
([%e attribute_value] : React.Event.Animation.t -> unit) ))]
| Event { type_ = Animation; jsxName }, true ->
[%expr
match
([%e attribute_value] : (React.Event.Animation.t -> unit) option)
with
| None -> None
| Some v ->
Some
(React.JSX.Event
([%e make_string ~loc jsxName], React.JSX.Animation v))]
| Event { type_ = Transition; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Transition
([%e attribute_value] : React.Event.Transition.t -> unit) ))]
| Event { type_ = Transition; jsxName }, true ->
[%expr
match
([%e attribute_value] : (React.Event.Transition.t -> unit) option)
with
| None -> None
| Some v ->
Some
(React.JSX.Event
([%e make_string ~loc jsxName], React.JSX.Transition v))]
| Event { type_ = Pointer; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Pointer
([%e attribute_value] : React.Event.Pointer.t -> unit) ))]
| Event { type_ = Pointer; jsxName }, true ->
[%expr
match
([%e attribute_value] : (React.Event.Pointer.t -> unit) option)
with
| None -> None
| Some v ->
Some
(React.JSX.Event
([%e make_string ~loc jsxName], React.JSX.Pointer v))]
| Event { type_ = Drag; jsxName }, false ->
[%expr
Some
(React.JSX.Event
( [%e make_string ~loc jsxName],
React.JSX.Drag
([%e attribute_value] : React.Event.Drag.t -> unit) ))]
| Event { type_ = Drag; jsxName }, true ->
[%expr
match ([%e attribute_value] : (React.Event.Drag.t -> unit) option) with
| None -> None
| Some v ->
Some
(React.JSX.Event ([%e make_string ~loc jsxName], React.JSX.Drag v))]
let is_optional = function Optional _ -> true | _ -> false
let transform_labelled ~loc ~tag_name (prop_label, (runtime_value : expression))
props =
match prop_label with
| Nolabel -> props
| Optional name | Labelled name ->
let is_optional = is_optional prop_label in
let prop = validate_prop ~loc tag_name name in
let name = estring ~loc (DomProps.getName prop) in
let new_prop = make_prop ~is_optional ~prop name runtime_value in
[%expr [%e new_prop] :: [%e props]]
let transform_lowercase_props ~loc ~tag_name args =
match args with
| [] -> [%expr []]
| attrs -> (
let list_of_attributes =
attrs
|> List.fold_right
~f:(transform_labelled ~loc ~tag_name)
~init:[%expr []]
in
match list_of_attributes with
| [%expr []] -> [%expr []]
| _ ->
[%expr Stdlib.List.filter_map Fun.id [%e list_of_attributes]])
let rewrite_lowercase ~loc:exprLoc tag_name args children =
let loc = exprLoc in
let dom_node_name = estring ~loc:exprLoc tag_name in
let props = transform_lowercase_props ~loc:exprLoc ~tag_name args in
match children with
| Some children ->
let childrens = pexp_list ~loc children in
[%expr React.createElement [%e dom_node_name] [%e props] [%e childrens]]
| None -> [%expr React.createElement [%e dom_node_name] [%e props] []]
let split_args args =
let children = ref (Location.none, []) in
let rest =
List.filter_map args ~f:(function
| Labelled "children", children_expression ->
let children' = unwrap_children [] children_expression in
children := (children_expression.pexp_loc, children');
None
| arg_label, e -> Some (arg_label, e))
in
let children_prop =
match !children with _loc, [] -> None | _loc, children -> Some children
in
(children_prop, rest)
let reverse_pexp_list ~loc expr =
let rec go acc = function
| [%expr []] -> acc
| [%expr [%e? hd] :: [%e? tl]] -> go [%expr [%e hd] :: [%e acc]] tl
| expr -> expr
in
go [%expr []] expr
let list_have_tail expr =
match expr with
| Pexp_construct
({ txt = Lident "::"; _ }, Some { pexp_desc = Pexp_tuple _; _ })
| Pexp_construct ({ txt = Lident "[]"; _ }, None) ->
false
| _ -> true
let transform_items_of_list ~loc children =
let rec run_mapper children accum =
match children with
| [%expr []] -> reverse_pexp_list ~loc accum
| [%expr [%e? v] :: [%e? acc]] when list_have_tail acc.pexp_desc ->
[%expr [%e v]]
| [%expr [%e? v] :: [%e? acc]] ->
run_mapper acc [%expr [%e v] :: [%e accum]]
| notAList -> notAList
in
run_mapper children [%expr []]
let remove_warning_16_optional_argument_cannot_be_erased ~loc =
let open Ast_helper in
{
attr_name = { txt = "warning"; loc };
attr_payload =
PStr [ Str.eval (Ast_helper.Exp.constant (Const.string "-16")) ];
attr_loc = loc;
}
let remove_warning_27_unused_var_strict ~loc =
let open Ast_helper in
{
attr_name = { txt = "warning"; loc };
attr_payload =
PStr [ Str.eval (Ast_helper.Exp.constant (Const.string "-27")) ];
attr_loc = loc;
}
let get_function_name binding =
match binding with
| { pvb_pat = { ppat_desc = Ppat_var { txt } } } -> txt
| _ ->
raise_errorf ~loc:binding.pvb_loc
"react.component calls cannot be destructured."
let rec transform_function_with_warning expression =
let loc = expression.pexp_loc in
match expression.pexp_desc with
| Pexp_fun
( ((Labelled _ | Optional _) as label),
default,
pattern,
({ pexp_desc = Pexp_fun _ } as internalExpression) ) ->
let exp = transform_function_with_warning internalExpression in
{ expression with pexp_desc = Pexp_fun (label, default, pattern, exp) }
| Pexp_fun
( Nolabel,
_default,
{ ppat_desc = Ppat_construct ({ txt = Lident "()" }, _) | Ppat_any },
_internalExpression ) ->
expression
| Pexp_fun (label, default, pattern, internalExpression) ->
{
expression with
pexp_attributes =
remove_warning_16_optional_argument_cannot_be_erased ~loc
:: expression.pexp_attributes;
pexp_desc =
Pexp_fun
( label,
default,
pattern,
{
pexp_loc = expression.pexp_loc;
pexp_desc =
Pexp_fun (Nolabel, None, [%pat? ()], internalExpression);
pexp_loc_stack = [];
pexp_attributes = [];
} );
}
| Pexp_let (recursive, vbs, internalExpression) ->
let exp = transform_function_with_warning internalExpression in
{ expression with pexp_desc = Pexp_let (recursive, vbs, exp) }
| Pexp_apply (_wrapperExpression, [ (Nolabel, internalExpression) ]) ->
transform_function_with_warning internalExpression
| Pexp_apply
( _wrapperExpression,
[
(Nolabel, internalExpression);
((Nolabel, { pexp_desc = Pexp_fun _ }) as _compareProps);
] ) ->
transform_function_with_warning internalExpression
| Pexp_sequence (wrapperExpression, internalExpression) ->
let exp = transform_function_with_warning internalExpression in
{ expression with pexp_desc = Pexp_sequence (wrapperExpression, exp) }
| _ -> expression
let make_value_binding binding =
let loc = binding.pvb_loc in
let ghost_loc = { binding.pvb_loc with loc_ghost = true } in
let binding_expr = transform_function_with_warning binding.pvb_expr in
let name =
Ast_helper.Pat.mk ~loc:ghost_loc
(Ppat_var { txt = get_function_name binding; loc = ghost_loc })
in
let key_arg = Optional "key" in
let default_value = None in
let key_renamed_to_underscore = ppat_var ~loc:ghost_loc { txt = "_"; loc } in
let core_type = [%type: string option] in
let key_pattern = ppat_constraint ~loc key_renamed_to_underscore core_type in
let body_expression =
pexp_fun ~loc:ghost_loc key_arg default_value key_pattern binding_expr
in
Ast_helper.Vb.mk ~loc name body_expression
let rewrite_signature_item signature_item =
match signature_item with
| {
psig_loc = _;
psig_desc =
Psig_value
({ pval_name = { txt = _fnName }; pval_attributes; pval_type } as
psig_desc);
} as psig -> (
match List.filter ~f:hasAttr pval_attributes with
| [] -> signature_item
| [ _ ] ->
{
psig with
psig_desc =
Psig_value
{
psig_desc with
pval_type;
pval_attributes =
List.filter ~f:otherAttrsPure pval_attributes;
};
}
| _ ->
let loc = signature_item.psig_loc in
[%sigi:
[%%ocaml.error
"externals aren't supported on server-reason-react. externals are \
used to bind to React components defined in JavaScript, in the \
server, that doesn't make sense. If you need to render this on \
the server, implement a placeholder or an empty element"]])
| _signature_item -> signature_item
let rewrite_structure_item structure_item =
match structure_item.pstr_desc with
| Pstr_primitive
({ pval_name = { txt = _fnName }; pval_attributes; pval_type = _ } as
_value_description) -> (
match List.filter ~f:hasAttr pval_attributes with
| [] -> structure_item
| _ ->
let loc = structure_item.pstr_loc in
[%stri
[%%ocaml.error
"externals aren't supported on server-reason-react. externals are \
used to bind to React components defined in JavaScript, in the \
server, that doesn't make sense. If you need to render this on \
the server, implement a placeholder or an empty element"]])
| Pstr_value (rec_flag, value_bindings) ->
let map_value_binding vb =
if not (hasAttrOnBinding vb) then vb else make_value_binding vb
in
let bindings = List.map ~f:map_value_binding value_bindings in
pstr_value ~loc:structure_item.pstr_loc rec_flag bindings
| _ -> structure_item
let rewrite_jsx =
object (_ : Ast_traverse.map)
inherit Ast_traverse.map as super
method! signature_item signature_item =
rewrite_signature_item (super#signature_item signature_item)
method! structure_item structure_item =
rewrite_structure_item (super#structure_item structure_item)
method! expression expr =
let expr = super#expression expr in
try
match expr.pexp_desc with
| Pexp_apply (({ pexp_desc = Pexp_ident _; _ } as tag), args)
when has_jsx_attr expr.pexp_attributes -> (
let children, rest_of_args = split_args args in
match tag.pexp_desc with
| Pexp_ident { txt = Lident name; loc = _name_loc } ->
rewrite_lowercase ~loc:expr.pexp_loc name rest_of_args children
| Pexp_ident
{ txt = Ldot (modulePath, ("createElement" | "make")); loc } ->
let id = { loc; txt = Ldot (modulePath, "make") } in
rewrite_component ~loc:expr.pexp_loc id rest_of_args children
| Pexp_ident id ->
rewrite_component ~loc:expr.pexp_loc id rest_of_args children
| _ -> assert false)
| Pexp_apply (tag, _props) when has_jsx_attr expr.pexp_attributes ->
raise_errorf ~loc:expr.pexp_loc
"jsx: %s should be an identifier, not an expression"
(Ppxlib_ast.Pprintast.string_of_expression tag)
| Pexp_construct
({ txt = Lident "::"; loc }, Some { pexp_desc = Pexp_tuple _; _ })
| Pexp_construct ({ txt = Lident "[]"; loc }, None) -> (
let jsx_attr, rest_attributes =
List.partition ~f:is_jsx expr.pexp_attributes
in
match (jsx_attr, rest_attributes) with
| [], _ -> expr
| _, rest_attributes ->
let children = transform_items_of_list ~loc expr in
let new_expr =
[%expr React.fragment (React.list [%e children])]
in
{ new_expr with pexp_attributes = rest_attributes })
| _ -> expr
with Error err -> [%expr [%e err]]
end
let () =
Ppxlib.Driver.register_transformation "server-reason-react.ppx"
~impl:rewrite_jsx#structure