Hi. I would like to build a CLI tool which extracts CSS classnames from the user’s CLJS source code and I am not sure where to start. The class names could be isolated strings in the source code like ["mx-2" "my-1"]
, inside keywords like :div.mx-2.my-1
, or if possible tagged by meta-data ^:css ["mx-2" "my-1"]
My first (dummy) approach will be to scan source file as edn, but I may get false positives and miss the classe-name which could be generated using macro.
I feel that the compiler could help, but I don’t know how exactly. Which path would you recommend? Do you have some links to relevant articles?
you might look into the machinery underneath clj-kondo. it already has a lot of static analysis tools that @borkdude has built for it that have a high likelihood of being useful
@vincent.cantin A few pointers: you can use https://github.com/borkdude/edamame for parsing Clojure code. You can also use tools.reader directly but it's likely that edamame works better with GraalVM native-image, if you want to make a native CLI. Another pointer: I think your code search can be implemented directly using https://github.com/borkdude/grasp.
As reference, grasp can search through my entire .m2 dir in 15 seconds (when parallelized)
and you get to use specs for what you want to search
@borkdude Thanks, I will take a look.
I found some leads:
• https://cljdoc.org/d/leiningen/leiningen/2.9.5/doc/leiningen-plugins#evaluating-in-project-context, so that I can use macro expanded source code.
• all-ns
, ns-interns
and ns-map
to list the root vars in a namepace, from which I can read associated metadata.
• I can ask the user to wrap its reagent functions in a macro which adds the ^:css
metadata derived from the body of the function.
If I missed something else, I would be glad to know - I am a beginner in this area.