Skip to content

Commit

Permalink
Handle symbol and string syntaxes
Browse files Browse the repository at this point in the history
  • Loading branch information
vemv committed Feb 6, 2022
1 parent 933f5c6 commit c3c8c08
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@
clojure.tools.namespace.track tracker}}
:unresolved-symbol {:exclude [(refactor-nrepl.ns.ns-parser/with-libspecs-from [libspecs])
(refactor-nrepl.middleware/set-descriptor! [set-descriptor!])]}
:unresolved-namespace {:exclude [clojure.main]}}}
:unresolved-namespace {:exclude [clojure.main]}
;; for integration tests:
:unused-namespace {:exclude [sample.unused.namespace
"more.unused.namespaces*"]}}}
4 changes: 3 additions & 1 deletion src/refactor_nrepl/ns/libspec_whitelist.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
(throw e)))))]
(->> exclude
(mapv (fn [entry]
(re-pattern (str "^" (Pattern/quote (str entry)) "$"))))
(if (symbol? entry)
(str "^" (Pattern/quote (str entry)) "$")
entry)))
(into (:libspec-whitelist config/*config*)))))

(def ^:private ^:dynamic *libspec-whitelist* nil)
Expand Down
10 changes: 6 additions & 4 deletions src/refactor_nrepl/ns/prune_dependencies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@
;; Some namespaces, e.g. those containing only protocol extensions,
;; are side-effecting at load but might look unused and otherwise be
;; pruned.
(defn- libspec-should-never-be-pruned? [libspec]
(defn libspec-should-never-be-pruned?
"Should `libspec` never be pruned away by the `clean-ns` op?"
[libspec]
(let [ns-name (str (:ns libspec))]
(some (fn [^String pattern]
(re-find (re-pattern pattern) ns-name))
(libspec-whitelist/libspec-whitelist))))
(boolean (some (fn [^String pattern]
(-> pattern re-pattern (re-find ns-name)))
(libspec-whitelist/libspec-whitelist)))))

(defn- prune-libspec [symbols-in-file current-ns libspec]
(if (libspec-should-never-be-pruned? libspec)
Expand Down
32 changes: 32 additions & 0 deletions test/refactor_nrepl/ns/libspec_whitelist_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns refactor-nrepl.ns.libspec-whitelist-test
(:require
[clojure.test :refer [are deftest is testing]]
[refactor-nrepl.ns.libspec-whitelist :as sut]
[refactor-nrepl.ns.prune-dependencies :as prune-dependencies]))

(deftest libspec-whitelist
(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-whitelist)))

(is (every? string? (sut/libspec-whitelist))
"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))))

0 comments on commit c3c8c08

Please sign in to comment.