rewrite-clj

2022-10-26T04:50:07.785709Z

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

2022-10-26T11:35:59.426319Z

That’s #clj-kondo

snoe 2022-10-26T14:38:31.255749Z

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.

2022-10-26T14:45:08.286079Z

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

2022-10-26T14:45:33.392829Z

Thank you for your suggestions

snoe 2022-10-26T14:45:38.929889Z

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

2022-10-26T14:48:31.813609Z

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

snoe 2022-10-26T14:49:33.100319Z

here's the pr, so just before this. https://github.com/clojure-lsp/clojure-lsp/pull/261/commits/a1ee3756c88b70609befc5bce5ca0b11a92770f1

πŸ‘ 1
❀️ 1
2022-10-26T14:54:07.101849Z

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**). @borkdude is also very helpful if you have questions about how it all works

2022-10-26T14:54:44.973779Z

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

borkdude 2022-10-26T14:55:07.662529Z

@s_zharinov 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
2022-10-26T15:10:23.163389Z

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.

2022-10-26T15:10:41.451099Z

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

πŸ‘€ 1
2022-10-26T15:15:45.034889Z

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 πŸ™