From 0b1c320e3deeb906db0e18c54fda38111388c24f Mon Sep 17 00:00:00 2001 From: Federico Tomassetti Date: Sat, 2 Aug 2014 09:15:18 +0100 Subject: [PATCH] First implementation of the linter --- src/app/core.clj | 9 ++++++++- src/app/linter.clj | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/app/core.clj b/src/app/core.clj index 98da7f9..964b97b 100644 --- a/src/app/core.clj +++ b/src/app/core.clj @@ -37,6 +37,9 @@ (println banner) (System/exit 1)) +(defn info [opts msg] + (println " [info] " msg)) + ; TODO extract part of this method to cli (defn -main "What I do" @@ -51,7 +54,11 @@ (when (:linter opts) (when (or (:interactive opts) (:query opts)) (usageError banner opts "Linter, interactive and query mode are self exclusive")) - (linter) + (when (not (:dir opts)) + (info opts "Linter, no directory indicated. Using current directory") + (linter ".") + (System/exit 0)) + (linter (:dir opts)) (System/exit 0)) (when (:interactive opts) (do diff --git a/src/app/linter.clj b/src/app/linter.clj index f8df367..5ca4fb0 100644 --- a/src/app/linter.clj +++ b/src/app/linter.clj @@ -2,5 +2,33 @@ ; Linter ; ============================================ -(defn linter [] - (println "Linter running")) \ No newline at end of file +; For a lint report we want to know: +; * which element is affected +; * have a message describing the warning/error +; The element is always the first returned by the operation + +(defrecord Check [operation params message]) + +(def checks + [(Check. classesWithManyConstructorsOp {:threshold 4} "This class has too many constructors (#1#). Considers the Builder pattern")]) + +(defn replaceParamsInMessage [message result] + (clojure.string/replace message "#1#" (toString (nth result 1)))) + +(defn printCheckResult [result message] + (println (toString (nth result 0)) ":" (replaceParamsInMessage message result))) + +(defn executeCheck [check cus] + (let [operation (.operation check) + params (.params check) + threshold (:threshold params) + results ((.query operation) {:cus cus :threshold threshold}) + message (.message check)] + (doseq [r results] + (printCheckResult r message)))) + +(defn linter [dir] + (let [cus (cus dir)] + (println "Loaded" (.size cus) "files") + (doseq [c checks] + (executeCheck c cus)))) \ No newline at end of file