Skip to content

Commit

Permalink
[Fix 91] Unmap old nses syms after rename file/dir
Browse files Browse the repository at this point in the history
as symbols in the renamed ns still resolve to the old namespace
dependents' ASTs are rebuilt with the wrong ns information in them. Fix is
to explicitly unmap syms of the old ns when doing rename so when the new
ASTs are built the new namespaces will be used.
  • Loading branch information
benedekfazekas committed Oct 4, 2015
1 parent 3b0c502 commit 9bc3914
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/refactor_nrepl/rename_file_or_dir.clj
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,31 @@
(when (= dialect :cljs)
{:require-macros (update-libspecs require-macros old-ns new-ns)}))})))

(defn- create-new-ns-form
(defn- clean-var-entry
[ns-entry]
(-> (val ns-entry)
str
(str/replace "#'" "")))

(defn- clean-interned-syms! [ns-name old-ns]
(doseq [[sym _]
(->> (ns-aliases ns-name)
(filter #(= (str old-ns) (clean-var-entry %))))]
(ns-unalias ns-name sym))
(doseq [[sym _]
(->> (ns-refers ns-name)
(filter #(.startsWith (clean-var-entry %) (str old-ns))))]
(ns-unmap ns-name sym)))

(defn- create-new-ns-form!
"Reads file and returns an updated ns."
[file old-ns new-ns]
(let [ns-form (core/read-ns-form file)
ns-name (second ns-form)
parsed-ns (ns-parser/parse-ns file)
deps (update-references-in-deps parsed-ns old-ns new-ns)]
(when (find-ns ns-name)
(clean-interned-syms! ns-name old-ns))
(pprint-ns (rebuild-ns-form deps ns-form))))

(defn- update-file-content-sans-ns
Expand All @@ -121,7 +140,7 @@
(defn- update-dependent
"New content for a dependent file."
[file old-ns new-ns]
(str (create-new-ns-form file old-ns new-ns)
(str (create-new-ns-form! file old-ns new-ns)
"\n"
(update-file-content-sans-ns file old-ns new-ns)))

Expand Down

2 comments on commit 9bc3914

@expez
Copy link
Member

@expez expez commented on 9bc3914 Oct 4, 2015

Choose a reason for hiding this comment

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

Nice 👍

I think we might have to do something similar for cljs.

@benedekfazekas
Copy link
Member Author

@benedekfazekas benedekfazekas commented on 9bc3914 Oct 4, 2015 via email

Choose a reason for hiding this comment

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

Please sign in to comment.