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

Use refactor-nrepl 3.3.2 #202

Merged
merged 4 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]}
Expand Down
55 changes: 34 additions & 21 deletions src/formatting_stack/formatters/clean_ns.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

What about requiring-resolve?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not available in every clojure version, so I tend not to use it in tooling projs

Copy link
Member

Choose a reason for hiding this comment

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

F-s ships with clojure 1.10.3, so I think you're safe 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

((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?)))))))