Skip to content

Commit

Permalink
Use cljr--resolve-alias to double check before inserting require
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
dgtized committed Jul 11, 2022
1 parent 11f6969 commit 949b681
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 2 additions & 4 deletions clj-refactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -2149,10 +2149,8 @@ will add the corresponding require statement to the ns form."
(if cljr-magic-require-prompts-includes-context
(when-let (selection (cljr--magic-prompt-or-select-namespace
(cljr--magic-require-candidates alias-ref)))
(let ((alias (symbol-name (cl-first selection)))
(namespace (cl-second selection)))
(when (and (not (cljr--in-namespace-declaration-p (concat ":as " alias "\b")))
(not (cljr--in-namespace-declaration-p (concat ":as-alias " alias "\b"))))
(cl-destructuring-bind (alias namespace _) selection
(unless (cljr--resolve-alias (symbol-name alias))
(cljr--insert-require-libspec (format "[%s :as %s]" namespace alias)))))
(when-let (aliases (cljr--magic-requires-lookup-alias alias-ref))
(let ((short (cl-first aliases))
Expand Down
18 changes: 18 additions & 0 deletions tests/unit-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@
(expect (cljr--magic-prompt-or-select-namespace '((a b.a (:clj))))
:to-equal '(a b.a (:clj))))))

(defun cljr--test-resolve (alias)
(cljr--with-clojure-temp-file "foo.clj"
(insert "(ns foo (:require [a.a :as a] [a.b :as-alias b] [a.b.c :as b.c]))")
(cljr--resolve-alias alias)))

(describe "cljr--resolve-alias"
(it "returns nil on no matching alias"
(expect (cljr--test-resolve "missing") :to-be nil))

(it "finds alias :as `a'"
(expect (cljr--test-resolve "a") :to-equal "a.a"))

(it "finds alias :as-alias `b'"
(expect (cljr--test-resolve "b") :to-equal "a.b"))

(it "finds dotted alias `b.c'"
(expect (cljr--test-resolve "b.c") :to-equal "a.b.c")))

(describe "cljr-slash"
(describe "with prompts including context"
(before-each (setq cljr-magic-require-prompts-includes-context t))
Expand Down

0 comments on commit 949b681

Please sign in to comment.