Skip to content

Commit 5a0e83c

Browse files
committed
Use cljr--resolve-alias to double check before inserting require
Attempted to add tests using `cljr--in-namespace-declaration`, but that method appears to always return nil, and isn't actually working. Left that be on the other non `cljr-magic-require-prompts-include-context` path to avoid changes in behavior. Given current behavior this test is not strictly necessary on the prompting path, as candidates with existing references should already be filtered out. Leaving it for now to keep the behavior close to before. Also, there is the possibility that the `cljr--magic-prompt-or-select-namespace` could allow users to type in a custom libspec at the prompt, in which case we would still need to filter to ensure it was not a duplicate. Added some test coverage to `cljr--resolve-alias`.
1 parent 779574d commit 5a0e83c

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

clj-refactor.el

+2-4
Original file line numberDiff line numberDiff line change
@@ -2149,10 +2149,8 @@ will add the corresponding require statement to the ns form."
21492149
(if cljr-magic-require-prompts-includes-context
21502150
(when-let (selection (cljr--magic-prompt-or-select-namespace
21512151
(cljr--magic-require-candidates alias-ref)))
2152-
(let ((alias (symbol-name (cl-first selection)))
2153-
(namespace (cl-second selection)))
2154-
(when (and (not (cljr--in-namespace-declaration-p (concat ":as " alias "\b")))
2155-
(not (cljr--in-namespace-declaration-p (concat ":as-alias " alias "\b"))))
2152+
(cl-destructuring-bind (alias namespace _) selection
2153+
(unless (cljr--resolve-alias (symbol-name alias))
21562154
(cljr--insert-require-libspec (format "[%s :as %s]" namespace alias)))))
21572155
(when-let (aliases (cljr--magic-requires-lookup-alias alias-ref))
21582156
(let ((short (cl-first aliases))

tests/unit-test.el

+18
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,24 @@
174174
(expect (cljr--magic-prompt-or-select-namespace '((a b.a (:clj))))
175175
:to-equal '(a b.a (:clj))))))
176176

177+
(defun cljr--test-resolve (alias)
178+
(cljr--with-clojure-temp-file "foo.clj"
179+
(insert "(ns foo (:require [a.a :as a] [a.b :as-alias b] [a.b.c :as b.c]))")
180+
(cljr--resolve-alias alias)))
181+
182+
(describe "cljr--resolve-alias"
183+
(it "returns nil on no matching alias"
184+
(expect (cljr--test-resolve "missing") :to-be nil))
185+
186+
(it "finds alias :as `a'"
187+
(expect (cljr--test-resolve "a") :to-equal "a.a"))
188+
189+
(it "finds alias :as-alias `b'"
190+
(expect (cljr--test-resolve "b") :to-equal "a.b"))
191+
192+
(it "finds dotted alias `b.c'"
193+
(expect (cljr--test-resolve "b.c") :to-equal "a.b.c")))
194+
177195
(describe "cljr-slash"
178196
(describe "with prompts including context"
179197
(before-each (setq cljr-magic-require-prompts-includes-context t))

0 commit comments

Comments
 (0)