Fork me on GitHub
#rewrite-clj
<
2022-10-26
>
zharinov04:10:07

Please, can someone point me out the example of code that statically analyses symbol resolution?

(ns user
  (:require [foo.bar :refer [baz] :as foobar]))

(println foobar/baz)
(println baz)
I would like to study from someone's code that can resolve baz and foobar/baz to foo.bar/baz, and also can handle things like let-bindings, etc

Noah Bogart11:10:59

Thatโ€™s #CHY97NXE2

snoe14:10:31

statically analyzing ns is actually quite difficult because it is so so flexible. Highly recommend using kondo as a library for static analysis to leverage the community effort. Though you can look at early clojure-lsp (parser.clj) commits before we switched to kondo.

zharinov14:10:08

So, you folks confirmed I have to dive in the clj-kondo source, which seems a bit scary to me for now ๐Ÿ˜…

zharinov14:10:33

Thank you for your suggestions

snoe14:10:38

If you want an example take a look at the old lsp code.

zharinov14:10:31

Yeah, I want to adapt some code for the CLJS, not necessary the most elaborate implementation

Noah Bogart14:10:07

I know clj-kondo seems scary, but it's honestly quite readable. If you start from https://github.com/clj-kondo/clj-kondo/blob/707454f78069c59811405c010c2349c0b33fda47/src/clj_kondo/main.clj#L184, trace the calls: > clj-kondo.main/-main -> clj-kondo.main/main -> clj-kondo.core/run! -> clj-kondo.core.impl/process-files -> clj-kondo.core.impl/process-file -> clj-kondo.core.impl/schedule (assuming not parallel for simplicity) -> clj-kondo.impl.analyzer/analyze-input -> clj-kondo.impl.analyzer/analyze-expressions and then at that point, clj-kondo loops over every top-level expression in the parsed input (from the file or string) and uses recursion to handle all sub-expressions (with clj-kondo.impl.analyzer/analyze-expression**). @U04V15CAJ is also very helpful if you have questions about how it all works

Noah Bogart14:10:44

@U0BUV7XSA that's a cool PR! I didn't know that clojure-lsp did its own analysis before using clj-kondo.

borkdude14:10:07

@U0GA10KA8 You don't have to read the source code of clj-kondo to be able to use its analysis: https://github.com/clj-kondo/clj-kondo/blob/master/analysis/README.md

๐Ÿ‘ 2
zharinov15:10:23

Just found it, looks like it provides everything I need (and even much more). Ideally, I'm aiming to have self-contained node.js library in the project I'm playing with.

zharinov15:10:41

It's plugin for Clojure support in Prettier formatting tooling, to be specific.

๐Ÿ‘€ 1
zharinov15:10:45

I think I'll just delay this particular decision and focus on another concerns for now. I appreciate you made me sure symbol resolution problem can be solved one way or another ๐Ÿ™