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

Extend undef op to work on fully qualified symbols #670

Merged
merged 3 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### New Features

* [#670](https://github.com/clojure-emacs/cider-nrepl/pull/670): Extend undef op to work on fully qualified symbols.

## 0.24.0 (2020-02-14)

### Bugs fixed
Expand Down
23 changes: 19 additions & 4 deletions src/cider/nrepl/middleware/undef.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
(ns cider.nrepl.middleware.undef
"Undefine a symbol"
"Middleware for undefining symbols.
Fully qualified symbols are interpreted as a var to be unmapped in its
original namespace, whereas unqualified symbols are interpreted as both a var
and ns alias to be unmapped from the current namespace."
(:require
[cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
[orchard.misc :as misc]))

(defn undef
"Undefines a symbol.
When `symbol` is unqualified, it is interpreted as both an alias and var to be
unmapped from the namespace `ns`.
When qualified (eg. `foo/bar`), it is interpreted as a var to be unmapped in
the namespace `foo`, which may be an alias in `ns`."
[{:keys [ns symbol]}]
(let [[ns symbol] (map misc/as-sym [ns symbol])]
(ns-unalias ns symbol)
(ns-unmap ns symbol)
(let [ns (misc/as-sym ns)
[sym-ns sym-name] ((juxt (comp misc/as-sym namespace) misc/name-sym)
(misc/as-sym symbol))]
(if sym-ns
;; fully qualified => var in other namespace
(let [other-ns (get (ns-aliases ns) sym-ns sym-ns)]
(ns-unmap other-ns sym-name))
;; unqualified => alias or var in current ns
(do (ns-unalias ns sym-name)
(ns-unmap ns sym-name)))
symbol))

(defn undef-reply
Expand Down
38 changes: 37 additions & 1 deletion test/clj/cider/nrepl/middleware/undef_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,43 @@
:symbol "x"}))))
(is (= ["nil"]
(:value (session/message {:op "eval"
:code "(ns-resolve 'user 'x)"}))))))
:code "(ns-resolve 'user 'x)"})))))
(testing "undef undefines vars in other namespaces"
(is (= #{"done"}
(:status (session/message {:op "eval"
:code "(do (ns other.ns) (in-ns 'user) (require '[other.ns :as other]))"}))))
(is (= ["#'other.ns/x"]
(:value (session/message {:op "eval"
:code "(do (in-ns 'other.ns) (def x 1) (in-ns 'user) (ns-resolve 'other.ns 'x))"}))))
(is (= #{"done"}
(:status (session/message {:op "undef"
:ns "other.ns"
:symbol "x"}))))
(is (= ["nil"]
(:value (session/message {:op "eval"
:code "(ns-resolve 'other.ns 'x)"})))))
(testing "undef takes fully qualified symbols"
(is (= ["#'other.ns/x"]
(:value (session/message {:op "eval"
:code "(do (in-ns 'other.ns) (def x 1) (in-ns 'user) (ns-resolve 'other.ns 'x))"}))))
(is (= #{"done"}
(:status (session/message {:op "undef"
:ns "user"
:symbol "other.ns/x"}))))
(is (= ["nil"]
(:value (session/message {:op "eval"
:code "(ns-resolve 'other.ns 'x)"})))))
(testing "undef resolves namespace aliases in fully qualified symbols"
(is (= ["#'other.ns/x"]
(:value (session/message {:op "eval"
:code "(do (in-ns 'other.ns) (def x 1) (in-ns 'user) (ns-resolve 'other.ns 'x))"}))))
(is (= #{"done"}
(:status (session/message {:op "undef"
:ns "user"
:symbol "other/x"}))))
(is (= ["nil"]
(:value (session/message {:op "eval"
:code "(ns-resolve 'other.ns 'x)"}))))))

(deftest undef-alias-test
(testing "undef undefines aliases"
Expand Down