Skip to content
This repository has been archived by the owner on Oct 2, 2020. It is now read-only.

Commit

Permalink
[Issue #1] use new/better heuristic for finding a namespace to use fo…
Browse files Browse the repository at this point in the history
…r :require/:refer
  • Loading branch information
Alex Baranosky committed Dec 24, 2012
1 parent cb50dd9 commit cb6f564
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
20 changes: 15 additions & 5 deletions src/slam/hound/regrow.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))

Expand All @@ -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))))
Expand Down
4 changes: 1 addition & 3 deletions src/slam/hound/stitch.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
12 changes: 6 additions & 6 deletions test/slam/hound_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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.