Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Issue #1] use new/better heuristic for finding a namespace to use fo…

…r :require/:refer
  • Loading branch information...
commit cb6f564c8fe6c6da8ae9137bb0b95010823f19c4 1 parent cb50dd9
Alex Baranosky authored
View
20 src/slam/hound/regrow.clj
@@ -43,13 +43,23 @@
(finally
(remove-ns (.name *ns*)))))))
-(defn candidates [type missing]
+(def ^:private attempted-vars-in-body
+ (memoize (fn [body]
+ (apply merge-with concat
+ (for [value (flatten body)
+ :when (symbol? value)
+ :let [[_ alias var-name] (re-matches #"(.+)/(.+)" (str value))]
+ :when alias]
+ {alias [(symbol var-name)]})))))
+
+(defn candidates [type missing body]
(case type
:import (for [class-name search/available-classes
:when (= missing (last (.split class-name "\\.")))]
(symbol class-name))
:require-as (for [n (all-ns)
- :when (= missing (last (.split (name (ns-name n)) "\\.")))]
+ :let [vars-with-alias (get (attempted-vars-in-body body) missing)]
+ :when (every? (set (keys (ns-publics n))) vars-with-alias)]
[(ns-name n) :as (symbol missing)])
:require-refer (for [n (all-ns)
[sym var] (ns-publics n)
@@ -85,8 +95,8 @@
(remove #(re-find disambiguator-blacklist (str %)))
first))
-(defn- grow-step [missing type ns-map]
- (if-let [addition (disambiguate (candidates type missing) missing ns-map type)]
+(defn- grow-step [missing type ns-map body]
+ (if-let [addition (disambiguate (candidates type missing body) missing ns-map type)]
(update-in ns-map [type] conj addition)
ns-map))
@@ -109,7 +119,7 @@
(inc type-to-try)
0)]
(if-let [type (get possible-types type-idx)]
- (recur (grow-step missing type ns-map) missing type-idx)
+ (recur (grow-step missing type ns-map body) missing type-idx)
(throw (Exception. (str "Couldn't resolve " missing
", got as far as " ns-map)))))
ns-map))))
View
4 src/slam/hound/stitch.clj
@@ -35,9 +35,7 @@
ns-map-clauses))
(defn ns-from-map [ns-map]
- (let [ns-map (-> ns-map ;; combining :require-as and :require-refer into :require
- (assoc :require (concat (:require-as ns-map) (:require-refer ns-map)))
- (dissoc :require-as :require-refer))]
+ (let [ns-map (assoc ns-map :require (concat (:require-as ns-map) (:require-refer ns-map)))]
`(~'ns ~(:name ns-map)
~@(if-let [doco (:doc (:meta ns-map))] ; avoid inserting nil
[doco])
View
12 test/slam/hound_test.clj
@@ -40,11 +40,11 @@
-(def problematic-ns (str '(ns foo.bar
- (:require [clj-schema.validation :as val]))
- '(val/validation-errors [[:name] String] {:name "Bob"})))
-
(deftest ^:integration test-regression
- (is (= problematic-ns
- (reconstruct (StringReader. problematic-ns)))))
+ (is (= "(ns foo.bar
+ (:require [clj-schema.validation :as val]))
+"
+ (reconstruct (StringReader. (str '(ns foo.bar
+ (:require [clj-schema.validation :as val]))
+ '(val/validation-errors [[:name] String] {:name "Bob"})))))))

0 comments on commit cb6f564

Please sign in to comment.
Something went wrong with that request. Please try again.