From d4a552c64be9af59e0a9475021b905630911bcb4 Mon Sep 17 00:00:00 2001 From: Reid 'arrdem' McKenzie Date: Thu, 10 Nov 2016 01:16:36 -0800 Subject: [PATCH] [#78] Monkeypatch core.logic to make sets work --- src/kibit/driver.clj | 47 ++++++++++++++++++++++----------- test/kibit/test/collections.clj | 16 +++++++---- test/kibit/test/driver.clj | 4 +++ test/resources/sets.clj | 4 +++ 4 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 test/resources/sets.clj diff --git a/src/kibit/driver.clj b/src/kibit/driver.clj index caab14e..3add54b 100644 --- a/src/kibit/driver.clj +++ b/src/kibit/driver.clj @@ -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" @@ -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 diff --git a/test/kibit/test/collections.clj b/test/kibit/test/collections.clj index e219f86..a81a1ea 100644 --- a/test/kibit/test/collections.clj +++ b/test/kibit/test/collections.clj @@ -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: diff --git a/test/kibit/test/driver.clj b/test/kibit/test/driver.clj index 2cd3534..ed93f4d 100644 --- a/test/kibit/test/driver.clj +++ b/test/kibit/test/driver.clj @@ -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))) diff --git a/test/resources/sets.clj b/test/resources/sets.clj new file mode 100644 index 0000000..694f0a3 --- /dev/null +++ b/test/resources/sets.clj @@ -0,0 +1,4 @@ +(ns resources.sets) + +(defn killit [coll] + (not-any? #{"string1" "string2"} (map ffirst coll)))