Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor clj-kondo's namespace local configuration #388

Merged
merged 10 commits into from
Oct 17, 2022
43 changes: 26 additions & 17 deletions src/refactor_nrepl/ns/libspec_allowlist.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@
(clojure.lang IFn)
(java.util.regex Pattern)))

(defn- libspec-allowlist* []
(let [kondo-file (io/file ".clj-kondo" "config.edn")
exclude (when (.exists kondo-file)
(try
(-> kondo-file slurp read-string :linters :unused-namespace :exclude)
(catch Exception e
(when (System/getenv "CI")
(throw e)))))]
(->> exclude
(mapv (fn [entry]
(if (symbol? entry)
(str "^" (Pattern/quote (str entry)) "$")
entry)))
(into (:libspec-whitelist config/*config*)))))
(defn- kondo-excludes [{:keys [ns meta]}]
OknoLombarda marked this conversation as resolved.
Show resolved Hide resolved
(let [local-config (:clj-kondo/config meta)
local-config (if (and (seq? local-config) (= 'quote (first local-config)))
(second local-config)
local-config)
kondo-file (io/file ".clj-kondo" "config.edn")
config (when (.exists kondo-file)
(try
(-> kondo-file slurp read-string)
(catch Exception e
(when (System/getenv "CI")
(throw e)))))]
(concat (get-in config [:linters :unused-namespace :exclude])
OknoLombarda marked this conversation as resolved.
Show resolved Hide resolved
OknoLombarda marked this conversation as resolved.
Show resolved Hide resolved
(get-in config [:config-in-ns ns :linters :unused-namespace :exclude])
(get-in local-config [:linters :unused-namespace :exclude]))))

(defn- libspec-allowlist* [current-ns]
(->> (kondo-excludes current-ns)
(mapv (fn [entry]
(if (symbol? entry)
(str "^" (Pattern/quote (str entry)) "$")
entry)))
(into (:libspec-whitelist config/*config*))))

(def ^:private ^:dynamic ^IFn *libspec-allowlist* nil)

Expand All @@ -32,9 +41,9 @@
with clj-kondo's `:unused-namespace` config.

Uses a memoized version if available."
[]
(or (some-> *libspec-allowlist* .invoke)
(libspec-allowlist*)))
[current-ns]
(or (some-> *libspec-allowlist* (.invoke current-ns))
(libspec-allowlist* current-ns)))

(defmacro with-memoized-libspec-allowlist
"Memoizes the libspec-allowlist internals while `body` is executing.
Expand Down
15 changes: 9 additions & 6 deletions src/refactor_nrepl/ns/ns_parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,15 @@
(parse-clj-or-cljs-ns path :cljs)))

(defn parse-ns [path-or-file]
(assoc
(if (core/cljc-file? (io/file path-or-file))
(parse-cljc-ns path-or-file)
(parse-clj-or-cljs-ns path-or-file))
:ns (second (core/read-ns-form-with-meta path-or-file))
:source-dialect (core/file->dialect path-or-file)))
(let [[_ ns & args] (core/read-ns-form-with-meta path-or-file)]
vemv marked this conversation as resolved.
Show resolved Hide resolved
(assoc
(if (core/cljc-file? (io/file path-or-file))
(parse-cljc-ns path-or-file)
(parse-clj-or-cljs-ns path-or-file))
:ns ns
;; Second element can also be a docstring or reference.
:meta (->> args (take 2) (some #(if (map? %) % nil)))
OknoLombarda marked this conversation as resolved.
Show resolved Hide resolved
:source-dialect (core/file->dialect path-or-file))))

(def ^:dynamic *read-ns-form-with-meta* core/read-ns-form-with-meta)

Expand Down
6 changes: 3 additions & 3 deletions src/refactor_nrepl/ns/prune_dependencies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@
;; pruned.
(defn libspec-should-never-be-pruned?
"Should `libspec` never be pruned away by the `clean-ns` op?"
[libspec]
[current-ns libspec]
(let [ns-name (str (:ns libspec))]
(boolean (some (fn [^String pattern]
(-> pattern re-pattern (re-find ns-name)))
(libspec-allowlist/libspec-allowlist)))))
(libspec-allowlist/libspec-allowlist current-ns)))))

(defn imports->namespaces
"Given a collection of `:import` clauses, returns the set of namespaces denoted by them, as symbols.
Expand Down Expand Up @@ -185,7 +185,7 @@

(defn- prune-libspec [symbols-in-file current-ns imports-namespaces libspec]
(cond
(libspec-should-never-be-pruned? libspec)
(libspec-should-never-be-pruned? current-ns libspec)
libspec

(imports-contain-libspec? imports-namespaces (:ns libspec))
Expand Down