Browse files

[Issue #22] disambiguate: pick refers that were referred from same ns…

… in old ns form
  • Loading branch information...
1 parent 99167c1 commit 835a6717e70a602e7fb2015ce8cd46016d44900e @AlexBaranosky AlexBaranosky committed Jan 2, 2013
Showing with 36 additions and 8 deletions.
  1. +18 −8 src/slam/hound/regrow.clj
  2. +18 −0 test/slam/hound_test.clj
View
26 src/slam/hound/regrow.clj
@@ -97,17 +97,27 @@
:require-refer [:require :use] ; could've been require/refer or use/only
[new-type]))
+(defn- referred-to-in-originals-pred [type originals]
+ (if-not (= type :require-refer)
+ (constantly false)
+ (fn [[ns1 _ [alias1]]]
+ (some (fn [[[ns2 _ [alias2] :as arg]]]
+ (and (= alias1 alias2) (= ns1 ns2)))
+ originals))))
+
(defn- disambiguate [candidates missing ns-map type]
;; TODO: prefer things in src/classes to jars
(debug :disambiguating missing :in candidates)
- (->> candidates
- (sort-by (juxt (complement (in-originals-pred
- (map #(get (:old ns-map) %)
- (new-type-to-old-types type))))
- ;; TODO: prefer candidates where last segment matches
- (comp count str)))
- (remove #(re-find disambiguator-blacklist (str %)))
- first))
+ (let [orig-clauses (map #(get (:old ns-map) %)
+ (new-type-to-old-types type))]
+ (->> candidates
+ (sort-by (juxt (complement (in-originals-pred orig-clauses))
+ (complement (referred-to-in-originals-pred
+ type orig-clauses))
+ ;; TODO: prefer candidates where last segment matches
+ (comp count str)))
+ (remove #(re-find disambiguator-blacklist (str %)))
+ first)))
(defn- grow-step [missing type ns-map body]
(if-let [addition (disambiguate (candidates type missing body)
View
18 test/slam/hound_test.clj
@@ -62,6 +62,24 @@
[[:name] String]
{:name "Bob"})}}))))))
+(deftest ^:integration test-chooses-referred-vars-preferring-those-referred-to-in-old-ns
+ (testing "since both clojure.string and clojure.set are present in the original namespace
+ we must disambiguate further by preferring namespaces that also referred the var
+ in the old version of the ns form"
+ (is (= "(ns foo.bar
+ (:require [clojure.set :refer [union]]
+ [clojure.string :refer [join]]))
+"
+ (reconstruct (StringReader.
+ (str '(ns foo.bar
+ (:require [clojure.string :only [join]]
+ [clojure.set :refer [union]]))
+ '(do
+ (defn f [xs]
+ (join "," xs))
+ (defn g [a b]
+ (union a b))))))))))
+
(deftest ^:integration test-prefers-requires-as-clauses-from-orig-ns
;; korma.core is on the :dev-dependencies, and was getting erroneously picked
;; for these 2 namespaces

0 comments on commit 835a671

Please sign in to comment.