-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
kondo.clj
400 lines (356 loc) · 15.9 KB
/
kondo.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
(ns clojure-lsp.kondo
(:require
[babashka.fs :as fs]
[clj-kondo.core :as kondo]
[clojure-lsp.config :as config]
[clojure-lsp.dep-graph :as dep-graph]
[clojure-lsp.feature.diagnostics :as f.diagnostic]
[clojure-lsp.logger :as logger]
[clojure-lsp.settings :as settings]
[clojure-lsp.shared :as shared]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as string]
[medley.core :as medley]))
(set! *warn-on-reflection* true)
(defn clj-kondo-version []
(string/trim (slurp (io/resource "CLJ_KONDO_VERSION"))))
(def clj-kondo-analysis-batch-size 150)
(defn ^:private project-config-dir [project-root-uri]
;; TODO: might be better to use clj-kondo.impl.core/config-dir, but it's not
;; yet part of the public kondo api.
#_(kondo/config-dir (shared/uri->filename project-root-uri))
(let [config-dir (io/file (shared/uri->filename project-root-uri) ".clj-kondo")]
(when (and (shared/file-exists? config-dir)
(shared/directory? config-dir))
config-dir)))
(defn ^:private config-path [config-dir]
(let [config-file (io/file config-dir "config.edn")]
(.getCanonicalPath ^java.io.File config-file)))
(defn home-config-path []
(let [xdg-config-home (or (config/get-env "XDG_CONFIG_HOME")
(io/file (config/get-property "user.home") ".config"))]
(config-path (io/file xdg-config-home "clj-kondo"))))
(defn project-config-path [project-root-uri]
(config-path (project-config-dir project-root-uri)))
(defn config-hash
[project-root]
(let [err (java.io.StringWriter.)]
(binding [*err* err]
(let [result (-> project-root
(io/file ".clj-kondo")
kondo/resolve-config
kondo/config-hash)]
(when-not (string/blank? (str err))
(logger/error (str err)))
result))))
(defn db-with-analysis
"Update `db` with normalized kondo analysis."
[db {:keys [analysis external?]}]
(-> db
(update :analysis merge analysis)
;; Whenever the analysis is updated, we refresh the dep graph. This is the
;; only place this is done, so to keep the dep graph in sync with the
;; analysis, everything that puts analysis in the db must either call this
;; function or db-with-results, which calls this function.
(dep-graph/refresh-analysis (select-keys (:analysis db) (keys analysis))
analysis
(not external?))))
(defn db-with-results
"Update `db` with normalized kondo result."
[db {:keys [findings config] :as results}]
(-> db
(db-with-analysis results)
(update :findings merge findings)
(shared/assoc-some :kondo-config config)))
(defn ^:private element-with-fallback-name-position [element]
(assoc element
:name-row (or (:name-row element) (:row element))
:name-col (or (:name-col element) (:col element))
:name-end-row (or (:name-end-row element) (:end-row element))
:name-end-col (or (:name-end-col element) (:end-col element))))
;;;; Normalization
;; The analysis we get back from clj-kondo is indexed by bucket
;; {(bucket [element+])*}
;; We re-index by uri then bucket
;; {(uri {(bucket [element+])+})*}
;; We also normalize elements somewhat, changing their keys, and even spliting
;; some into two elements. As such, the buckets we get from clj-kondo aren't
;; exactly the same as the buckets we have in our db.
(defn ^:private element->normalized-elements [{:keys [bucket arglist-strs] :as element}]
(case bucket
:var-definitions
[(-> element
(shared/assoc-some :arglist-kws (->> arglist-strs
(keep (fn [arglist-str]
(try
(:keys (first (edn/read-string arglist-str)))
(catch Exception _ nil))))
seq)))]
;; We create two elements here (and maybe more for refer)
:namespace-usages
(cond-> [(set/rename-keys element {:to :name})]
(:alias element)
(conj (-> element
(assoc :bucket :namespace-alias)
(set/rename-keys {:alias-row :name-row
:alias-col :name-col
:alias-end-row :name-end-row
:alias-end-col :name-end-col}))))
(:locals :local-usages :java-class-usages :symbols)
[(element-with-fallback-name-position element)]
:keywords
[(-> element
element-with-fallback-name-position
(assoc :bucket (if (:reg element) :keyword-definitions :keyword-usages)))]
(:java-member-definitions
:java-class-definitions)
[(-> element
(assoc :name-row 0
:name-col 0
:name-end-row 0
:name-end-col 0))]
[element]))
(defn ^:private valid-element?
[{:keys [name bucket name-row name-col name-end-row name-end-col
derived-name-location derived-location]}]
(and name-row
name-col
name-end-row
name-end-col
(not derived-name-location)
(not derived-location)
;; #1510
(or (not (identical? :var-definitions bucket))
name)))
(defn ^:private normalize-analysis [external? analysis]
(reduce-kv
(fn [result kondo-bucket kondo-elements]
(transduce
(comp
(mapcat #(element->normalized-elements (assoc % :bucket kondo-bucket :external? external?)))
(filter valid-element?))
(completing
(fn [result {:keys [uri bucket] :as element}]
;; intentionally use element bucket, since it may have been
;; normalized to something besides kondo-bucket
(update-in result [uri bucket] (fnil conj []) element)))
result
kondo-elements))
;; TODO: Can result be a transient?
{}
analysis))
(defn ^:private normalize
"Put kondo result in a standard format, with `analysis` normalized and
`analysis` and `findings` indexed by uri."
[{:keys [analysis findings] :as kondo-results}
{:keys [external? ensure-uris filter-analysis] :or {filter-analysis identity}}
db]
(let [filename->uri (memoize #(shared/filename->uri % db))
trade-filename-for-uri (fn [obj]
(-> obj
(assoc :uri (or (some-> (:uri obj) shared/conform-uri-scheme)
(filename->uri (:filename obj))))
(dissoc :filename)))
with-uris (fn [uris default coll]
(merge (zipmap uris (repeat default)) coll))
analysis (->> analysis
filter-analysis
(medley/map-vals #(map trade-filename-for-uri %))
(normalize-analysis external?)
(with-uris ensure-uris {}))
findings (->> findings
(map trade-filename-for-uri)
(group-by :uri)
(with-uris (keys analysis) []))]
(assoc kondo-results
:external? external?
:analysis analysis
:findings findings)))
(defn ^:private normalize-for-file
"Normalize kondo result for a single file."
[kondo-results db filename uri]
(let [external? (shared/external-filename? filename (settings/get db [:source-paths]))
normalization-config {:external? external?
:ensure-uris [uri]}]
(normalize kondo-results normalization-config db)))
(defn ^:private with-additional-config
[config settings]
(cond-> config
(get-in settings [:linters :clj-kondo :report-duplicates] true)
(->
(assoc-in [:config :linters :unresolved-symbol :report-duplicates] true)
(assoc-in [:config :linters :unresolved-namespace :report-duplicates] true)
(assoc-in [:config :linters :unresolved-var :report-duplicates] true))))
(defn ^:private run-custom-lint? [config]
(not= :off (get-in config [:linters :clojure-lsp/unused-public-var :level])))
(defn ^:private custom-lint-project!
[db {:keys [config] :as kondo-ctx} normalization-config]
(when (run-custom-lint? config)
(shared/logging-time
"Linting whole project for unused-public-var took %s"
(let [db (db-with-analysis db (normalize kondo-ctx normalization-config db))]
(f.diagnostic/custom-lint-project! db kondo-ctx)))))
(defn ^:private custom-lint-files!
[uris db {:keys [config] :as kondo-ctx} normalization-config]
(when (run-custom-lint? config)
(shared/logging-task
:reference-files/lint
(let [db (db-with-analysis db (normalize kondo-ctx normalization-config db))]
(f.diagnostic/custom-lint-uris! uris db kondo-ctx)))))
(defn ^:private custom-lint-file!
[filename uri db {:keys [config] :as kondo-ctx}]
(when (run-custom-lint? config)
(let [db (db-with-analysis db (normalize-for-file kondo-ctx db filename uri))]
(f.diagnostic/custom-lint-uris! [uri] db kondo-ctx))))
(defn ^:private ignore-path? [db path]
(let [paths-ignore-regex (get-in db [:settings :paths-ignore-regex] [])]
(some #(re-matches (re-pattern %) (fs/unixify path)) paths-ignore-regex)))
(def ^:private config-for-shallow-analysis
{:arglists true
:keywords true
:protocol-impls true
:java-class-definitions true
:java-member-definitions true
:var-usages false
:var-definitions {:shallow true
:meta [:arglists :style/indent]}})
(def ^:private config-for-full-analysis
{:arglists true
:locals true
:keywords true
:protocol-impls true
:java-class-definitions true
:java-member-definitions true
:instance-invocations true
:java-class-usages true
:context [:clojure.test
:re-frame.core]
:var-definitions {:meta [:arglists :style/indent]}
:symbols true})
(defn ^:private config-for-paths [paths file-analyzed-fn db]
(-> {:cache true
:parallel true
:config-dir (some-> db :project-root-uri project-config-dir)
:copy-configs (settings/get db [:copy-kondo-configs?] true)
:lint [(->> paths
(remove (partial ignore-path? db))
(string/join (System/getProperty "path.separator")))]
:config {:output {:canonical-paths true}}
:file-analyzed-fn file-analyzed-fn}
(with-additional-config (settings/all db))))
(defn ^:private config-for-internal-paths [paths db custom-lint-fn file-analyzed-fn]
(-> (config-for-paths paths file-analyzed-fn db)
(assoc :custom-lint-fn custom-lint-fn)
(assoc-in [:config :analysis] config-for-full-analysis)))
(defn ^:private config-for-external-paths [paths db file-analyzed-fn]
(let [full-analysis? (= :project-and-full-dependencies (:project-analysis-type db))]
(-> (config-for-paths paths file-analyzed-fn db)
(assoc :skip-lint true)
(assoc-in [:config :analysis] config-for-shallow-analysis)
(shared/assoc-in-some [:config :analysis :java-member-definitions] full-analysis?)
(shared/assoc-in-some [:config :analysis :java-class-definitions] full-analysis?))))
(defn ^:private config-for-copy-configs [paths db]
{:cache true
:parallel true
:skip-lint true
:config-dir (some-> db :project-root-uri project-config-dir)
:copy-configs (settings/get db [:copy-kondo-configs?] true)
:lint [(string/join (System/getProperty "path.separator") paths)]
:config {:output {:canonical-paths true}}})
(defn ^:private config-for-jdk-source [paths db]
{:lint paths
:config-dir (some-> db :project-root-uri project-config-dir)
:config {:output {:canonical-paths true}
:analysis {:java-class-definitions true
:java-member-definitions true}}})
(defn ^:private config-for-single-file [uri db*]
(let [db @db*
filename (shared/uri->filename uri)
custom-lint-fn #(custom-lint-file! filename uri @db* %)
lang (shared/uri->file-type uri)]
(-> {:cache true
:lint ["-"]
:copy-configs (settings/get db [:copy-kondo-configs?] true)
:filename filename
:config-dir (some-> db :project-root-uri project-config-dir)
:custom-lint-fn custom-lint-fn
:config {:output {:canonical-paths true}
:analysis config-for-full-analysis}}
(shared/assoc-in-some [:lang] (when (not= :unknown lang)
lang))
(with-additional-config (settings/all db)))))
(defn ^:private run-kondo! [config err-hint]
(let [err-writer (java.io.StringWriter.)]
(try
(let [result (binding [*err* err-writer]
(kondo/run! config))]
(when-not (string/blank? (str err-writer))
(logger/warn "Non-fatal error from clj-kondo:" (str err-writer)))
result)
(catch Exception e
(logger/error e "Error running clj-kondo on" err-hint)))))
(defn run-kondo-on-paths! [paths db* {:keys [external?] :as normalization-config} file-analyzed-fn]
(let [db @db*
config (if external?
(config-for-external-paths paths db file-analyzed-fn)
(config-for-internal-paths paths db
#(custom-lint-project! @db* % normalization-config)
file-analyzed-fn))]
(-> config
(run-kondo! (str "paths " (string/join ", " paths)))
(normalize normalization-config db))))
(defn run-kondo-on-paths-batch!
"Run kondo on paths by partitioning the paths, with this we should call
kondo more times but with fewer paths to analyze, improving memory."
[paths normalization-config file-analyzed-fn db*]
(let [total (count paths)
batches (->> paths
(partition-all clj-kondo-analysis-batch-size)
(map-indexed (fn [index batch-paths]
{:index (inc index)
:paths batch-paths})))
batch-count (count batches)]
(logger/info
(case batch-count
0 "No new paths to analyze"
1 (str "Analyzing " total " paths with clj-kondo")
(str "Analyzing " total " paths with clj-kondo in " batch-count " batches...")))
(case batch-count
0 {}
1 (run-kondo-on-paths! paths db* normalization-config (partial file-analyzed-fn 1 1))
(->> batches
(map (fn [{:keys [index paths]}]
(logger/info "Analyzing batch" (str index "/" batch-count))
(run-kondo-on-paths! paths db* normalization-config (partial file-analyzed-fn index batch-count))))
(reduce shared/deep-merge)))))
(defn run-kondo-on-reference-uris! [uris db*]
(let [db @db*
filenames (map shared/uri->filename uris)
normalization-config {:external? false
:ensure-uris uris}
custom-lint-fn #(custom-lint-files! uris @db* % normalization-config)]
(-> (config-for-internal-paths filenames db custom-lint-fn nil)
(run-kondo! (str "files " (string/join ", " uris)))
(normalize normalization-config db))))
(defn run-kondo-on-text! [text uri db*]
(let [filename (shared/uri->filename uri)
ignore-filename? (ignore-path? @db* filename)
db @db*]
(if ignore-filename?
{}
(with-in-str text
(-> (config-for-single-file uri db*)
(run-kondo! filename)
(normalize-for-file db filename uri))))))
(defn run-kondo-copy-configs! [paths db]
(-> (config-for-copy-configs paths db)
(run-kondo! (str "paths " (string/join ", " paths)))))
(defn run-kondo-on-jdk-source! [paths db]
(let [normalization-config {:external? true
:filter-analysis #(select-keys % [:java-class-definitions
:java-member-definitions])}]
(-> (config-for-jdk-source paths db)
(run-kondo! (str "paths " paths))
(normalize normalization-config db))))