Skip to content

Commit e565ca7

Browse files
committed
First step towards ast mapping
1 parent b6407d0 commit e565ca7

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

compiler/ml/ast_mapper_from0.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ module E = struct
315315
let open Exp in
316316
let loc = sub.location sub loc in
317317
let attrs = sub.attributes sub attrs in
318+
let has_jsx_attribute () =
319+
attrs |> List.exists (fun ({txt}, _) -> txt = "JSX")
320+
in
318321
match desc with
319322
| Pexp_ident x -> ident ~loc ~attrs (map_loc sub x)
320323
| Pexp_constant x -> constant ~loc ~attrs (map_constant x)
@@ -327,6 +330,10 @@ module E = struct
327330
(map_opt (sub.expr sub) def)
328331
(sub.pat sub p) (sub.expr sub e)
329332
| Pexp_function _ -> assert false
333+
| Pexp_apply ({pexp_desc = Pexp_ident tag_name}, _args)
334+
when has_jsx_attribute () ->
335+
let attrs = attrs |> List.filter (fun ({txt}, _) -> txt <> "JSX") in
336+
jsx_unary_element ~loc ~attrs tag_name []
330337
| Pexp_apply (e, l) ->
331338
let e =
332339
match (e.pexp_desc, l) with
@@ -376,7 +383,7 @@ module E = struct
376383
| Pexp_tuple el -> tuple ~loc ~attrs (List.map (sub.expr sub) el)
377384
(* <></> *)
378385
| Pexp_construct ({txt = Longident.Lident "[]" | Longident.Lident "::"}, _)
379-
when attrs |> List.exists (fun ({txt}, _) -> txt = "JSX") ->
386+
when has_jsx_attribute () ->
380387
let attrs = attrs |> List.filter (fun ({txt}, _) -> txt <> "JSX") in
381388
(* TODO: support spread *)
382389
jsx_fragment ~loc ~attrs loc.loc_start

compiler/ml/ast_mapper_to0.ml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ module M = struct
281281
end
282282

283283
module E = struct
284+
let jsx_attr sub =
285+
sub.attribute sub (Location.mknoloc "JSX", Parsetree.PStr [])
286+
284287
(* Value expressions for the core language *)
285288

286289
let map sub {pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} =
@@ -420,11 +423,27 @@ module E = struct
420423
in
421424
let list_expr = Ast_helper.Exp.make_list_expression loc xs None in
422425
let mapped = sub.expr sub list_expr in
423-
let jsx_attr =
424-
sub.attribute sub (Location.mknoloc "JSX", Parsetree.PStr [])
426+
427+
{mapped with pexp_attributes = jsx_attr sub :: attrs}
428+
| Pexp_jsx_unary_element
429+
{
430+
jsx_unary_element_tag_name = tag_name;
431+
jsx_unary_element_props = _props;
432+
} ->
433+
let tag_ident = map_loc sub tag_name in
434+
let children_expr =
435+
Ast_helper0.Exp.construct ~loc {txt = Lident "()"; loc} None
436+
in
437+
let unit_expr =
438+
Ast_helper0.Exp.construct ~loc:!Ast_helper0.default_loc
439+
{txt = Lident "()"; loc = !Ast_helper0.default_loc}
440+
None
425441
in
426-
{mapped with pexp_attributes = jsx_attr :: attrs}
427-
| Pexp_jsx_unary_element _ -> failwith "TODO: Pexp_jsx_unary_element 1"
442+
apply ~loc ~attrs:(jsx_attr sub :: attrs) (ident tag_ident)
443+
[
444+
(Asttypes.Noloc.Labelled "children", children_expr);
445+
(Asttypes.Noloc.Nolabel, unit_expr);
446+
]
428447
| Pexp_jsx_container_element _ ->
429448
failwith "TODO: Pexp_jsx_container_element 1"
430449
end

0 commit comments

Comments
 (0)