-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honor clj-kondo
:unused-namespace
config, if present (#361)
- Loading branch information
Showing
7 changed files
with
106 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
(ns refactor-nrepl.ns.libspec-allowlist | ||
(:require | ||
[clojure.java.io :as io] | ||
[refactor-nrepl.config :as config]) | ||
(:import | ||
(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*))))) | ||
|
||
(def ^:private ^:dynamic *libspec-allowlist* nil) | ||
|
||
(defn with-memoized-libspec-allowlist* [f] | ||
(binding [*libspec-allowlist* (memoize libspec-allowlist*)] | ||
(f))) | ||
|
||
(defn libspec-allowlist | ||
"Obtains a libspec allowlist, which is the result of merging clj-refactor's own `:libspec-whitelist` | ||
with clj-kondo's `:unused-namespace` config. | ||
Uses a memoized version if available." | ||
[] | ||
(or *libspec-allowlist* | ||
(libspec-allowlist*))) | ||
|
||
(defmacro with-memoized-libspec-allowlist | ||
"Memoizes the libspec-allowlist internals while `body` is executing. | ||
_Temporary_ memoization is important because: | ||
* one does want to reload clj-kondo config if the user changes it, without needing a JVM restart; | ||
* one doesn't want that to imply a performance hit in terms of repeatedly reading clj-kondo config files." | ||
{:style/indent 0} | ||
[& body] | ||
`(with-memoized-libspec-allowlist* | ||
(fn [] | ||
~@body))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(ns refactor-nrepl.ns.libspec-allowlist-test | ||
(:require | ||
[clojure.test :refer [are deftest is testing]] | ||
[refactor-nrepl.ns.libspec-allowlist :as sut] | ||
[refactor-nrepl.ns.prune-dependencies :as prune-dependencies])) | ||
|
||
(deftest libspec-allowlist | ||
(testing "Takes into account refactor-nrepls own config, and .clj-kondo/config files alike, | ||
merging their results" | ||
(is (= [;; From refactor-nrepl's default config: | ||
"^cljsjs" | ||
;; from our .clj-kondo file - symbols become quoted patterns: | ||
"^\\Qsample.unused.namespace\\E$" | ||
;; from our .clj-kondo file - strings have 'regex' semantics so are kept as-is: | ||
"more.unused.namespaces*"] | ||
|
||
(sut/libspec-allowlist))) | ||
|
||
(is (every? string? (sut/libspec-allowlist)) | ||
"Items coming from different sources all have the same class, | ||
ensuring they will be treated homogeneously by refactor-nrepl") | ||
|
||
(testing "`libspec-should-never-be-pruned?` is integrated with clj-kondo logic, | ||
effecively parsing its config into well-formed regexes" | ||
(are [input expected] (= expected | ||
(prune-dependencies/libspec-should-never-be-pruned? {:ns input})) | ||
'sample.unused.namespace true | ||
'Asample.unused.namespace false | ||
'sample.unused.namespaceB false | ||
'more.unused.namespaces true | ||
'more.unused.namespacessss true | ||
'more.unused.namespac false)))) |