-
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
9 changed files
with
199 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,27 @@ | ||
(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 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 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,45 @@ | ||
(ns refactor-nrepl.ns.libspecs-test | ||
(:require | ||
[refactor-nrepl.ns.libspecs :as sut] | ||
[clojure.test :refer [are deftest is testing]] | ||
[clojure.string :as string] | ||
[clojure.java.io :as io])) | ||
|
||
(def this-file | ||
(-> ::_ | ||
namespace | ||
(string/replace "." "/") | ||
(string/replace "-" "_") | ||
(str ".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]} [this-file] {'foo [`bar] | ||
'libspecs-test ['refactor-nrepl.ns.libspecs-test] | ||
'ns.libspecs-test ['refactor-nrepl.ns.libspecs-test]} | ||
|
||
"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-test [`other]} [this-file] {'libspecs-test [`other] | ||
'ns.libspecs-test ['refactor-nrepl.ns.libspecs-test]})) |
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,31 @@ | ||
(ns refactor-nrepl.ns.suggest-aliases-test | ||
(:require | ||
[clojure.test :refer [are deftest is testing]] | ||
[refactor-nrepl.ns.suggest-aliases :as sut])) | ||
|
||
(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]})) |