Source file jsx_helper.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
open Printf
open Asttypes
open Longident
open Parsetree
open Ast_helper
let make_loc (startpos, endpos) =
{
Location.loc_start = startpos;
Location.loc_end = endpos;
Location.loc_ghost = false;
}
let mkloc = Location.mkloc
let mkexp ~loc d = Exp.mk ~loc:(make_loc loc) d
let mkjsxexp ~loc:loc' e =
let e = mkexp ~loc:loc' e in
let loc = make_loc loc' in
let pexp_attributes = [ Attr.mk ~loc { txt = "JSX"; loc } (PStr []) ] in
{ e with pexp_attributes }
let rec equal_longindent a b =
match a, b with
| Longident.Lident a, Longident.Lident b -> String.equal a b
| Ldot (pa, a), Ldot (pb, b) ->
String.equal a.txt b.txt && equal_longindent pa.txt pb.txt
| Lapply _, _ | _, Lapply _ -> assert false
| _ -> false
let make_jsx_element ~raise ~loc:_ ~tag ~end_tag ~props ~children () =
let () =
match end_tag with
| None -> ()
| Some (end_tag, (_, end_loc_e)) ->
let eq =
match tag, end_tag with
| (`Module, _, s), (`Module, _, e) -> equal_longindent s e
| (`Value, _, s), (`Value, _, e) -> equal_longindent s e
| _ -> false
in
if not eq then
let _, (end_loc_s, _), _ = end_tag in
let end_loc = end_loc_s, end_loc_e in
let _, start_loc, tag = tag in
let tag = Longident.flatten tag |> String.concat "." in
raise
Syntaxerr.(
Error
(Unclosed
( make_loc start_loc,
sprintf "<%s>" tag,
make_loc end_loc,
sprintf "</%s>" tag )))
in
let tag =
match tag with
| `Value, loc, txt ->
mkexp ~loc (Pexp_ident { loc = make_loc loc; txt })
| `Module, loc, txt ->
let txt = Longident.Ldot (mkloc txt (make_loc loc), mkloc "createElement" (make_loc loc)) in
mkexp ~loc (Pexp_ident { loc = make_loc loc; txt })
in
let props =
let prop_exp ~loc name =
let id = mkloc (Lident name.txt) (make_loc loc) in
mkexp ~loc (Pexp_ident id)
in
List.map
(function
| loc, `Prop_punned name -> Labelled {txt=name.txt;loc = make_loc loc}, prop_exp ~loc name
| loc, `Prop_opt_punned name -> Optional {txt=name.txt;loc = make_loc loc}, prop_exp ~loc name
| _loc, `Prop (name, expr) -> Labelled name, expr
| _loc, `Prop_opt (name, expr) -> Optional name, expr)
props
in
let unit =
Exp.mk ~loc:Location.none
(Pexp_construct ({ txt = Lident "()"; loc = Location.none }, None))
in
let props = (Labelled {txt="children"; loc=children.pexp_loc}, children) :: props in
Pexp_apply (tag, (Nolabel, unit) :: props)
let make_jsx_fragment ~children =
let tag =
Exp.mk ~loc:Location.none (Pexp_ident { loc = Location.none; txt = Lident "" })
in
let unit =
Exp.mk ~loc:Location.none
(Pexp_construct ({ txt = Lident "()"; loc = Location.none }, None))
in
Pexp_apply
(tag, [ Nolabel, unit; Labelled { txt = "children"; loc = children.pexp_loc }, children ])
(** The children of a JSX element: a list of children, or a single spread child (`<Foo> ...expr </Foo>`). *)
type jsx_children = Children of expression list | Spread of expression
(** A [@JSX] application that can be printed with JSX syntax. *)
type element = {
tag : string;
tag_loc : Location.t;
unit_loc : Location.t;
props : (arg_label * expression) list;
children_loc : Location.t;
children : jsx_children;
loc : Location.t;
(** Location of the [@JSX] attribute, which spans the whole element. *)
extra_attrs : attributes;
(** Attributes other than [@JSX] carried by the application, printed
after the element (e.g. [(<Foo />) [@mel.as "and"]]). *)
}
(** Classify a [@JSX] application. JSX syntax can only express applications
of the exact shape produced by [make_jsx_element]: an identifier tag
applied to one unlabelled [()] argument, one [~children] argument that
is a list literal or any other expression (a children spread), and
labelled or optional props. Hand-written [@JSX] applications may have
any other shape (see ocaml-mlx/ocamlformat-mlx#12), in which case
[None] is returned and the application must be printed as a regular
application. *)
let classify_element ~attrs e0 args =
let tag =
let rec ident_of = function
| Lident name -> Some name
| Ldot (id, name) ->
Option.map (fun path -> path ^ "." ^ name.txt) (ident_of id.txt)
| Lapply _ -> None
in
match e0.pexp_desc with
| Pexp_ident { txt = Lident name; loc } -> Some (name, loc)
| Pexp_ident { txt = Ldot (id, name); loc = _ } ->
Option.map
(fun path ->
let tag =
match name.txt with
| "createElement" -> path
| name -> path ^ "." ^ name
in
(tag, e0.pexp_loc))
(ident_of id.txt)
| _ -> None
in
let units, children, props =
List.fold_right
(fun arg (units, children, props) ->
match arg with
| ( Nolabel,
{ pexp_desc = Pexp_construct ({ txt = Lident "()"; _ }, None);
pexp_attributes = [];
pexp_loc;
_ } ) ->
(pexp_loc :: units, children, props)
| ( Labelled { txt = "children"; _ },
{ pexp_desc = Pexp_list es; pexp_attributes = []; pexp_loc; _ } )
->
(units, (pexp_loc, Children es) :: children, props)
| ( Labelled { txt = "children"; _ },
{ pexp_desc = Pexp_construct ({ txt = Lident "[]"; _ }, None);
pexp_attributes = [];
pexp_loc;
_ } ) ->
(units, (pexp_loc, Children []) :: children, props)
| ( Labelled { txt = "children"; _ }, e ) ->
(units, (e.pexp_loc, Spread e) :: children, props)
| arg -> (units, children, arg :: props))
args ([], [], [])
in
let is_prop = function
| Labelled { txt = "children"; _ }, _ ->
false
| (Labelled _ | Optional _), _ -> true
| Nolabel, _ -> false
in
let rec split_jsx_attr acc = function
| [] -> None
| ({ attr_name = { txt = "JSX"; _ }; attr_payload = PStr []; attr_loc } as
_jsx)
:: rest ->
Some (attr_loc, List.rev_append acc rest)
| a :: rest -> split_jsx_attr (a :: acc) rest
in
match (split_jsx_attr [] attrs, tag, units, children) with
| ( Some (attr_loc, ),
Some (tag, tag_loc),
[ unit_loc ],
[ (children_loc, children) ] )
when List.for_all is_prop props ->
Some
{ tag; tag_loc; unit_loc; props; children_loc; children;
loc = attr_loc; extra_attrs }
| _ -> None