From 4f576117477cf832b682d1b8dca3f4779693cfe0 Mon Sep 17 00:00:00 2001 From: vemv Date: Tue, 8 Feb 2022 10:13:15 +0100 Subject: [PATCH 1/4] Use refactor-nrepl 3.3.1 https://github.com/clojure-emacs/refactor-nrepl/blob/bc7779fc0d25ec025486438ad0f5b0990644a742/CHANGELOG.md#330 --- project.clj | 2 +- src/formatting_stack/formatters/clean_ns.clj | 55 ++++++++++++------- .../formatting_stack/formatters/clean_ns.clj | 14 +++++ 3 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 test/unit/formatting_stack/formatters/clean_ns.clj diff --git a/project.clj b/project.clj index 90f8231..afab139 100644 --- a/project.clj +++ b/project.clj @@ -128,7 +128,7 @@ :resource-paths ["test-resources-extra" "test-resources"]} - :refactor-nrepl {:dependencies [[refactor-nrepl "3.1.0"] + :refactor-nrepl {:dependencies [[refactor-nrepl "3.3.1"] [nrepl "0.9.0"]] ;; cider-nrepl is a :provided dependency from refactor-nrepl. :plugins [[cider/cider-nrepl "0.27.4" :exclusions [nrepl]]]} diff --git a/src/formatting_stack/formatters/clean_ns.clj b/src/formatting_stack/formatters/clean_ns.clj index 1e70bea..812ebc2 100644 --- a/src/formatting_stack/formatters/clean_ns.clj +++ b/src/formatting_stack/formatters/clean_ns.clj @@ -64,32 +64,45 @@ (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] + `(do + (require 'refactor-nrepl.ns.libspec-allowlist) + ((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 + (->> 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 diff --git a/test/unit/formatting_stack/formatters/clean_ns.clj b/test/unit/formatting_stack/formatters/clean_ns.clj new file mode 100644 index 0000000..f222bba --- /dev/null +++ b/test/unit/formatting_stack/formatters/clean_ns.clj @@ -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?))))))) From 1935ae431cdfe314c7aff139a56519b82d4420ed Mon Sep 17 00:00:00 2001 From: vemv Date: Thu, 10 Feb 2022 14:20:20 +0100 Subject: [PATCH 2/4] Bump refactor-nrepl again --- project.clj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project.clj b/project.clj index afab139..905e33a 100644 --- a/project.clj +++ b/project.clj @@ -128,10 +128,10 @@ :resource-paths ["test-resources-extra" "test-resources"]} - :refactor-nrepl {:dependencies [[refactor-nrepl "3.3.1"] + :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 [] From 59556a1a9087e022b72318d323919afcd46a4875 Mon Sep 17 00:00:00 2001 From: vemv Date: Thu, 10 Feb 2022 14:20:37 +0100 Subject: [PATCH 3/4] Bump Eastwood --- project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.clj b/project.clj index 905e33a..5caa067 100644 --- a/project.clj +++ b/project.clj @@ -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" From 7d3fd0bfeb17c50f9c38cfb1e07065034936d88e Mon Sep 17 00:00:00 2001 From: vemv Date: Thu, 10 Feb 2022 14:22:00 +0100 Subject: [PATCH 4/4] Prefer `requiring-resolve` --- src/formatting_stack/formatters/clean_ns.clj | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/formatting_stack/formatters/clean_ns.clj b/src/formatting_stack/formatters/clean_ns.clj index 812ebc2..f9ca6af 100644 --- a/src/formatting_stack/formatters/clean_ns.clj +++ b/src/formatting_stack/formatters/clean_ns.clj @@ -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 @@ -69,11 +66,9 @@ while respecting formatting-stack's need of lazily requiring refactor-nrepl." {:style/indent 0} [& body] - `(do - (require 'refactor-nrepl.ns.libspec-allowlist) - ((resolve 'refactor-nrepl.ns.libspec-allowlist/with-memoized-libspec-allowlist*) - (fn [] - ~@body)))) + `((requiring-resolve 'refactor-nrepl.ns.libspec-allowlist/with-memoized-libspec-allowlist*) + (fn [] + ~@body))) (defn format! [this files]