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.
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?
I intend to use it from both clj + cljs.
I don't understand what you're asking then. What does it matter if a node is parsed inside of CLJ or CLJS?
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).
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?
isn't that what my original question is about?
Well the answer to my first question did confuse me
perhaps you can just give a concrete example to avoid any confusion
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
yes. and to clarify: isn't the "from" irrelevant to your use case?
no, because a cljs file will have values of different types than a clj file
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
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"
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.
ok
@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.
@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!
@afoltzm just out of curiosity, what tool are you creating?
TBA 😉
I guess I could make a lot of the dependencies that are in clj-kondo optional, leaving mostly transit and rewrite-clj ;)
wait, it doesn't even depend on rewrite-clj, it has its own fork