Skip to content

Commit 0b3e68f

Browse files
authored
rename-file-or-dir: rename more kinds of constructs in dependent namespaces (#365)
* `rename-file-or-dir`: rename more kinds of constructs in dependent namespaces Fixes #173
1 parent 12ae90a commit 0b3e68f

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
* [#173](https://github.com/clojure-emacs/refactor-nrepl/issues/173): `rename-file-or-dir`: rename more kinds of constructs in dependent namespaces: namespace-qualified maps, fully-qualified functions, metadata.
6+
57
## 3.3.1
68

79
* [#363](https://github.com/clojure-emacs/refactor-nrepl/issues/363): Fix a memoization bug in `clean-namespace`.

src/refactor_nrepl/rename_file_or_dir.clj

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
(let [dialect (:source-dialect parsed-ns)
9090
libspecs (-> parsed-ns dialect :require)
9191
require-macros (-> parsed-ns :cljs :require-macros)
92-
classes (-> parsed-ns dialect :import)]
92+
classes (-> parsed-ns dialect :import)]
9393
(merge parsed-ns
9494
{dialect
9595
(merge {:require (update-libspecs libspecs old-ns new-ns)
@@ -108,16 +108,18 @@
108108
(defn- update-file-content-sans-ns
109109
"Any fully qualified references to old-ns has to be replaced with new-ns."
110110
[file old-ns new-ns]
111-
(let [old-prefix (str (str/replace old-ns "-" "_") "/")
112-
new-prefix (str (str/replace new-ns "-" "_") "/")
113-
old-ns-ref (str old-ns "/")
114-
new-ns-ref (str new-ns "/")]
115-
(-> file
116-
slurp
117-
core/file-content-sans-ns
118-
str/triml
119-
(str/replace old-prefix new-prefix)
120-
(str/replace old-ns-ref new-ns-ref))))
111+
(let [replacements {(str/replace old-ns "-" "_") (str/replace new-ns "-" "_")
112+
(str old-ns "/") (str new-ns "/")
113+
(str "#:" old-ns "{") (str "#:" new-ns "{")
114+
(str "#:" old-ns " {") (str "#:" new-ns " {")}
115+
content (-> file
116+
slurp
117+
core/file-content-sans-ns
118+
str/triml)]
119+
(reduce (fn [s [old new]]
120+
(str/replace s old new))
121+
content
122+
replacements)))
121123

122124
(defn- update-dependent
123125
"New content for a dependent file."

test/refactor_nrepl/rename_file_or_dir_test.clj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns refactor-nrepl.rename-file-or-dir-test
22
(:require
3-
[clojure.test :refer [deftest is]]
3+
[clojure.string :as string]
4+
[clojure.test :refer [deftest is testing]]
45
[refactor-nrepl.core :refer [get-ns-component ns-form-from-string]]
56
[refactor-nrepl.rename-file-or-dir :as sut]
67
[refactor-nrepl.unreadable-files :refer [ignore-errors?]])
@@ -48,7 +49,12 @@
4849
imported-ns (-> import-form second first)]]
4950
(is (= 'com.move.moved-ns required-ns))
5051
(when imported-ns
51-
(is (= 'com.move.moved_ns imported-ns)))))))
52+
(is (= 'com.move.moved_ns imported-ns)))
53+
(testing content
54+
(is (not (string/includes? content "ns-to-be-moved"))
55+
"Renames various types of constructs (metadata, ns-qualified maps, etc)")
56+
(is (not (string/includes? content "ns_to_be_moved"))
57+
"Renames emitted class references"))))))
5258

5359
(deftest replaces-fully-qualified-vars-in-dependents
5460
(let [dependents (atom [])]

testproject/src/com/move/dependent_ns1.clj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
TypeToBeMoved RecordToBeMoved]))
66

77
(defn- use-some-publics []
8+
9+
(com.move.ns-to-be-moved/fn-to-be-moved :_)
10+
11+
'com.move.ns-to-be-moved/fn-to-be-moved
12+
13+
#'com.move.ns-to-be-moved/fn-to-be-moved
14+
15+
^:com.move.ns-to-be-moved/fn-to-be-moved []
16+
17+
#:com.move.ns-to-be-moved {:a :b}
18+
19+
(let [^com.move.ns_to_be_moved.TypeToBeMoved x (fn-to-be-moved :_)]
20+
x)
21+
22+
(com.move.ns_to_be_moved.TypeToBeMoved. :ok)
23+
824
(macro-to-be-moved
925
(fn-to-be-moved (TypeToBeMoved. :ok))
1026
(fn-to-be-moved (RecordToBeMoved. :ok))))

0 commit comments

Comments
 (0)