Skip to content

Commit 42d6ecd

Browse files
committed
Don't suggest alias fragments with leading number
Namespace aliases are symbols, and symbols begin with a non-numeric character, so we shouldn't suggest aliases that start with numbers. This fixes clj-refactor.el's cljr-slash for projects that contain ns'es like `db.migration.25`. Previously, it failed in parseclj when that tried to parse the nrepl response containing suggestions like `{25 [db.migration.25]}`, since `25` isn't a valid symbol.
1 parent e2eb88b commit 42d6ecd

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

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

79
* [#383](https://github.com/clojure-emacs/refactor-nrepl/issues/383): `prune-dependencies`: increase accuracy for `:cljs`.

src/refactor_nrepl/ns/suggest_aliases.clj

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns refactor-nrepl.ns.suggest-aliases
22
"Suggestion of aliases based on these guidelines: https://stuartsierra.com/2015/05/10/clojure-namespace-aliases"
33
(:require
4+
[clojure.edn :as edn]
45
[clojure.string :as string]))
56

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

16+
(def readable-as-symbol?
17+
(memoize
18+
(fn [s]
19+
(try
20+
(symbol? (edn/read-string s))
21+
(catch Exception _
22+
false)))))
23+
1524
(defn suggested-aliases [namespace-name]
1625
(let [fragments (-> namespace-name str (string/split #"\."))
1726
fragments (into []
@@ -29,8 +38,10 @@
2938
(range 1 (inc (count fragments)))
3039
(repeat (distinct fragments)))
3140
v (into {}
32-
(map (fn [segments]
33-
[(->> segments (string/join ".") (symbol)),
34-
[namespace-name]]))
41+
(keep (fn [segments]
42+
(let [candidate (->> segments (string/join "."))]
43+
(when (readable-as-symbol? candidate)
44+
[(symbol candidate),
45+
[namespace-name]]))))
3546
fragments)]
3647
(dissoc v namespace-name)))

test/refactor_nrepl/ns/suggest_aliases_test.clj

+23-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@
2121
'unit.foo true
2222
'foo.unit true))
2323

24+
(deftest readable-as-symbol?
25+
(testing "is readable as symbol"
26+
(are [input] (sut/readable-as-symbol? input)
27+
"test"
28+
"a.test"
29+
"a-test"
30+
"b.a-test"
31+
"ok.5<-here"
32+
"this:too!#$%&*"))
33+
34+
(testing "not readable as symbol"
35+
(are [input] (not (sut/readable-as-symbol? input))
36+
":kw"
37+
"15"
38+
"5-bad"
39+
"6:nope"
40+
";what"
41+
"#yeah")))
42+
2443
(deftest suggested-aliases
2544
(are [desc input expected] (testing input
2645
(is (= expected
@@ -46,4 +65,7 @@
4665
"Removes redundant bits such as `clj-` and `.core`"
4766
'clj-a.b.c.core '{c [clj-a.b.c.core]
4867
b.c [clj-a.b.c.core]
49-
a.b.c [clj-a.b.c.core]}))
68+
a.b.c [clj-a.b.c.core]}
69+
70+
"Removes fragments that start with a number (in this case: `9d`, `8b.c.9d`), since they aren't valid symbols"
71+
'a.8b.c.9d '{c.9d [a.8b.c.9d]}))

0 commit comments

Comments
 (0)