Skip to content

Commit

Permalink
[#78] Monkeypatch core.logic to make sets work
Browse files Browse the repository at this point in the history
  • Loading branch information
arrdem committed Nov 10, 2016
1 parent e751aff commit d4a552c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
47 changes: 31 additions & 16 deletions src/kibit/driver.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
(ns kibit.driver
(:require [clojure.java.io :as io]
[kibit.rules :refer [all-rules]]
[kibit.check :refer [check-file]]
[kibit.reporters :refer :all]
[clojure.tools.cli :refer [cli]])
(:import [java.io File]))
"The (leiningen) facing interface for Kibit. Provides helpers for finding files in a project, and
linting a list of files."
(:require [clojure.core.logic :refer [tree-term?]]
[clojure.java.io :as io]
[clojure.tools.cli :refer [cli]]
[kibit
[check :refer [check-file]]
[reporters :refer :all]
[rules :refer [all-rules]]])
(:import java.io.File))

(defn our-tree-term? [x]
(and (or (coll? x)
(instance? clojure.core.logic.protocols.ITreeTerm x))
(not (instance? clojure.lang.IPersistentSet x))))

(def cli-specs [["-r" "--reporter"
"The reporter used when rendering suggestions"
Expand Down Expand Up @@ -33,16 +42,22 @@

(defn run [source-paths rules & args]
(let [[options file-args usage-text] (apply (partial cli args) cli-specs)
source-files (mapcat #(-> % io/file find-clojure-sources-in-dir)
(if (empty? file-args) source-paths file-args))]
(mapcat (fn [file] (try (check-file file
:reporter (name-to-reporter (:reporter options)
cli-reporter)
:rules (or rules all-rules))
(catch Exception e
(binding [*out* *err*]
(println "Check failed -- skipping rest of file")
(println (.getMessage e))))))
source-files (mapcat #(-> % io/file find-clojure-sources-in-dir)
(if (empty? file-args) source-paths file-args))]
(mapcat (fn [file]
(let [old-tree-term tree-term?]
(try
(.bindRoot ^clojure.lang.Var #'tree-term? our-tree-term?)
(check-file file
:reporter (name-to-reporter (:reporter options)
cli-reporter)
:rules (or rules all-rules))
(catch Exception e
(binding [*out* *err*]
(println "Check failed -- skipping rest of file")
(println (.getMessage e))))
(finally
(.bindRoot ^clojure.lang.Var #'tree-term? old-tree-term)))))
source-files)))

(defn external-run
Expand Down
16 changes: 11 additions & 5 deletions test/kibit/test/collections.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@

(deftest collections-are
(are [expected-alt-form test-form]
(= expected-alt-form (:alt (kibit/check-expr test-form)))
'(seq a) '(not (empty? a))
(= expected-alt-form (:alt (kibit/check-expr test-form)))
'(seq a) '(not (empty? a))
'(when (seq a) b) '(when-not (empty? a) b)
'(when (seq a) b) '(when (not (empty? a)) b)
'(vector a) '(conj [] a)

'(vector a) '(conj [] a)
'(vector a b) '(conj [] a b)
'(vec coll) '(into [] coll)
'(vec coll) '(into [] coll)

'(set coll) '(into #{} coll)

'(update-in coll [k] f) '(assoc coll k (f (k coll)))
'(update-in coll [k] f) '(assoc coll k (f (coll k)))
'(update-in coll [k] f) '(assoc coll k (f (get coll k)))

'(assoc-in coll [k0 k1] a) '(assoc coll k0 (assoc (k0 coll) k1 a))
'(assoc-in coll [k0 k1] a) '(assoc coll k0 (assoc (coll k0) k1 a))
'(assoc-in coll [k0 k1] a) '(assoc coll k0 (assoc (get coll k0) k1 a))
'(assoc-in coll [k1 k2] v) '(update-in coll [k1 k2] assoc v)

'(update-in coll [k] f a b c) '(assoc coll k (f (k coll) a b c))
'(update-in coll [k] f a b c) '(assoc coll k (f (coll k) a b c))
'(update-in coll [k] f a b c) '(assoc coll k (f (get coll k) a b c))
'(assoc-in coll [k1 k2] v) '(update-in coll [k1 k2] assoc v)

'(repeatedly 10 (constantly :foo)) '(take 10 (repeatedly (constantly :foo)))

;; some wrong simplifications happened in the past:
Expand Down
4 changes: 4 additions & 0 deletions test/kibit/test/driver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
(deftest find-clojure-sources-are
(is (= [(io/file "test/resources/first.clj")
(io/file "test/resources/second.cljx")
(io/file "test/resources/sets.clj")
(io/file "test/resources/third.cljs")]
(driver/find-clojure-sources-in-dir (io/file "test/resources")))))

(deftest test-set-file
(is (driver/run ["test/resources/sets.clj"] nil)))
4 changes: 4 additions & 0 deletions test/resources/sets.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns resources.sets)

(defn killit [coll]
(not-any? #{"string1" "string2"} (map ffirst coll)))

0 comments on commit d4a552c

Please sign in to comment.