Skip to content

Commit 58f1bd2

Browse files
authored
Represent JSX component annotations with a parsetree node (#8884)
1 parent 9a1c88b commit 58f1bd2

29 files changed

Lines changed: 612 additions & 100 deletions

‎analysis/src/dump_ast.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ let print_value_binding value ~pos ~indentation =
366366
^ ". "
367367
^ print_core_type pvc_type ~pos
368368
in
369-
print_attributes value.Parsetree.pvb_attributes
369+
print_attributes (Ast_helper.Vb.binding_attributes value)
370370
^ "value" ^ ":\n"
371371
^ add_indentation (indentation + 1)
372372
^ (value.pvb_pat |> print_pattern ~pos ~indentation:(indentation + 1))

‎analysis/src/utils.ml‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ let rec unwrap_if_option (t : Types.type_expr) =
162162
| _ -> t
163163

164164
let is_jsx_component (vb : Parsetree.value_binding) =
165-
vb.pvb_attributes
166-
|> List.exists (function
167-
| {Location.txt = "react.component" | "jsx.component"}, _payload -> true
168-
| _ -> false)
165+
match vb.pvb_jsx_component with
166+
| Some {pjc_kind = Pjc_labelled _} -> true
167+
| Some {pjc_kind = Pjc_with_props} | None -> false
169168

170169
let check_name name ~prefix ~exact =
171170
if exact then name = prefix else starts_with name prefix
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Represent `@react.component`, `@jsx.component` and their `componentWithProps` forms with a dedicated parsetree node. https://github.com/rescript-lang/rescript/pull/8884
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Report a JSX component annotation with an unsupported payload, such as `@react.component("x")`, or a second one on the same definition, instead of ignoring it. https://github.com/rescript-lang/rescript/pull/8884

‎compiler/frontend/ast_external_mk.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let local_external_apply loc ?(pval_attributes = [])
3636
pval_type;
3737
pval_loc = loc;
3838
pval_prim = Some pval_prim;
39+
pval_jsx_component = None;
3940
pval_attributes;
4041
};
4142
],

‎compiler/frontend/ast_tuple_pattern_flatten.ml‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,18 @@ let flattern_tuple_pattern_vb (self : Ast_mapper.mapper)
6060
in
6161
let pvb_expr = self.expr self vb.pvb_expr in
6262
let pvb_attributes = self.attributes self vb.pvb_attributes in
63+
let pvb_jsx_component = vb.pvb_jsx_component in
6364
let pvb_expr_desc = (Ast_payload.unwrap_braces pvb_expr).pexp_desc in
6465
match (pvb_constraint, pvb_pat.ppat_desc, pvb_expr_desc) with
6566
| Some _, _, _ ->
66-
{pvb_pat; pvb_expr; pvb_constraint; pvb_loc = vb.pvb_loc; pvb_attributes}
67+
{
68+
pvb_pat;
69+
pvb_expr;
70+
pvb_constraint;
71+
pvb_jsx_component;
72+
pvb_loc = vb.pvb_loc;
73+
pvb_attributes;
74+
}
6775
:: acc
6876
| None, Ppat_tuple xs, _ when List.for_all is_simple_pattern xs -> (
6977
match Ast_open_cxt.destruct_open_tuple pvb_expr [] with
@@ -77,12 +85,20 @@ let flattern_tuple_pattern_vb (self : Ast_mapper.mapper)
7785
pvb_pat = pat;
7886
pvb_expr = Ast_open_cxt.restore_exp exp wholes;
7987
pvb_constraint = None;
88+
pvb_jsx_component;
8089
pvb_attributes;
8190
pvb_loc = vb.pvb_loc;
8291
}
8392
:: acc)
8493
| _ ->
85-
{pvb_pat; pvb_expr; pvb_constraint; pvb_loc = vb.pvb_loc; pvb_attributes}
94+
{
95+
pvb_pat;
96+
pvb_expr;
97+
pvb_constraint;
98+
pvb_jsx_component;
99+
pvb_loc = vb.pvb_loc;
100+
pvb_attributes;
101+
}
86102
:: acc)
87103
| None, Ppat_record (_, _, Some rest), Pexp_pack {pmod_desc = Pmod_ident _} ->
88104
Location.raise_errorf ~loc:rest.rest_loc
@@ -99,14 +115,22 @@ let flattern_tuple_pattern_vb (self : Ast_mapper.mapper)
99115
Ast_helper.Exp.ident ~loc:lid.loc
100116
{lid with txt = Ldot (id.txt, s)};
101117
pvb_constraint = None;
118+
pvb_jsx_component = None;
102119
pvb_attributes = [];
103120
pvb_loc = pat.ppat_loc;
104121
}
105122
| _ ->
106123
Location.raise_errorf ~loc:lid.loc
107124
"Not supported pattern match on modules")
108125
| _ ->
109-
{pvb_pat; pvb_expr; pvb_constraint; pvb_loc = vb.pvb_loc; pvb_attributes}
126+
{
127+
pvb_pat;
128+
pvb_expr;
129+
pvb_constraint;
130+
pvb_jsx_component;
131+
pvb_loc = vb.pvb_loc;
132+
pvb_attributes;
133+
}
110134
:: acc
111135

112136
let value_bindings_mapper (self : Ast_mapper.mapper)

‎compiler/frontend/bs_builtin_ppx.ml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) :
495495
{
496496
value_desc with
497497
pval_prim = Some (Ast_external_mk.inline_string semantic);
498+
pval_jsx_component = None;
498499
pval_attributes = [];
499500
};
500501
}
@@ -508,6 +509,7 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) :
508509
{
509510
value_desc with
510511
pval_prim = Some (Ast_external_mk.inline_int s);
512+
pval_jsx_component = None;
511513
pval_attributes = [];
512514
};
513515
}
@@ -520,6 +522,7 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) :
520522
{
521523
value_desc with
522524
pval_prim = Some (Ast_external_mk.inline_bigint s);
525+
pval_jsx_component = None;
523526
pval_attributes = [];
524527
};
525528
}
@@ -532,6 +535,7 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) :
532535
{
533536
value_desc with
534537
pval_prim = Some (Ast_external_mk.inline_float s);
538+
pval_jsx_component = None;
535539
pval_attributes = [];
536540
};
537541
}
@@ -545,6 +549,7 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) :
545549
{
546550
value_desc with
547551
pval_prim = Some (Ast_external_mk.inline_bool (txt = "true"));
552+
pval_jsx_component = None;
548553
pval_attributes = [];
549554
};
550555
}
@@ -588,6 +593,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
588593
pvb_pat = {ppat_desc = Ppat_var pval_name} as pvb_pat;
589594
pvb_expr;
590595
pvb_constraint = None;
596+
pvb_jsx_component;
591597
pvb_attributes;
592598
pvb_loc;
593599
};
@@ -617,6 +623,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
617623
pval_name;
618624
pval_type = Ast_literal.type_string ();
619625
pval_loc = pvb_loc;
626+
pval_jsx_component = None;
620627
pval_attributes = [];
621628
pval_prim = Some (Ast_external_mk.inline_string semantic);
622629
};
@@ -632,6 +639,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
632639
pval_name;
633640
pval_type = Ast_literal.type_int ();
634641
pval_loc = pvb_loc;
642+
pval_jsx_component = None;
635643
pval_attributes = [];
636644
pval_prim = Some (Ast_external_mk.inline_int s);
637645
};
@@ -646,6 +654,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
646654
pval_name;
647655
pval_type = Ast_literal.type_float;
648656
pval_loc = pvb_loc;
657+
pval_jsx_component = None;
649658
pval_attributes = [];
650659
pval_prim = Some (Ast_external_mk.inline_float s);
651660
};
@@ -662,6 +671,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
662671
pval_name;
663672
pval_type = Ast_literal.type_bool ();
664673
pval_loc = pvb_loc;
674+
pval_jsx_component = None;
665675
pval_attributes = [];
666676
pval_prim = Some (Ast_external_mk.inline_bool (txt = "true"));
667677
};
@@ -677,6 +687,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
677687
pvb_pat;
678688
pvb_expr;
679689
pvb_constraint = None;
690+
pvb_jsx_component;
680691
pvb_attributes;
681692
pvb_loc;
682693
};

‎compiler/ml/ast_helper.ml‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,96 @@ module Str = struct
325325
let attribute ?loc a = mk ?loc (Pstr_attribute a)
326326
end
327327

328+
(* [@react.component] and its variants are taken out of the attributes when a
329+
value binding or value description is built, so every producer - the
330+
parser, a ppx, the frozen AST bridge - agrees on what is a component. *)
331+
module Jsx_component = struct
332+
let of_attribute (({txt; loc}, payload) : attribute) =
333+
let component pjc_kind pjc_namespace =
334+
Some {pjc_kind; pjc_namespace; pjc_loc = loc}
335+
in
336+
match (txt, payload) with
337+
| "react.component", PStr [] -> component (Pjc_labelled None) Pjc_react
338+
| "react.component", PTyp typ ->
339+
component (Pjc_labelled (Some typ)) Pjc_react
340+
| "jsx.component", PStr [] -> component (Pjc_labelled None) Pjc_jsx
341+
| "jsx.component", PTyp typ -> component (Pjc_labelled (Some typ)) Pjc_jsx
342+
| "react.componentWithProps", PStr [] -> component Pjc_with_props Pjc_react
343+
| "jsx.componentWithProps", PStr [] -> component Pjc_with_props Pjc_jsx
344+
| _ -> None
345+
346+
let to_attribute {pjc_kind; pjc_namespace; pjc_loc} : attribute =
347+
let namespace =
348+
match pjc_namespace with
349+
| Pjc_react -> "react"
350+
| Pjc_jsx -> "jsx"
351+
in
352+
match pjc_kind with
353+
| Pjc_labelled None ->
354+
({txt = namespace ^ ".component"; loc = pjc_loc}, PStr [])
355+
| Pjc_labelled (Some typ) ->
356+
({txt = namespace ^ ".component"; loc = pjc_loc}, PTyp typ)
357+
| Pjc_with_props ->
358+
({txt = namespace ^ ".componentWithProps"; loc = pjc_loc}, PStr [])
359+
360+
(* The first [@react.component], or failing that the first
361+
[@react.componentWithProps]: the JSX transform has always given them
362+
this precedence. A later one, or one whose payload is not understood, is
363+
left in the attributes for the JSX transform to reject, so that
364+
formatting does not drop it. *)
365+
let take attrs =
366+
let take_first is_kind =
367+
let rec go seen = function
368+
| [] -> (None, attrs)
369+
| attr :: rest -> (
370+
match of_attribute attr with
371+
| Some component when is_kind component.pjc_kind ->
372+
(Some component, List.rev_append seen rest)
373+
| _ -> go (attr :: seen) rest)
374+
in
375+
go [] attrs
376+
in
377+
match
378+
take_first (function
379+
| Pjc_labelled _ -> true
380+
| Pjc_with_props -> false)
381+
with
382+
| (Some _, _) as found -> found
383+
| None, _ ->
384+
take_first (function
385+
| Pjc_with_props -> true
386+
| Pjc_labelled _ -> false)
387+
388+
(* Inverse of [take], for printers and the frozen AST bridge. Attributes
389+
print in source order, so the annotation goes back where it was written.
390+
Ppx output has no source position, so all of it ties on [Location.none];
391+
the annotation then goes first among the tied, where it is usually
392+
written. *)
393+
let put_back component attrs =
394+
match component with
395+
| None -> attrs
396+
| Some component ->
397+
let written_earlier (({loc}, _) : attribute) =
398+
loc.loc_start.pos_cnum < component.pjc_loc.loc_start.pos_cnum
399+
in
400+
let earlier, later = List.partition written_earlier attrs in
401+
earlier @ (to_attribute component :: later)
402+
end
403+
328404
module Val = struct
329405
let mk ?(loc = !default_loc) ?(attrs = []) ?prim name typ =
406+
let jsx_component, attrs = Jsx_component.take attrs in
330407
{
331408
pval_name = name;
332409
pval_type = typ;
410+
pval_jsx_component = jsx_component;
333411
pval_attributes = attrs;
334412
pval_loc = loc;
335413
pval_prim = prim;
336414
}
415+
416+
let description_attributes vd =
417+
Jsx_component.put_back vd.pval_jsx_component vd.pval_attributes
337418
end
338419

339420
module Md = struct
@@ -368,13 +449,18 @@ end
368449

369450
module Vb = struct
370451
let mk ?(loc = !default_loc) ?(attrs = []) ?constraint_ pat expr =
452+
let jsx_component, attrs = Jsx_component.take attrs in
371453
{
372454
pvb_pat = pat;
373455
pvb_expr = expr;
374456
pvb_constraint = constraint_;
457+
pvb_jsx_component = jsx_component;
375458
pvb_attributes = attrs;
376459
pvb_loc = loc;
377460
}
461+
462+
let binding_attributes vb =
463+
Jsx_component.put_back vb.pvb_jsx_component vb.pvb_attributes
378464
end
379465

380466
module Type = struct

‎compiler/ml/ast_helper.mli‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,15 @@ module Exp : sig
283283
Location.t -> expression list -> expression option -> expression
284284
end
285285

286+
(** JSX component annotations *)
287+
module Jsx_component : sig
288+
val of_attribute : attribute -> jsx_component option
289+
(** The component an attribute such as [@react.component] denotes, if its
290+
payload is one the JSX transform understands. *)
291+
292+
val to_attribute : jsx_component -> attribute
293+
end
294+
286295
(** Value declarations *)
287296
module Val : sig
288297
val mk :
@@ -292,6 +301,12 @@ module Val : sig
292301
str ->
293302
core_type ->
294303
value_description
304+
(** A [@react.component] annotation in [attrs] is taken out and becomes
305+
[pval_jsx_component]. *)
306+
307+
val description_attributes : value_description -> attrs
308+
(** The attributes as written, with the [@react.component] annotation put
309+
back. For printers, which must reproduce the source. *)
295310
end
296311

297312
(** Type declarations *)
@@ -495,4 +510,10 @@ module Vb : sig
495510
pattern ->
496511
expression ->
497512
value_binding
513+
(** A [@react.component] annotation in [attrs] is taken out and becomes
514+
[pvb_jsx_component]. *)
515+
516+
val binding_attributes : value_binding -> attrs
517+
(** The attributes as written, with the [@react.component] annotation put
518+
back. For printers, which must reproduce the source. *)
498519
end

‎compiler/ml/ast_iterator.ml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,12 @@ let default_iterator =
520520
type_extension = T.iter_type_extension;
521521
extension_constructor = T.iter_extension_constructor;
522522
value_description =
523-
(fun this
524-
{pval_name; pval_type; pval_prim = _; pval_loc; pval_attributes}
525-
->
523+
(fun this ({pval_name; pval_type; pval_prim = _; pval_loc} as vd) ->
526524
iter_loc this pval_name;
527525
this.typ this pval_type;
528-
this.attributes this pval_attributes;
526+
(* An iterator sees the component annotation as the attribute it was
527+
written as. *)
528+
this.attributes this (Ast_helper.Val.description_attributes vd);
529529
this.location this pval_loc);
530530
pat = P.iter;
531531
expr = E.iter;
@@ -563,7 +563,7 @@ let default_iterator =
563563
this.location this pincl_loc;
564564
this.attributes this pincl_attributes);
565565
value_binding =
566-
(fun this {pvb_pat; pvb_expr; pvb_constraint; pvb_attributes; pvb_loc} ->
566+
(fun this ({pvb_pat; pvb_expr; pvb_constraint; pvb_loc} as vb) ->
567567
this.pat this pvb_pat;
568568
Option.iter
569569
(fun {pvc_newtypes; pvc_type} ->
@@ -572,7 +572,7 @@ let default_iterator =
572572
pvb_constraint;
573573
this.expr this pvb_expr;
574574
this.location this pvb_loc;
575-
this.attributes this pvb_attributes);
575+
this.attributes this (Ast_helper.Vb.binding_attributes vb));
576576
constructor_declaration =
577577
(fun this {pcd_name; pcd_args; pcd_res; pcd_loc; pcd_attributes} ->
578578
iter_loc this pcd_name;

0 commit comments

Comments
 (0)