Registering an attribute transformation with ppxlib

ppx
ppxlib

#1

I want to register a rewriter for ppxlib that matches expressions like this:

([@JSX] div ~children:[] ())

Generally it should match any expression with a [@JSX] attribute. The payload is always empty.

Is there an API to do this conveniently? The only one that’s documented is the Extension one.


#2

Doesn’t look like there’s any “canonical” way to do this yet like there is for extensions, but the most convenient is to inherit from Ast_traverse.map and override the #expression method:

let map_exprs = object
  inherit Ast_traverse.map as super

  method! expression expr =
    let expr = super#expression expr in
    if contains_jsx expr.pexp_attributes then
      (* do the transform *)
    else
      expr
end

And then register the transformation with ~impl and ~intf:

let () =
  Ppxlib.Driver.register_transformation
    "ppx_jsx_tyxml"
    ~impl:map_exprs#structure
    ~intf:map_exprs#signature