rewrite-clj

respatialized 2024-04-16T12:32:03.797879Z

Question about using rewrite-clj across both Clojure and ClojureScript: how do you annotate a parsed node to distinguish whether it came from a Clojure or ClojureScript source? I can just assoc a :source-type entry into the node for :clj or :cljs, but I'm wondering if there's already a convention for this that I've overlooked. Sorry if this question has been asked before, it's hard to find using the right search query.

borkdude 2024-04-16T12:38:06.126279Z

There's probably not a convention for this in rewrite-clj itself. Perhaps you can explain a bit more about your use case. Are you using rewrite-clj FROM clj + cljs or are you using rewrite-clj in clj to parse .clj + .cljs?

respatialized 2024-04-16T12:38:33.322939Z

I intend to use it from both clj + cljs.

borkdude 2024-04-16T12:39:00.984519Z

I don't understand what you're asking then. What does it matter if a node is parsed inside of CLJ or CLJS?

respatialized 2024-04-16T12:43:06.149959Z

if, for example, you are annotating a node with metadata about the types of its elements, the concrete type of those elements will depend on the detail of the platform (e.g. no Ratio or Character types in JS).

borkdude 2024-04-16T12:44:54.180149Z

yes, but in CLJS rewrite-clj you can parse .clj so doesn't it matter more where the data came from rather than which platform you're performing the parsing on?

respatialized 2024-04-16T12:45:26.482889Z

isn't that what my original question is about?

borkdude 2024-04-16T12:47:24.007859Z

Well the answer to my first question did confuse me

borkdude 2024-04-16T12:48:09.218399Z

perhaps you can just give a concrete example to avoid any confusion

respatialized 2024-04-16T12:49:05.146299Z

sorry, I should have been more clear: I am writing a tool intended to work across all 4 of the following use cases (potentially more!) • parse + annotate clj from clj • parse + annotate cljs from cljs • parse + annotate clj from cljs • parse + annotate cljs from clj

borkdude 2024-04-16T12:50:22.256339Z

yes. and to clarify: isn't the "from" irrelevant to your use case?

respatialized 2024-04-16T12:51:12.932539Z

no, because a cljs file will have values of different types than a clj file

borkdude 2024-04-16T12:51:43.493239Z

a concrete example would be parsing 1/2 which is indeed different in clj than in cljs. But rewrite-clj doesn't care much about values, it has nodes which should behave the same in clj and cljs

borkdude 2024-04-16T12:53:09.427499Z

for example:

$ clj
Clojure 1.8.0
user=> (require '[rewrite-clj.parser :as p])
nil
user=> (p/parse-string "1/2")
<token: 1/2>
user=>
borkdude@m1-3 ~/dev/rewrite-clj (main) $ clj -M:cljs -m cljs.main -re node
ClojureScript 1.11.60
cljs.user=> (require '[rewrite-clj.parser :as p])
nil
cljs.user=> (p/parse-string "1/2")
<token: 1/2>
cljs.user=> (str (p/parse-string "1/2"))
"1/2"

respatialized 2024-04-16T12:55:04.136049Z

I recognize that I am going a bit beyond rewrite-clj itself and into the realm of static analysis. I think you answered my original question about whether there's a convention for this already. I think just adding extra information to the nodes themselves about the source (because they implement the right interfaces for it) will suffice to convey the information I may need to perform the annotations in a platform-aware way.

borkdude 2024-04-16T12:55:54.338339Z

ok

lread 2024-04-16T14:23:06.174899Z

@afoltzm I assume you are aware of https://github.com/clj-kondo/clj-kondo/tree/master/analysis? It has :lang key, which might help you. It is only populated for .cljc files, but easy to populate yourself for .clj and .cljs files.

respatialized 2024-04-16T16:01:34.013769Z

@lee clj-kondo has more dependencies than I want to introduce to my project, so I may have to go with something more minimal. thanks for the pointer to the :lang key - I will use the same convention for the library I’m working on!

borkdude 2024-04-16T16:03:27.293009Z

@afoltzm just out of curiosity, what tool are you creating?

respatialized 2024-04-16T16:03:47.644669Z

TBA 😉

borkdude 2024-04-16T16:06:03.436359Z

I guess I could make a lot of the dependencies that are in clj-kondo optional, leaving mostly transit and rewrite-clj ;)

borkdude 2024-04-16T16:06:22.499929Z

wait, it doesn't even depend on rewrite-clj, it has its own fork