Fork me on GitHub
#clojurescript
<
2021-10-30
>
AJ Jaro15:10:40

I’d like to be able to print out the docstring for a macro that used in CLJS. The macro is defined in a CLJ file then required in a CLJS file via require-macros. I’m working towards documenting some of our work standards and having the docstring available to present would be super helpful. In CLJ, I would normally use (:doc (meta #'macro-here)), but that doesn’t seem to work the same when requiring macros. Is there a different way I should be solving this?

emccue17:10:01

(defmacro macro-doc [macro-name] 
  (:doc (meta (var macro-name))))

emccue17:10:25

there might be a better way, but you can pull it out in another macro since that runs in the CLJ scope

AJ Jaro22:10:26

@U3JH98J4R Are you suggesting writing a CLJ macro? That might be useful in a number of places for us anyway

AJ Jaro22:11:15

@U3JH98J4R Thanks. I am struggling with the details here. I wrote the macro like this and used refer-macros, but I’m getting the error that it can’t resolve the passed in var

(defmacro docstring [fn]
  `(:doc (meta (var ~fn))))
(docstring defn) Unable to resolve var: defn in this context

emccue22:11:10

Get rid of the ` and the ~

emccue22:11:32

When you return a quoted list, that's what gets used as the CLJS code

emccue22:11:10

This whole process needs to run in CLJ, and the thing that gets inserted into CLJS as code would just be the literal string

AJ Jaro22:11:31

Ok, there’s more thinking I need to do here because it worked successfully in a CLJ REPL without those changes

emccue22:11:55

That's because the var exists in CLJ

emccue22:11:28

In CLJ it can be just a regular function