Skip to content

Don't suggest alias fragments with leading number #385

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

Merged
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 @@ -2,6 +2,8 @@

## Unreleased

* [#385](https://github.com/clojure-emacs/refactor-nrepl/pull/385): Fix cljr-refactor.el's `cljr-slash` when namespace fragments start with a number.

## 3.5.4

* [#383](https://github.com/clojure-emacs/refactor-nrepl/issues/383): `prune-dependencies`: increase accuracy for `:cljs`.
Expand Down
17 changes: 14 additions & 3 deletions src/refactor_nrepl/ns/suggest_aliases.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns refactor-nrepl.ns.suggest-aliases
"Suggestion of aliases based on these guidelines: https://stuartsierra.com/2015/05/10/clojure-namespace-aliases"
(:require
[clojure.edn :as edn]
[clojure.string :as string]))

(defn test-like-ns-name? [ns-sym]
Expand All @@ -12,6 +13,14 @@
(string/starts-with? last-fragment "test-")
(some #{"test" "unit" "integration" "acceptance" "functional" "generative"} fragments)))))))

(def readable-as-symbol?
(memoize
(fn [s]
(try
(symbol? (edn/read-string s))
(catch Exception _
false)))))

(defn suggested-aliases [namespace-name]
(let [fragments (-> namespace-name str (string/split #"\."))
fragments (into []
Expand All @@ -29,8 +38,10 @@
(range 1 (inc (count fragments)))
(repeat (distinct fragments)))
v (into {}
(map (fn [segments]
[(->> segments (string/join ".") (symbol)),
[namespace-name]]))
(keep (fn [segments]
(let [candidate (->> segments (string/join "."))]
(when (readable-as-symbol? candidate)
[(symbol candidate),
[namespace-name]]))))
fragments)]
(dissoc v namespace-name)))
24 changes: 23 additions & 1 deletion test/refactor_nrepl/ns/suggest_aliases_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@
'unit.foo true
'foo.unit true))

(deftest readable-as-symbol?
(testing "is readable as symbol"
(are [input] (sut/readable-as-symbol? input)
"test"
"a.test"
"a-test"
"b.a-test"
"ok.5<-here"
"this:too!#$%&*"))

(testing "not readable as symbol"
(are [input] (not (sut/readable-as-symbol? input))
":kw"
"15"
"5-bad"
"6:nope"
";what"
"#yeah")))

(deftest suggested-aliases
(are [desc input expected] (testing input
(is (= expected
Expand All @@ -46,4 +65,7 @@
"Removes redundant bits such as `clj-` and `.core`"
'clj-a.b.c.core '{c [clj-a.b.c.core]
b.c [clj-a.b.c.core]
a.b.c [clj-a.b.c.core]}))
a.b.c [clj-a.b.c.core]}

"Removes fragments that start with a number (in this case: `9d`, `8b.c.9d`), since they aren't valid symbols"
'a.8b.c.9d '{c.9d [a.8b.c.9d]}))