@@ -325,15 +325,96 @@ module Str = struct
325325 let attribute ?loc a = mk ?loc (Pstr_attribute a)
326326end
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+
328404module 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
337418end
338419
339420module Md = struct
@@ -368,13 +449,18 @@ end
368449
369450module 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
378464end
379465
380466module Type = struct
0 commit comments