Created
April 17, 2024 06:06
-
-
Save anmonteiro/91d25ee8e0544df08b962f7c0e6e4c27 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Ppxlib | |
let icon ~loc name = | |
let open Ast_helper in | |
Str.module_ | |
(Mb.mk | |
{ loc; txt = Some name } | |
(Mod.structure | |
[ Str.primitive | |
(Val.mk | |
~attrs: | |
[ Attr.mk | |
{ loc; txt = "mel.module" } | |
(PStr [ [%stri "lucide-react"] ]) | |
; Attr.mk { loc; txt = "react.component" } (PStr []) | |
] | |
~prim:[ name ] | |
{ loc; txt = "make" } | |
[%type: | |
?className:string | |
-> ?color:string | |
-> ?size:int | |
-> ?strokeWidth:int | |
-> ?absoluteStrokeWidth:bool | |
-> React.element]) | |
])) | |
let rule = | |
let handler ~ctxt lident = | |
let loc = Expansion_context.Extension.extension_point_loc ctxt in | |
match lident with | |
| Lident name -> icon ~loc name | |
| _ -> | |
[%stri | |
[%%ocaml.error | |
[%e | |
Ast_helper.Exp.constant | |
(Pconst_string | |
( Format.asprintf | |
"Invalid lucide icon name `%s'" | |
(Longident.name lident) | |
, loc | |
, None ))]]] | |
in | |
let extender = | |
Extension.V3.declare | |
"icon" | |
Structure_item | |
Ast_pattern.( | |
single_expr_payload (pexp_ident __ ||| pexp_construct __ none)) | |
handler | |
in | |
Context_free.Rule.extension extender | |
let () = Driver.V2.register_transformation "lucide" ~rules:[ rule ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment