Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file brisk_ppx.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313moduleP=Ppxlib.AstmoduleATH=Ppxlib.Ast_helpermoduleAst_builder=Ppxlib.Ast_builder.Defaultletcomponent_ident~loc=Ast_builder.(pexp_ident~loc(Located.lident~loc"brisk-component"))letcomponent_ident_pattern~loc=Ast_builder.(ppat_var~loc(Located.mk~loc"brisk-component"))lethooks_ident~loc=Ast_builder.(pexp_ident~loc(Located.lident~loc"brisk-hooks"))lethooks_ident_pattern~loc=Ast_builder.(ppat_var~loc(Located.mk~loc"brisk-hooks"))moduleJSX_ppx=structletrecprops_filter_children~acc=function|[]->List.revacc|(P.Labelled"children",P.([%expr[]]))::tail->props_filter_children~acctail|(P.Labelled"children",P.([%expr[%e?h]::[%e?t]]asexp))::tail->letloc=exp.P.pexp_locinletprop=(P.Labelled"children",P.([%exprBrisk_reconciler.Expert.jsx_list([%eh]::[%et])]))inprops_filter_children~acc:(prop::acc)tail|prop::tail->props_filter_children~acc:(prop::acc)tailletprops_filter_childrenprops=props_filter_children~acc:[]propsletrewrite_apply~loc~attributes:attrsprops=letargs=props_filter_childrenpropsinATH.Exp.apply~loc~attrs(component_ident~loc)argsletis_jsx=letopenPpxlib.Ast_patterninletjsx_attr=attribute~name:(string"JSX")~payload:__infunattr->parsejsx_attrPpxlib.Location.none~on_error:(fun_->false)attr(fun_->true)letfilter_jsx=List.filteris_jsxletexists_jsx=List.existsis_jsxletrectransform_createElement=letopenLongidentinfunction|Ldot(head,"createElement")->Ldot(head,"make")|Lapply(left,right)->Lapply(left,transform_createElementright)|Lident_asident->ident|Ldot_asldot->ldotletexprexpr=matchexpr.P.pexp_descwith|P.Pexp_apply(fn,args)whenexists_jsxexpr.pexp_attributes->letattributes=filter_jsxexpr.pexp_attributesinletargs=List.map(fun(label,arg)->(label,arg))argsinletloc=expr.P.pexp_locinletfn=matchfn.P.pexp_descwith|P.Pexp_ident{txt;loc}->lettxt=transform_createElementtxtin{fnwithpexp_desc=Pexp_ident{txt;loc}}|_->fninP.([%exprlet[%pcomponent_ident_pattern~loc]=[%efn]in[%erewrite_apply~attributes~loc:expr.P.pexp_locargs]])|_->exprendmoduleDeclaration_ppx=structletfunc_pattern=Ppxlib.Ast_pattern.(alt(pexp_fun________|>map~f:(funflblopt_argpatexpr->f(`Fun(lbl,opt_arg,pat,expr))))(pexp_newtype__'__|>map~f:(funfidentexpr->f(`Newtype(ident,expr)))))letmatch_pattern?on_errorlocast_node~with_=Ppxlib.Ast_pattern.parsepattern?on_errorlocast_nodewith_letattribute_name=function|`Component->"component"|`Native->"nativeComponent"lettransform_component_expr~useDynamicKey~attribute~component_nameexpr=letrecmap_component_expression({P.pexp_loc=loc}asexpr)=match_func_patternlocexpr~with_:(function|`Fun(lbl,opt_arg,pat,child_expression)->(letmake_fun_with_expr~expr=Ast_builder.pexp_fun~loclblopt_argpatexprinletloc=pat.Ppxlib.ppat_locinmatch(lbl,pat)with|(Ppxlib.Labelled_|Optional_),_->make_fun_with_expr~expr:(map_component_expressionchild_expression)|Ppxlib.Nolabel,[%pat?()]->letloc=child_expression.pexp_locinmake_fun_with_expr~expr:[%expr[%ecomponent_ident~loc]~key[%echild_expression]]|_->Location.raise_errorf~loc"A labelled argument or () was expected")|`Newtype(ident,child_expression)->Ast_builder.pexp_newtype~locident(map_component_expressionchild_expression))inletopenPinletloc=expr.P.pexp_locinletcreate_component_expr=matchattributewith|`Native->[%exprBrisk_reconciler.Expert.nativeComponent]|`Component->[%exprBrisk_reconciler.Expert.component]in[%exprlet[%pcomponent_ident_pattern~loc]=[%ecreate_component_expr]~useDynamicKey:[%eAst_builder.(ebool~locuseDynamicKey)][%ecomponent_name]infun?(key=Brisk_reconciler.Key.none)->[%emap_component_expressionexpr]]letdeclare_attributectxtyp=letopenPpxlib.Attributeindeclare(attribute_nametyp)ctxPpxlib.Ast_pattern.(alt_option(single_expr_payload(pexp_ident(lident__')))(pstrnil))(function|Some{txt="useDynamicKey"}->true|Some{loc}->Location.raise_errorf~loc"A labelled argument or () was expected"|None->false)letexpr_attribute_component=declare_attributePpxlib.Attribute.Context.expression`Componentletexpr_attribute_nativeComponent=declare_attributePpxlib.Attribute.Context.expression`Nativeletexpr_attribute=function|`Component->expr_attribute_component|`Native->expr_attribute_nativeComponentletexprunmatched_expr=letconsume_attrattr=Ppxlib.Attribute.consume(expr_attributeattr)unmatched_exprinlettransform~useDynamicKeyattributeexpr=letloc=expr.P.pexp_locintransform_component_expr~useDynamicKey~attribute~component_name:[%expr__LOC__]exprinmatchconsume_attr`Componentwith|Some(expr,useDynamicKey)->transform~useDynamicKey`Componentexpr|None->(matchconsume_attr`Nativewith|Some(expr,useDynamicKey)->transform~useDynamicKey`Nativeexpr|None->unmatched_expr)letvalue_binding_attribute_component=declare_attributePpxlib.Attribute.Context.value_binding`Componentletvalue_binding_attribute_nativeComponent=declare_attributePpxlib.Attribute.Context.value_binding`Nativeletvalue_binding_attribute=function|`Component->value_binding_attribute_component|`Native->value_binding_attribute_nativeComponentletvalue_bindingunmatched_value_binding=letconsume_attrattr=Ppxlib.Attribute.consume(value_binding_attributeattr)unmatched_value_bindinginlettransform~useDynamicKeyattributevalue_binding=letvalue_binding_loc=value_binding.P.pvb_locinPpxlib.Ast_pattern.(parse(value_binding~pat:(ppat_var__)~expr:__))value_binding_locvalue_binding(funvar_patexpr->letcomponent_name=ATH.Exp.constant~loc:expr.P.pexp_loc(ATH.Const.stringvar_pat)inletcomponent_pat=value_binding.pvb_patinlettransformed_expr=transform_component_expr~useDynamicKey~attribute~component_nameexprinAst_builder.(value_binding~pat:component_pat~loc:value_binding_loc~expr:transformed_expr))inmatchconsume_attr`Componentwith|Some(value_binding,useDynamicKey)->transform~useDynamicKey`Componentvalue_binding|None->(matchconsume_attr`Nativewith|Some(value_binding,useDynamicKey)->transform~useDynamicKey`Nativevalue_binding|None->unmatched_value_binding)letregisterattribute=letopenPpxlibinExtension.declare(attribute_nameattribute)Extension.Context.structure_itemAst_pattern.(pstr(pstr_value__(value_binding~pat:(ppat_var__)~expr:__^::nil)^::nil))(fun~loc~pathrecursivepatexpr->letcomponent_name=ATH.Exp.constant~loc(ATH.Const.string(path^"."^pat))inlettransformed_expression=transform_component_expr~useDynamicKey:false~attribute~component_nameexprinletpat=ATH.Pat.var~loc(Ast_builder.Default.Located.mk~locpat)inmatchrecursivewith|Recursive->[%striletrec[%ppat]=[%etransformed_expression]]|Nonrecursive->[%strilet[%ppat]=[%etransformed_expression]])endmoduleHooks_ppx=structopenPpxlib(* Grab a list of all the output expressions *)letlint_hook_usage=objectinherit[bool]Ast_traverse.foldassupermethod!expressionexpr_=letopenExtension.Contextinmatchget_extensionexpressionexprwith|Some(({txt="hook"},_),_)->true|Some_|None->super#expressionexprfalseendletcontains_hook_expressionexpr=lint_hook_usage#expressionexprfalseletexpand~locexpr=letexpansion=matchexpr.pexp_descwith|Pexp_let(Nonrecursive,[binding],next_expression)->letwrapped_next_expression=ifcontains_hook_expressionexprthen[%expr[%enext_expression][%ehooks_ident~loc]]else[%expr[%enext_expression],[%ehooks_ident~loc]]in[%exprfun[%phooks_ident_pattern~loc]->let[%pbinding.pvb_pat],[%phooks_ident_pattern~loc]=[%ebinding.pvb_expr][%ehooks_ident~loc]in[%ewrapped_next_expression]]|Pexp_let(Recursive,_,_)->Location.raise_errorf~loc"'let%%hook' may not be recursive"|_->Location.raise_errorf~loc"'hook' can only be used with 'let'"in{expansionwithpexp_attributes=expr.pexp_attributes@expansion.pexp_attributes;}letextension=Extension.declare"hook"Extension.Context.expressionAst_pattern.(single_expr_payload__)(fun~loc~path:_expr->expand~locexpr)endletdeclaration_mapper=objectinheritPpxlib.Ast_traverse.mapassupermethod!expressione=lete=super#expressioneinDeclaration_ppx.expremethod!value_bindingbinding=letbinding=super#value_bindingbindinginDeclaration_ppx.value_bindingbindingendletjsx_mapper=objectinheritPpxlib.Ast_traverse.mapassupermethod!expressione=lete=super#expressioneinJSX_ppx.expreendlet()=Ppxlib.Driver.register_transformation"component"~impl:declaration_mapper#structure~extensions:[Declaration_ppx.register`Component;Declaration_ppx.register`Native;Hooks_ppx.extension;];Ppxlib.Driver.register_transformation"JSX"~impl:jsx_mapper#structure