Skip to content

Commit 67573f7

Browse files
DerGuteMoritzvemv
authored andcommitted
Add always-return-ns-form option to clean-ns
When true, clean-ns will always return the ns form even if no structural changes were applied. This can be useful when the only changes would be in the pprint result (due to whitespace changes).
1 parent e3da5e4 commit 67573f7

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
* Add `always-return-ns-form` option to `clean-ns` message
6+
57
## 3.9.1
68

79
* `suggest-libspecs` op: support collections for `:only` values.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ Configuration settings are passed along with each msg, currently the recognized
100100
;; Should `clean-ns` favor prefix forms in the ns macro?
101101
:prefix-rewriting true
102102

103+
;; Should `clean-ns` always return the ns form even if there were no structural changes? Useful for when there would only have been whitespace changes from pretty-printing it again.
104+
:always-return-ns-form false
105+
103106
;; Should `pprint-ns` place a newline after the `:require` and `:import` tokens?
104107
:insert-newline-after-require true
105108

src/refactor_nrepl/ns/clean_ns.clj

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
ns-form)
3636

3737
(defn clean-ns
38-
"Returns nil if there's nothing to clean."
39-
[{:keys [path relative-path]}]
38+
"Returns nil if there's nothing to clean unless always-return-ns-form is true."
39+
[{:keys [path relative-path always-return-ns-form]}]
4040
(let [path (some (fn [p]
4141
(when (and p (.exists (io/file p)))
4242
p))
@@ -56,5 +56,6 @@
5656
new-ns-form (-> (ns-parser/parse-ns path)
5757
deps-preprocessor
5858
(rebuild-ns-form ns-form))]
59-
(when-not (= ns-form new-ns-form)
59+
(when (or always-return-ns-form
60+
(not= ns-form new-ns-form))
6061
new-ns-form)))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns ns-with-whitespace-changes-only
2+
3+
(:require [clojure.string :as str]
4+
[ clojure.walk :as walk]))
5+
6+
7+
8+
(defn foo [x]
9+
(walk/postwalk (comp vector str/trim str) x))

test/refactor_nrepl/ns/clean_ns_test.clj

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
(def ns-with-npm-strs (clean-msg "test-resources/ns_with_npm_strs.cljs"))
5959
(def ns-with-npm-strs-clean (clean-msg "test-resources/ns_with_npm_strs_clean.cljs"))
6060

61+
(def ns-with-whitespace-changes-only (clean-msg "test-resources/ns_with_whitespace_changes_only.clj"))
62+
6163
(deftest combines-requires
6264
(let [prefix-requires (config/with-config {:prefix-rewriting true}
6365
(core/get-ns-component (clean-ns ns2) :require))
@@ -269,6 +271,14 @@
269271
(is (= (pprint-ns (clean-ns ns-with-npm-strs))
270272
(pprint-ns (read-string (slurp (:path ns-with-npm-strs-clean)))))))
271273

274+
(deftest whitespace-only-changes-are-ignored-by-default
275+
(is (nil? (clean-ns ns-with-whitespace-changes-only))))
276+
277+
(deftest whitespace-only-changes-are-considered-when-always-return-ns-form-option-is-true
278+
(is (= (read-string (slurp (:path ns-with-whitespace-changes-only)))
279+
(clean-ns (assoc ns-with-whitespace-changes-only
280+
:always-return-ns-form true)))))
281+
272282
(core/with-clojure-version->= {:major 1 :minor 11}
273283
(deftest as-alias
274284
(is (= '(ns as-alias (:require [foo :as-alias f]))

0 commit comments

Comments
 (0)