-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
:suggest
option for the namespace-aliases
op
Fixes #369
- Loading branch information
Showing
11 changed files
with
248 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(ns refactor-nrepl.ns.suggest-aliases | ||
"Suggestion of aliases based on these guidelines: https://stuartsierra.com/2015/05/10/clojure-namespace-aliases" | ||
(:require | ||
[clojure.string :as string])) | ||
|
||
(defn test-like-ns-name? [ns-sym] | ||
(let [ns-str (str ns-sym)] | ||
(boolean (or (string/ends-with? ns-str "-test") | ||
(let [fragments (string/split ns-str #"\.") | ||
last-fragment (last fragments)] | ||
(or (string/starts-with? last-fragment "t-") | ||
(string/starts-with? last-fragment "test-") | ||
(some #{"test" "unit" "integration" "acceptance" "functional" "generative"} fragments))))))) | ||
|
||
(defn suggested-aliases [namespace-name] | ||
(let [fragments (-> namespace-name str (string/split #"\.")) | ||
fragments (into [] | ||
(comp (remove #{"core" "alpha" "api" "kws"}) | ||
(map (fn [s] | ||
(-> s | ||
(string/replace "-clj" "") | ||
(string/replace "clj-" "") | ||
(string/replace "-cljs" "") | ||
(string/replace "cljs-" "") | ||
(string/replace "-clojure" "") | ||
(string/replace "clojure-" ""))))) | ||
fragments) | ||
fragments (map take-last | ||
(range 1 (inc (count fragments))) | ||
(repeat (distinct fragments))) | ||
v (into {} | ||
(map (fn [segments] | ||
[(->> segments (string/join ".") (symbol)), | ||
[namespace-name]])) | ||
fragments)] | ||
(dissoc v namespace-name))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(ns bar.ns.libspecs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(ns foo.ns.libspecs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(ns refactor-nrepl.ns.libspecs-test | ||
(:require | ||
[clojure.java.io :as io] | ||
[clojure.test :refer [are deftest is testing]] | ||
[refactor-nrepl.ns.libspecs :as sut])) | ||
|
||
(def example-file | ||
(-> "foo/ns/libspecs.clj" io/resource io/as-file)) | ||
|
||
(def other-similarly-named-file | ||
(-> "bar/ns/libspecs.clj" io/resource io/as-file)) | ||
|
||
(def unreadable-file | ||
(-> "unreadable_file.clj" io/resource io/as-file)) | ||
|
||
(deftest add-tentative-aliases-test | ||
|
||
(testing "`ignore-errors?`" | ||
(let [files [unreadable-file]] | ||
(is (thrown? Exception (sut/add-tentative-aliases {} :clj files false))) | ||
(is (= {} | ||
(sut/add-tentative-aliases {} :clj files true))))) | ||
|
||
(are [desc base input expected] (testing desc | ||
(is (= expected | ||
(sut/add-tentative-aliases base :clj input false))) | ||
true) | ||
#_base #_input #_expected | ||
"Doesn't remove existing aliases" | ||
{'foo [`bar]} [] {'foo [`bar]} | ||
|
||
"Adds the two possible aliases for the given namespace" | ||
{'foo [`bar]} [example-file] '{foo [refactor-nrepl.ns.libspecs-test/bar] | ||
libspecs [foo.ns.libspecs] | ||
ns.libspecs [foo.ns.libspecs]} | ||
|
||
"When an existing alias overlaps with a suggested alias, | ||
the original one is kept and no other semantic is suggested | ||
(this way, any given alias will point to one namespace at most)" | ||
{'libspecs [`other]} [example-file] '{libspecs [refactor-nrepl.ns.libspecs-test/other], | ||
ns.libspecs [foo.ns.libspecs]} | ||
|
||
"If a namespace is already aliased, no extra aliases are suggested at all" | ||
{'example '[foo.ns.libspecs]} [example-file] '{example [foo.ns.libspecs]} | ||
|
||
"If a namespace is only aliased as `sut`, extra aliases are suggested as usual" | ||
{'sut '[foo.ns.libspecs]} [example-file] '{sut [foo.ns.libspecs], | ||
libspecs [foo.ns.libspecs], | ||
ns.libspecs [foo.ns.libspecs]} | ||
|
||
"If two files can result in the same alias being suggested, both will be included" | ||
{} [example-file other-similarly-named-file] '{libspecs [foo.ns.libspecs | ||
bar.ns.libspecs], | ||
ns.libspecs [foo.ns.libspecs | ||
bar.ns.libspecs]})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
(ns refactor-nrepl.ns.suggest-aliases-test | ||
(:require | ||
[clojure.test :refer [are deftest is testing]] | ||
[refactor-nrepl.ns.suggest-aliases :as sut])) | ||
|
||
(deftest test-like-ns-name? | ||
(are [input expected] (= expected | ||
(sut/test-like-ns-name? input)) | ||
'test true | ||
'tast false | ||
'a.test true | ||
'a.tast false | ||
'a-test true | ||
'a-ast false | ||
'b.a-test true | ||
'b.a-ast false | ||
't-foo true | ||
'bar.t-foo true | ||
'te-foo false | ||
'bar.te-foo false | ||
'unit.foo true | ||
'foo.unit true)) | ||
|
||
(deftest suggested-aliases | ||
(are [desc input expected] (testing input | ||
(is (= expected | ||
(sut/suggested-aliases input)) | ||
desc) | ||
(is (every? #{input} | ||
(->> input | ||
sut/suggested-aliases | ||
vals | ||
(reduce into []))) | ||
"The values of the returned hashmap always contain exactly the given ns as-is") | ||
true) | ||
"Returns nothing for a single-segment ns, because no alias can be derived from it" | ||
'a {} | ||
|
||
"Returns one alias for a two-segment ns" | ||
'a.b '{b [a.b]} | ||
|
||
"Returns two aliases for a three-segment ns" | ||
'a.b.c '{c [a.b.c] | ||
b.c [a.b.c]} | ||
|
||
"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]})) |