Skip to content

Use refactor-nrepl 3.3.2 #202

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

Merged
merged 4 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(def eastwood-version "1.1.1")
(def eastwood-version "1.2.2")

;; Please don't bump the library version by hand - use ci.release-workflow instead.
(defproject formatting-stack "4.5.0"
Expand Down Expand Up @@ -128,10 +128,10 @@
:resource-paths ["test-resources-extra"
"test-resources"]}

:refactor-nrepl {:dependencies [[refactor-nrepl "3.1.0"]
:refactor-nrepl {:dependencies [[refactor-nrepl "3.3.2"]
[nrepl "0.9.0"]]
;; cider-nrepl is a :provided dependency from refactor-nrepl.
:plugins [[cider/cider-nrepl "0.27.4" :exclusions [nrepl]]]}
:plugins [[cider/cider-nrepl "0.28.2" :exclusions [nrepl]]]}

:ncrw {:global-vars {*assert* true} ;; `ci.release-workflow` relies on runtime assertions
:source-paths ^:replace []
Expand Down
60 changes: 34 additions & 26 deletions src/formatting_stack/formatters/clean_ns.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@

(def default-nrepl-config-opts
(delay

(require ;; lazy-loading in order to support unconfigured consumers
'[refactor-nrepl.config])

(-> 'refactor-nrepl.config/*config* resolve deref)))
;; lazy-loading in order to support unconfigured consumers
(-> 'refactor-nrepl.config/*config* requiring-resolve deref)))

(def default-nrepl-opts
(delay
Expand All @@ -64,32 +61,43 @@
(make-cleaner how-to-ns-opts refactor-nrepl-opts namespaces-that-should-never-cleaned libspec-whitelist filename)
how-to-ns-opts)))

(defmacro with-memoized-libspec-allowlist
"Uses refactor-nrepl's 'memoized libspec' facility for best performance,
while respecting formatting-stack's need of lazily requiring refactor-nrepl."
{:style/indent 0}
[& body]
`((requiring-resolve 'refactor-nrepl.ns.libspec-allowlist/with-memoized-libspec-allowlist*)
(fn []
~@body)))

(defn format!
[this files]
(->> files
(process-in-parallel! (fn [filename]
(when-let [ns-replacement (replaceable-ns-form this filename)]
(println "Cleaning unused imports:" filename)
(write-ns-replacement! filename ns-replacement)))))
(with-memoized-libspec-allowlist
Copy link
Contributor Author

@vemv vemv Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This macro invocation could be a strategy instead, added to the stack, but I prefered to leave something that would survive any possible refactorings (as we foresee that will happen wrt config, strategies and stuff) and clearly work no matter what.

Could be revisited later when f-s' new API becomes an actual thing

(->> files
(process-in-parallel! (bound-fn [filename]
(when-let [ns-replacement (replaceable-ns-form this filename)]
(println "Cleaning unused imports:" filename)
(write-ns-replacement! filename ns-replacement))))))
nil)

(defn lint! [this files]
(->> files
(process-in-parallel! (fn [filename]
(when-let [{:keys [final-ns-form-str
original-ns-form-str]} (replaceable-ns-form this filename)]
(let [diff (diff/unified-diff filename original-ns-form-str final-ns-form-str)]
(->> (diff->line-numbers diff)
(mapv (fn [{:keys [start]}]
{:filename filename
:diff diff
:level :warning
:column 0
:line start
:msg "ns can be cleaned"
:source :formatting-stack/clean-ns})))))))
(filter some?)
(mapcat ensure-sequential)))
(with-memoized-libspec-allowlist
(->> files
(process-in-parallel! (bound-fn [filename]
(when-let [{:keys [final-ns-form-str
original-ns-form-str]} (replaceable-ns-form this filename)]
(let [diff (diff/unified-diff filename original-ns-form-str final-ns-form-str)]
(->> (diff->line-numbers diff)
(mapv (fn [{:keys [start]}]
{:filename filename
:diff diff
:level :warning
:column 0
:line start
:msg "ns can be cleaned"
:source :formatting-stack/clean-ns})))))))
(filter some?)
(mapcat ensure-sequential))))

(defn new [{:keys [refactor-nrepl-opts libspec-whitelist how-to-ns-opts namespaces-that-should-never-cleaned]
:or {namespaces-that-should-never-cleaned default-namespaces-that-should-never-cleaned
Expand Down
14 changes: 14 additions & 0 deletions test/unit/formatting_stack/formatters/clean_ns.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns unit.formatting-stack.formatters.clean-ns
(:require
[clojure.test :refer [deftest is testing]]
[formatting-stack.formatters.clean-ns :as sut]
[formatting-stack.strategies :as strategies]))

(when (strategies/refactor-nrepl-available?)
(deftest with-memoized-libspec-allowlist
(testing "Binds `*libspec-allowlist*`, which means that memoization will be effectively used"
(let [bound? (fn []
@(resolve 'refactor-nrepl.ns.libspec-allowlist/*libspec-allowlist*))]
(assert (not (bound?)))
(is (sut/with-memoized-libspec-allowlist
(bound?)))))))