2021-06-19 14:32:58 +00:00
|
|
|
(ns normalize-keywords
|
|
|
|
|
(:require [babashka.pods :as pods]
|
2022-11-16 16:01:39 +00:00
|
|
|
[clojure.java.io :as io]
|
2021-06-19 14:32:58 +00:00
|
|
|
[rewrite-clj.node :as node]
|
|
|
|
|
[rewrite-clj.zip :as z]))
|
|
|
|
|
|
2022-11-16 14:49:07 +00:00
|
|
|
(pods/load-pod 'clj-kondo/clj-kondo "2022.11.02")
|
2021-06-19 14:32:58 +00:00
|
|
|
|
|
|
|
|
(require '[pod.borkdude.clj-kondo :as clj-kondo])
|
|
|
|
|
|
|
|
|
|
(def code (first *command-line-args*))
|
|
|
|
|
|
2022-11-16 16:01:39 +00:00
|
|
|
(defn findings [file-path]
|
|
|
|
|
(->> (clj-kondo/run! {:lint [file-path]
|
|
|
|
|
:config {:output {:analysis {:keywords true}}}})
|
2021-06-19 14:32:58 +00:00
|
|
|
:analysis
|
|
|
|
|
:keywords
|
|
|
|
|
(filter (some-fn :alias :auto-resolved))))
|
|
|
|
|
|
|
|
|
|
(defn finding->keyword [{:keys [:ns :name]}]
|
|
|
|
|
(keyword (str ns) (str name)))
|
|
|
|
|
|
|
|
|
|
(defn remove-locs [zloc findings]
|
|
|
|
|
(loop [zloc zloc
|
|
|
|
|
findings (seq findings)]
|
|
|
|
|
(if findings
|
|
|
|
|
(let [{:keys [:row :col] :as finding} (first findings)
|
|
|
|
|
node (z/node zloc)
|
|
|
|
|
m (meta node)]
|
|
|
|
|
(if (and (= row (:row m))
|
|
|
|
|
(= col (:col m)))
|
|
|
|
|
(let [k (finding->keyword finding)
|
|
|
|
|
zloc (z/replace zloc (node/coerce k))]
|
|
|
|
|
(recur zloc (next findings)))
|
|
|
|
|
(recur (z/next zloc) findings)))
|
2022-11-16 16:01:39 +00:00
|
|
|
(str (z/root zloc)))))
|
2021-06-19 14:32:58 +00:00
|
|
|
|
2022-11-16 16:01:39 +00:00
|
|
|
(doseq [f (file-seq (io/file code))
|
2022-11-16 16:13:13 +00:00
|
|
|
:when (re-find #"\.clj[cdsx]?$" (str f))
|
2022-11-16 16:01:39 +00:00
|
|
|
:let [file-path (str f)]]
|
|
|
|
|
(when-let [findings' (findings file-path)]
|
|
|
|
|
(prn (format "Rewriting %s" file-path))
|
|
|
|
|
(spit f (remove-locs (z/of-file file-path) findings'))))
|