-
Notifications
You must be signed in to change notification settings - Fork 2
/
printer.clj
86 lines (76 loc) · 2.73 KB
/
printer.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
; This Source Code Form is subject to the terms of the Mozilla Public
; License, v. 2.0. If a copy of the MPL was not distributed with this
; file, You can obtain one at https://mozilla.org/MPL/2.0/.
(ns noahtheduke.splint.printer
(:require
[clojure.data.json :as json]
[clojure.pprint :as pp]))
(defn print-find-dispatch [output _diagnostic] output)
(defmulti print-find #'print-find-dispatch)
(defmethod print-find "full" [_ {:keys [filename rule-name form line column message alt]}]
(printf "%s:%s:%s [%s] - %s" filename line column rule-name message)
(newline)
(pp/pprint form)
(when alt
(println "Consider using:")
(pp/pprint alt))
(newline)
(flush))
(defmethod print-find "simple" [_ {:keys [filename rule-name line column message]}]
(printf "%s:%s:%s [%s] - %s" filename line column rule-name message)
(newline)
(flush))
(defmethod print-find "clj-kondo" [_ {:keys [filename line column message]}]
(printf "%s:%s:%s: warning: %s" filename line column message)
(newline)
(flush))
(defmethod print-find "markdown" [_ {:keys [filename rule-name form line column message alt]}]
(println "----")
(newline)
(printf "#### %s:%s:%s [%s]" filename line column rule-name)
(newline)
(newline)
(println message)
(newline)
(println "```clojure")
(pp/pprint form)
(println "```")
(newline)
(when alt
(println "Consider using:")
(newline)
(println "```clojure")
(pp/pprint alt)
(println "```")
(newline))
(flush))
(defmethod print-find "json" [_ diagnostic]
(let [diagnostic (-> diagnostic
(update :rule-name pr-str)
(update :form pr-str)
(update :alt pr-str))]
(json/write diagnostic *out* {:escape-slash false})
(newline)
(flush)))
(defmethod print-find "json-pretty" [_ diagnostic]
(let [diagnostic (-> diagnostic
(update :rule-name pr-str)
(update :form pr-str)
(update :alt pr-str))]
(json/pprint diagnostic {:escape-slash false})
(newline)
(flush)))
(defn print-results
[options diagnostics total-time]
(when-not (or (:quiet options) (:silent options))
(let [printer (get-method print-find (:output options))]
(doseq [diagnostic (sort-by :filename diagnostics)]
(printer nil diagnostic))))
(when-not (or (:silent options) (#{"markdown" "json" "json-pretty"} (:output options)))
(printf "Linting took %sms, %s style warnings%s\n"
total-time
(count (remove #(= 'splint/error (:rule-name %)) diagnostics))
(if-let [errors (seq (filter #(= 'splint/error (:rule-name %)) diagnostics))]
(format ", %s errors" (count errors))
""))
(flush)))