From cee4aeb0451b5c45138f3b1228dbc8b84508589e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20V=C3=A4is=C3=A4nen?= Date: Fri, 22 Sep 2023 22:06:39 +0300 Subject: [PATCH] Pass arity as an option to `clj-kondo/transform` - use arity to decide whether to use `{:op :rest}` or `:seqable` --- src/malli/clj_kondo.cljc | 16 +++++++++++----- test/malli/clj_kondo_test.cljc | 29 ++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/malli/clj_kondo.cljc b/src/malli/clj_kondo.cljc index a994db60b..0e824fb37 100644 --- a/src/malli/clj_kondo.cljc +++ b/src/malli/clj_kondo.cljc @@ -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) ;;?? @@ -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 diff --git a/test/malli/clj_kondo_test.cljc b/test/malli/clj_kondo_test.cljc index a80827509..82145530a 100644 --- a/test/malli/clj_kondo_test.cljc +++ b/test/malli/clj_kondo_test.cljc @@ -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, @@ -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 @@ -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 @@ -110,13 +123,19 @@ (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" (is (= :string (clj-kondo/transform [:re "kikka"])) "the :re schema models a string, clj-kondo's :regex a Pattern object"))) + +(deftest accept-test + (testing "foo" + (is (clj-kondo/accept nil nil nil nil ))))