/
clj_kondo.cljc
189 lines (168 loc) · 7.59 KB
/
clj_kondo.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(ns malli.clj-kondo
(:require #?(:clj [clojure.java.io :as io])
[fipp.edn :as fipp]
[malli.core :as m]))
(declare transform)
(defmulti accept (fn [name _schema _children _options] name) :default ::default)
(defmethod accept ::default [_ _ _ _] :any)
(defmethod accept 'any? [_ _ _ _] :any)
(defmethod accept 'some? [_ _ _ _] :any) ;;??
(defmethod accept 'number? [_ _ _ _] :number)
(defmethod accept 'integer? [_ _ _ _] :int)
(defmethod accept 'int? [_ _ _ _] :int)
(defmethod accept 'pos-int? [_ _ _ _] :pos-int)
(defmethod accept 'neg-int? [_ _ _ _] :neg-int)
(defmethod accept 'nat-int? [_ _ _ _] :nat-int)
(defmethod accept 'nat-int? [_ _ _ _] :nat-int)
(defmethod accept 'pos? [_ _ _ _] :pos-int)
(defmethod accept 'neg? [_ _ _ _] :neg-int)
(defmethod accept 'float? [_ _ _ _] :double)
(defmethod accept 'double? [_ _ _ _] :double)
(defmethod accept 'boolean? [_ _ _ _] :boolean)
(defmethod accept 'string? [_ _ _ _] :string)
(defmethod accept 'ident? [_ _ _ _] :symbol) ;;??
(defmethod accept 'simple-ident? [_ _ _ _] :symbol) ;;??
(defmethod accept 'qualified-ident? [_ _ _ _] :symbol) ;;??
(defmethod accept 'keyword? [_ _ _ _] :keyword)
(defmethod accept 'simple-keyword? [_ _ _ _] :keyword)
(defmethod accept 'qualified-keyword? [_ _ _ _] :keyword)
(defmethod accept 'symbol? [_ _ _ _] :symbol)
(defmethod accept 'simple-symbol? [_ _ _ _] :symbol)
(defmethod accept 'qualified-symbol? [_ _ _ _] :symbol)
(defmethod accept 'uuid? [_ _ _ _] :any) ;;??
(defmethod accept 'uri? [_ _ _ _] :any) ;;??
(defmethod accept 'decimal? [_ _ _ _] :double) ;;??
(defmethod accept 'inst? [_ _ _ _] :any) ;;??
(defmethod accept 'seqable? [_ _ _ _] :seqable)
(defmethod accept 'indexed? [_ _ _ _] :vector) ;;??
(defmethod accept 'map? [_ _ _ _] :map)
(defmethod accept 'vector? [_ _ _ _] :vector)
(defmethod accept 'list? [_ _ _ _] :list)
(defmethod accept 'seq? [_ _ _ _] :seq)
(defmethod accept 'char? [_ _ _ _] :char)
(defmethod accept 'set? [_ _ _ _] :set)
(defmethod accept 'nil? [_ _ _ _] :nil)
(defmethod accept 'false? [_ _ _ _] :boolean) ;;??
(defmethod accept 'true? [_ _ _ _] :boolean) ;;??
(defmethod accept 'zero? [_ _ _ _] :int) ;;??
#?(:clj (defmethod accept 'rational? [_ _ _ _] :double)) ;;??
(defmethod accept 'coll? [_ _ _ _] :coll)
(defmethod accept 'empty? [_ _ _ _] :seq) ;;??
(defmethod accept 'associative? [_ _ _ _] :associative)
(defmethod accept 'sequential? [_ _ _ _] :sequential)
(defmethod accept 'ratio? [_ _ _ _] :int) ;;??
(defmethod accept 'bytes? [_ _ _ _] :char-sequence) ;;??
(defmethod accept 'ifn? [_ _ _ _] :ifn)
(defmethod accept 'fn? [_ _ _ _] :fn)
(defmethod accept :> [_ _ _ _] :number) ;;??
(defmethod accept :>= [_ _ _ _] :number) ;;??
(defmethod accept :< [_ _ _ _] :number) ;;??
(defmethod accept :<= [_ _ _ _] :number) ;;??
(defmethod accept := [_ _ _ _] :any) ;;??
(defmethod accept :not= [_ _ _ _] :any) ;;??
(defmethod accept :and [_ _ _ _] :any) ;;??
(defmethod accept :or [_ _ _ _] :any) ;;??
(defmethod accept :orn [_ _ _ _] :any) ;;??
(defmethod accept :not [_ _ _ _] :any) ;;??
(defmethod accept :map [_ _ children _]
(let [{req true opt false} (->> children (group-by (m/-comp not :optional second)))
opt (apply array-map (mapcat (fn [[k _ s]] [k s]) opt))
req (apply array-map (mapcat (fn [[k _ s]] [k s]) req))]
(cond-> {:op :keys}, (seq opt) (assoc :opt opt), (seq req) (assoc :req req))))
(defmethod accept :map-of [_ _ _ _] :map) ;;??
(defmethod accept :vector [_ _ _ _] :vector)
(defmethod accept :sequential [_ _ _ _] :sequential)
(defmethod accept :set [_ _ _ _] :set)
(defmethod accept :enum [_ _ children _]
(let [types (->> children (map type) (set))]
(if (< 1 (count types))
:any
(let [child (first children)]
(cond
(string? child) :string
(keyword? child) :keyword
(integer? child) :int
(char? child) :char
(number? child) :number
(symbol? child) :symbol
:else :any)))))
(defmethod accept :maybe [_ _ [child] _] (if (keyword? child) (keyword "nilable" (name child)) child))
(defmethod accept :tuple [_ _ children _] children)
(defmethod accept :multi [_ _ _ _] :any) ;;??
(defmethod accept :re [_ _ _ _] :regex)
(defmethod accept :fn [_ _ _ _] :fn)
(defmethod accept :ref [_ _ _ _] :any) ;;??
(defmethod accept :=> [_ _ _ _] :fn)
(defmethod accept :function [_ _ _ _] :fn)
(defmethod accept :schema [_ schema _ options] (transform (m/deref schema) options))
(defmethod accept ::m/schema [_ schema _ options] (transform (m/deref schema) options))
(defmethod accept ::m/val [_ _ children _] (first children))
(defmethod accept :any [_ _ _ _] :any)
(defmethod accept :nil [_ _ _ _] :nil)
(defmethod accept :string [_ _ _ _] :string)
(defmethod accept :int [_ _ _ _] :int)
(defmethod accept :double [_ _ _ _] :double)
(defmethod accept :boolean [_ _ _ _] :boolean)
(defmethod accept :keyword [_ _ _ _] :keyword)
(defmethod accept :qualified-keyword [_ _ _ _] :keyword)
(defmethod accept :symbol [_ _ _ _] :symbol)
(defmethod accept :qualified-symbol [_ _ _ _] :symbol)
(defmethod accept :uuid [_ _ _ _] :any) ;;??
(defmethod accept :+ [_ _ [child] _] {:op :rest, :spec child})
(defmethod accept :* [_ _ [child] _] {:op :rest, :spec child})
(defmethod accept :? [_ _ [child] _] {:op :rest, :spec child})
(defmethod accept :repeat [_ _ [child] _] {:op :rest, :spec child})
(defmethod accept :cat [_ _ children _] children)
(defmethod accept :catn [_ _ children _] (mapv last children))
(defmethod accept :alt [_ _ _ _] :any) ;;??
(defmethod accept :altn [_ _ _ _] :any) ;??
(defmethod accept :merge [_ schema _ options] (transform (m/deref schema) options))
(defmethod accept :union [_ schema _ options] (transform (m/deref schema) options))
(defmethod accept :select-keys [_ schema _ options] (transform (m/deref schema) options))
(defn- -walk [schema _ children options] (accept (m/type schema) schema children options))
(defn -transform [?schema options] (m/walk ?schema -walk options))
;;
;; public api
;;
(defn transform
([?schema]
(transform ?schema nil))
([?schema options]
(-transform ?schema options)))
#?(:clj
(defn save! [config]
(let [cfg-file (io/file ".clj-kondo" "metosin" "malli-types" "config.edn")]
;; delete the old file if exists (does not throw)
(.delete (io/file ".clj-kondo" "configs" "malli" "config.edn"))
(io/make-parents cfg-file)
(spit cfg-file (with-out-str (fipp/pprint config {:width 120})))
config)))
(defn from [{:keys [schema ns name]}]
(let [ns-name (-> ns str symbol)
schema (if (= :function (m/type schema)) schema (m/into-schema :function nil [schema] (m/options schema)))]
(reduce
(fn [acc schema]
(let [{:keys [input output arity min]} (m/-function-info schema)
args (transform input)
ret (transform output)]
(conj acc (cond-> {:ns ns-name
:name name
:arity arity
:args args
:ret ret}
(= arity :varargs) (assoc :min-arity min)))))
[] (m/children schema))))
(defn collect
([] (collect nil))
([ns]
(let [-collect (fn [k] (or (nil? ns) (= k (symbol (str ns)))))]
(->> (for [[k vs] (m/function-schemas) :when (-collect k) [_ v] vs v (from v)] v)))))
(defn linter-config [xs]
(reduce
(fn [acc {:keys [ns name arity] :as data}]
(assoc-in
acc [:linters :type-mismatch :namespaces (symbol (str ns)) name :arities arity]
(select-keys data [:args :ret :min-arity])))
{:linters {:unresolved-symbol {:exclude ['(malli.core/=>)]}}} xs))
#?(:clj
(defn emit! [] (-> (collect) (linter-config) (save!)) nil))