rewrite-clj

vemv 2021-11-16T19:41:52.151600Z

from time to time I dream of a declarative refactoring engine, e.g. for every second argument given to foo , wrap it in a hashmap with key :bar. is it an idea you've entertained much? I'd imagine that is much much easier said than done 😇

lread 2021-11-16T20:59:09.156900Z

Hi @vemv, no, that has not come to my mind. But there are more things in heaven and earth, vemv, than are dreamt of my wee brain! So you’d have some declarative recipe that turns:

(foo 1 2 3 4 5 6)
That would produce this?:
(for 1 {:bar 2} 3 {:bar 4} 5 {:bar 6})
Without thinking about it too much, I’d probably lean toward simply coding up something in Clojure to do the transform instead of creating some sort of DSL.

borkdude 2021-11-16T20:59:39.157300Z

I would prefer coding it over a DSL myself.

borkdude 2021-11-16T21:04:19.160400Z

Note that you can use other tools (like clj-kondo) to detect the positions of all the calls of foo, and then use fairly thin rewrite-clj code to transform those calls

borkdude 2021-11-16T21:05:25.161900Z

The problem with a DSL is usually that it covers 80% of what you want, but then you leave it for coding anyway

vemv 2021-11-16T21:06:04.162200Z

> I would prefer coding it over a DSL myself. I have mixed feelings about it. Probably a fully generalized DSL would be overly dense, but I sense that telling people to code rewrite-clj script for any possible refactoring idea would not make people very enthusiatic. Maybe the middle ground would be implementing different 'intents' that a user may want. 1 intent == 1 defn, defns might compose There's more to writing a rewrite-clj 'script' anyway i.e. traversing a codebase, filtering out false positives, etc. So I sense there's a place for a 'framework'

borkdude 2021-11-16T21:08:53.162600Z

A lot of what is in clojure-lsp may already worth getting out into a library

borkdude 2021-11-16T21:09:08.163Z

and libraries I would much prefer over a framework, but this is often just a matter of framing :)