borkdude / grasp

Grep Clojure code using clojure.spec regexes
Eclipse Public License 1.0
242 stars 7 forks source link

Add conveniences for searching for function calls? #20

Closed holyjak closed 2 years ago

holyjak commented 2 years ago

Add a convenience macro list similar to cat but checking that it is a list and something that makes it easy to match on a call to a particular function. What I had to do, to find where my-fun is called with three arguments (b/c we are deprecating the arity so I wanted to know where we use it) was:

(s/def ::my-fn (s/and symbol? (comp #{'my.ns/my-fn} g/resolve-symbol)))
(g/grasp "./" (g/cat ::my-fn any? any?  any?)

That is obvioulsy quite neat already. But I could have imagined st. like:

(g/grasp "./" (g/list (rsym my.ns/my-fn) any? any?  any?)

where rsym is a macro that expands into (s/and symbol? (comp #{<the symbol>} g/resolve-symbol))).

I would be happy to contribute code for this.

borkdude commented 2 years ago

I would prefer that as a function rather than a macro:

(resolved-symbol 'my.ns/my-fn)

This makes it more composeable.

holyjak commented 2 years ago

I gather rsym is to cryptic :) But if we have both resolved-symbol and resolve-symbol, don't we expose the user to a great riks of mixing them up?

borkdude commented 2 years ago

OK, let's add rsym but as a function.

borkdude commented 2 years ago

Sorry, I missed the part about list. Why do you need this? Often I think list? should just be seq? instead, since list? doesn't match on (cons 1 [1 2 3]) for example.

holyjak commented 2 years ago

Maybe I don't? The idea was that function calls in Clojure are calls so it would be natural to search for (list (rsym 'myns/myfn int?)). But I guess seq works just as well and makes list unnecessary. A proper clojure dev should know that 😅

borkdude commented 2 years ago

I recommend using seq? and to forget about list? forever :).

holyjak commented 2 years ago

Well, in this particular case I really, really want a list because fn calls cannot be lazy sequences :-)

But seq works fine it I guess it is worthwile keeping the api small

borkdude commented 2 years ago

The parsed s-expression is usually not a lazy sequence, but it can be a Cons because of macro-expansions.

holyjak commented 2 years ago

Ah