Skip to content

Commit

Permalink
Pass arity as an option to clj-kondo/transform
Browse files Browse the repository at this point in the history
- use arity to decide whether to use `{:op :rest}` or `:seqable`
  • Loading branch information
tvaisanen committed Sep 22, 2023
1 parent 852d7c3 commit 68d1a82
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/malli/clj_kondo.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@
(defmethod accept :qualified-symbol [_ _ _ _] :symbol)
(defmethod accept :uuid [_ _ _ _] :any) ;;??

(defmethod accept :+ [_ _ _ _] :seqable)
(defmethod accept :* [_ _ _ _] :seqable)
(defmethod accept :? [_ _ _ _] :seqable)
(defmethod accept :repeat [_ _ [child] _] {:op :rest, :spec child})
(defn -seqable-or-rest [_ _ [child] {:keys [arity]}]
(if (= arity :varargs)
{:op :rest :spec child}
:seqable))

(defmethod accept :+ [_ _ children options] (-seqable-or-rest nil nil children options))
(defmethod accept :* [_ _ children options] (-seqable-or-rest nil nil children options))
(defmethod accept :? [_ _ children options] (-seqable-or-rest nil nil children options))
(defmethod accept :repeat [_ _ children options] (-seqable-or-rest nil nil children options))

(defmethod accept :cat [_ _ children _] children)
(defmethod accept :catn [_ _ children _] (mapv last children))
(defmethod accept :alt [_ _ _ _] :any) ;;??
Expand Down Expand Up @@ -169,7 +175,7 @@
(reduce
(fn [acc schema]
(let [{:keys [input output arity min]} (m/-function-info schema)
args (transform input)
args (transform input {:arity arity})
ret (transform output)]
(conj acc (cond-> {:ns ns-name
:name name
Expand Down
23 changes: 18 additions & 5 deletions test/malli/clj_kondo_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
(m/=> clj-kondo-issue-1922-3
[:=> [:cat [:map [:keys [:? :string]]]] :nil])

(defn clj-kondo-issue-1922-4 [_x])
(m/=> clj-kondo-issue-1922-4
[:function
[:=> [:cat :int :int] :nil]
[:=> [:cat :int :int [:repeat :int]] :nil]])

(deftest clj-kondo-integration-test

(is (= {:op :keys,
Expand All @@ -75,7 +81,7 @@
{'kikka
{:arities {1 {:args [:int],
:ret :int},
:varargs {:args [:int :int :seqable],
:varargs {:args [:int :int {:op :rest :spec :int}],
:ret :int,
:min-arity 2}}}
'siren
Expand All @@ -94,7 +100,14 @@
'clj-kondo-issue-1922-3
{:arities {1 {:args [{:op :keys
:req {:keys :seqable}}]
:ret :nil}}}}}]
:ret :nil}}}

'clj-kondo-issue-1922-4
{:arities {2 {:args [:int :int]
:ret :nil}
:varargs {:args [:int :int {:op :rest :spec :int}]
:ret :nil
:min-arity 2}}}}}]

#?(:clj
(is (= expected-out
Expand All @@ -110,11 +123,11 @@
(clj-kondo/linter-config)
(get-in [:linters :type-mismatch :namespaces]))))))
(testing "sequential elements"
(is (= {:op :rest :spec :int}
(is (= :seqable
(clj-kondo/transform [:repeat :int])))
(is (= {:op :rest :spec {:op :keys :req {:price :int}}}
(is (= :seqable
(clj-kondo/transform [:repeat [:map [:price :int]]])))
(is (= {:op :rest :spec [:int]}
(is (= :seqable
(clj-kondo/transform [:repeat [:tuple :int]]))))

(testing "regular expressions"
Expand Down

0 comments on commit 68d1a82

Please sign in to comment.