forked from clj-kondo/clj-kondo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.clj
285 lines (265 loc) · 13.4 KB
/
config.clj
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
(ns clj-kondo.impl.config
{:no-doc true}
(:require
[clj-kondo.impl.utils :refer [vconj deep-merge map-vals]]))
(def default-config
'{;; no linting inside calls to these functions/macros
;; note that you will still get an arity error when calling the fn/macro itself incorrectly
:skip-args [#_clojure.core/comment #_cljs.core/comment]
:skip-comments false ;; convenient shorthand for :skip-args [clojure.core/comment cljs.core/comment]
;; linter level can be tweaked by setting :level to :error, :warn or :info (or any other keyword)
;; all linters are enabled by default, but can be turned off by setting :level to :off.
:linters {:invalid-arity {:level :error
:skip-args [#_riemann.test/test-stream]}
:not-a-function {:level :error
:skip-args [#_user/foo]}
:private-call {:level :error}
:inline-def {:level :warning}
:redundant-do {:level :warning}
:redundant-let {:level :warning}
:cond-else {:level :warning}
:syntax {:level :error}
:file {:level :error}
:missing-test-assertion {:level :warning}
:conflicting-alias {:level :error}
:constant-test-assertion {:level :warning}
:duplicate-map-key {:level :error}
:duplicate-set-key {:level :error}
:missing-map-value {:level :error}
:redefined-var {:level :warning}
:unreachable-code {:level :warning}
:datalog-syntax {:level :error}
:unbound-destructuring-default {:level :warning}
:unused-binding {:level :warning
:exclude-destructured-keys-in-fn-args false}
:unsorted-required-namespaces {:level :off}
:unused-namespace {:level :warning
;; don't warn about these namespaces:
:exclude [#_clj-kondo.impl.var-info-gen]
;; :simple-libspec true
}
:unresolved-symbol {:level :error
:exclude [;; ignore globally:
#_js*
;; ignore occurrences of service and event in call to riemann.streams/where:
#_(riemann.streams/where [service event])
;; ignore all unresolved symbols in one-of:
#_(clj-kondo.impl.utils/one-of)
(user/defproject) ;; ignore project.clj's defproject
(clojure.test/are [thrown? thrown-with-msg?])
(cljs.test/are [thrown? thrown-with-msg?])
(clojure.test/is [thrown? thrown-with-msg?])
(cljs.test/is [thrown? thrown-with-msg?])]}
:unresolved-namespace {:level :warning
:exclude [#_foo.bar
;; for example: foo.bar is always loaded in a user profile
]}
:misplaced-docstring {:level :warning}
:not-empty? {:level :warning}
:deprecated-var {:level :warning
#_:exclude
#_{foo.foo/deprecated-fn
;; suppress warnings in the following namespaces
{:namespaces [foo.bar "bar\\.*"]
;; or in these definitions:
:defs [foo.baz/allowed "foo.baz/ign\\.*"]}}}
:unused-referred-var {:level :warning
:exclude {#_#_taoensso.timbre [debug]}}
:unused-private-var {:level :warning}
:duplicate-require {:level :warning}
:refer {:level :off}
:refer-all {:level :warning
:exclude #{}}
:use {:level :warning}
:missing-else-branch {:level :warning}
:type-mismatch {:level :error}
:missing-docstring {:level :off}
:consistent-alias {:level :warning
;; warn when alias for clojure.string is
;; different from str
:aliases {#_clojure.string #_str}}
:unused-import {:level :warning}
:single-operand-comparison {:level :warning}
:single-key-in {:level :off}
:missing-clause-in-try {:level :warning}
:missing-body-in-when {:level :warning}
:hook {:level :error}
:format {:level :error}}
:lint-as {cats.core/->= clojure.core/->
cats.core/->>= clojure.core/->>
rewrite-clj.custom-zipper.core/defn-switchable clojure.core/defn
clojure.core.async/go-loop clojure.core/loop
clojure.test.check.generators/let clojure.core/let
cljs.core.async/go-loop clojure.core/loop
cljs.core.async.macros/go-loop clojure.core/loop
schema.core/defschema clojure.core/def
compojure.core/defroutes clojure.core/def
compojure.core/let-routes clojure.core/let}
:macroexpand {}
:output {:format :text ;; or :edn
:summary true ;; outputs summary at end, only applicable to output :text
;; outputs analyzed var definitions and usages of them
:analysis false
;; set to truthy to print progress while linting, only applicable to output :text
:progress false
;; output can be filtered and removed by regex on filename. empty options leave the output untouched.
:include-files [] #_["^src" "^test"]
:exclude-files [] #_["^cljs/core"]
;; the output pattern can be altered using a template. use {{LEVEL}} to print the level in capitals.
;; the default template looks like this:
;; :pattern "{{filename}}:{{row}}:{{col}}: {{level}}: {{message}}"
:canonical-paths false ;; set to true to see absolute file paths and jar files
}})
(defn merge-config! [cfg* cfg]
(if (empty? cfg) cfg*
(let [cfg (cond-> cfg
(:skip-comments cfg)
(-> (update :skip-args vconj 'clojure.core/comment 'cljs.core/comment))
(contains? (:linters cfg) :if) (assoc-in [:linters :missing-else-branch] (:if (:linters cfg))))]
(if (:replace (meta cfg))
cfg
(deep-merge cfg* cfg)))))
(defn fq-syms->vecs [fq-syms]
(map (fn [fq-sym]
(if-let [ns* (namespace fq-sym)]
[(symbol ns*) (symbol (name fq-sym))]
(throw (ex-info (str "Configuration error. Expected fully qualified symbol, got: " fq-sym)
{:type :clj-kondo/config}))))
fq-syms))
(defn skip-args*
([config]
(fq-syms->vecs (get config :skip-args)))
([config linter]
(fq-syms->vecs (get-in config [:linters linter :skip-args]))))
(def skip-args (memoize skip-args*))
(defn skip?
"we optimize for the case that disable-within returns an empty sequence"
([config callstack]
(when-let [disabled (seq (skip-args config))]
(some (fn [disabled-sym]
(some #(= disabled-sym %) callstack))
disabled)))
([config linter callstack]
(when-let [disabled (seq (skip-args config linter))]
(some (fn [disabled-sym]
(some #(= disabled-sym %) callstack))
disabled))))
(defn lint-as-config* [config]
(let [m (get config :lint-as)]
(zipmap (fq-syms->vecs (keys m))
(fq-syms->vecs (vals m)))))
(def lint-as-config (memoize lint-as-config*))
(defn lint-as [config v]
(get (lint-as-config config) v))
(def unused-namespace-excluded
(let [delayed-cfg (fn [config]
(let [excluded (get-in config [:linters :unused-namespace :exclude])
syms (set (filter symbol? excluded))
regexes (map re-pattern (filter string? excluded))]
{:syms syms :regexes regexes}))
delayed-cfg (memoize delayed-cfg)]
(fn [config ns-sym]
(let [{:keys [:syms :regexes]} (delayed-cfg config)]
(or (contains? syms ns-sym)
(let [ns-str (str ns-sym)]
(boolean (some #(re-find % ns-str) regexes))))))))
(def unused-referred-var-excluded
(let [delayed-cfg (fn [config]
(let [excluded (get-in config [:linters :unused-referred-var :exclude])]
(map-vals set excluded)))
delayed-cfg (memoize delayed-cfg)]
(fn [config ns-sym var-sym]
(let [excluded (delayed-cfg config)]
(when-let [vars (get excluded ns-sym )]
(contains? vars var-sym))))))
(def unresolved-namespace-excluded
(let [delayed-cfg (fn [config]
(set (get-in config [:linters :unresolved-namespace :exclude])))
delayed-cfg (memoize delayed-cfg)]
(fn [config ns-sym]
(let [excluded (delayed-cfg config)]
(contains? excluded ns-sym)))))
(def unresolved-symbol-excluded
(let [delayed-cfg
(fn [config]
(let [excluded (get-in config [:linters :unresolved-symbol :exclude])
syms (set (filter symbol? excluded))
calls (filter list? excluded)]
{:excluded syms
:excluded-in
(reduce (fn [acc [fq-name excluded]]
(let [ns-name (symbol (namespace fq-name))
var-name (symbol (name fq-name))]
(update acc [ns-name var-name]
(fn [old]
(cond (nil? old)
(if excluded
(set excluded)
identity)
(set? old)
(if excluded
(into old excluded)
old)
:else identity)))))
{} calls)}))
delayed-cfg (memoize delayed-cfg)]
(fn [config callstack sym]
(let [{:keys [:excluded :excluded-in]} (delayed-cfg config)]
(or (contains? excluded sym)
(some #(when-let [check-fn (get excluded-in %)]
;; e.g. for user/defproject, check-fn is identity, so any
;; truthy value will be excluded inside of that
(check-fn sym))
callstack))))))
(def deprecated-var-excluded
(let [delayed-cfg (fn [config var-sym]
(let [excluded (get-in config [:linters :deprecated-var :exclude var-sym])
namespaces (:namespaces excluded)
namespace-regexes (map re-pattern (filter string? namespaces))
namespace-syms (set (filter symbol? namespaces))
defs (:defs excluded)
def-regexes (map re-pattern (filter string? defs))
def-syms (set (filter symbol? defs))]
{:namespace-regexes namespace-regexes
:namespace-syms namespace-syms
:def-regexes def-regexes
:def-syms def-syms}))
delayed-cfg (memoize delayed-cfg)]
(fn [config var-sym excluded-ns excluded-in-def]
(let [{:keys [:namespace-regexes :namespace-syms
:def-regexes :def-syms]} (delayed-cfg config var-sym)]
(or (when excluded-in-def
(let [excluded-in-def (symbol (str excluded-ns) (str excluded-in-def))]
(or (contains? def-syms excluded-in-def)
(let [excluded-in-def-str (str excluded-in-def)]
(boolean (some #(re-find % excluded-in-def-str) def-regexes))))))
(contains? namespace-syms excluded-ns)
(let [ns-str (str excluded-ns)]
(boolean (some #(re-find % ns-str) namespace-regexes))))))))
(def type-mismatch-config
(let [delayed-cfg
(fn [config var-ns var-name]
;; (prn (get-in config [:linters :type-mismatch :namespaces 'foo 'foo]))
(get-in config [:linters :type-mismatch :namespaces var-ns var-name]))
delayed-cfg (memoize delayed-cfg)]
delayed-cfg))
(def unused-private-var-excluded
(let [delayed-cfg
(fn [config]
(let [syms (get-in config [:linters :unused-private-var :exclude])
vecs (fq-syms->vecs syms)]
(set vecs)))
delayed-cfg (memoize delayed-cfg)]
(fn [config ns-name var-name]
(contains? (delayed-cfg config) [ns-name var-name]))))
(def refer-all-excluded?
(let [delayed-cfg (fn [config]
(let [syms (get-in config [:linters :refer-all :exclude])]
(set syms)))
delayed-cfg (memoize delayed-cfg)]
(fn [config referred-all-ns]
(let [excluded (delayed-cfg config)]
(contains? excluded referred-all-ns)))))
;;;; Scratch
(comment
)