Merge branch 'better-reporting'

This commit is contained in:
Jonas Enlund 2012-03-13 21:53:11 +02:00
commit 058c7c66f2
2 changed files with 37 additions and 8 deletions

View file

@ -16,8 +16,16 @@
(comment
(when (not (pred? x y)) (f x y))
(when true
(if (pred? x)
(do
(action a)
(action b)))))
(if (pred? x)
(do (action a)
(action b)
(if-let [x (f a b c)
y (g a b c)
z (h a b c)]
(do (+ 1 0)
(= 1 1)
(< 1 0)))
(action d)
(action f))
nil))

View file

@ -1,7 +1,24 @@
(ns leiningen.kibit
(:require [clojure.tools.namespace :as clj-ns]
(:require [clojure.string :as string]
[clojure.tools.namespace :as clj-ns]
[clojure.java.io :as io]
[jonase.kibit.core :as kibit]))
[clojure.pprint :as pp]
[jonase.kibit.core :as kibit])
(:import [java.io StringWriter]))
;; A hack to get the code indented.
(defn pprint-code [form]
(let [string-writer (StringWriter.)]
(pp/write form
:dispatch pp/code-dispatch
:stream string-writer
:pretty true)
(->> (str string-writer)
string/split-lines
(map #(str " " %))
(string/join "\n")
println)))
(defn kibit
"Suggest idiomatic replacements for patterns of code."
@ -14,5 +31,9 @@
(or (second (clj-ns/read-file-ns-decl source-file)) source-file))
(with-open [reader (io/reader source-file)]
(doseq [{:keys [line expr alt]} (kibit/check-file reader)]
(printf "[%s] Consider %s instead of %s\n" line alt expr)))
(printf "[%s] Consider:\n" line)
(pprint-code alt)
(println "instead of:")
(pprint-code expr)
(newline)))
(flush))))