Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use kondo api #102

Closed
wants to merge 15 commits into from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*~
.*.swo
.*.swp
.clj-kondo/
.DS_Store
.eastwood
.idea/
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;; Please don't bump the library version by hand - use ci.release-workflow instead.
(defproject formatting-stack "1.0.1"
;; Please keep the dependencies sorted a-z.
:dependencies [[clj-kondo "2019.05.19-alpha"]
:dependencies [[clj-kondo "2020.01.13"]
[cljfmt "0.6.5" :exclusions [rewrite-clj]]
[com.gfredericks/how-to-ns "0.2.6"]
[com.gfredericks/lein-all-my-files-should-end-with-exactly-one-newline-character "0.1.1"]
Expand Down
2 changes: 1 addition & 1 deletion src/formatting_stack/branch_formatter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
strategies/exclude-cljs
strategies/jvm-requirable-files
strategies/namespaces-within-refresh-dirs-only)))
(-> (linters.kondo/new)
(-> (linters.kondo/new {})
(assoc :strategies (conj default-strategies
strategies/exclude-edn
strategies/exclude-clj
Expand Down
13 changes: 4 additions & 9 deletions src/formatting_stack/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require
[clojure.main]
[formatting-stack.background]
[formatting-stack.defaults :refer :all]
[formatting-stack.defaults :refer [default-processors default-formatters default-linters default-strategies]]
[formatting-stack.indent-specs :refer [default-third-party-indent-specs]]
[formatting-stack.protocols.formatter :as protocols.formatter]
[formatting-stack.protocols.linter :as protocols.linter]
Expand Down Expand Up @@ -69,14 +69,9 @@
in-background? (if (some? in-background?)
in-background?
true)
{formatters-strategies
:formatters
linters-strategies
:linters
processors-strategies
:processors
default-strategies
:default} strategies
{formatters-strategies :formatters
linters-strategies :linters
processors-strategies :processors} strategies
impl (bound-fn [] ;; important that it's a bound-fn (for an undetermined reason)
(process! protocols.formatter/format! formatters formatters-strategies strategies intersperse-newlines?)
(when intersperse-newlines?
Expand Down
2 changes: 1 addition & 1 deletion src/formatting_stack/defaults.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
strategies/exclude-cljs
strategies/jvm-requirable-files
strategies/namespaces-within-refresh-dirs-only)))
(-> (linters.kondo/new)
(-> (linters.kondo/new {})
(assoc :strategies (conj extended-strategies
strategies/exclude-edn
strategies/exclude-clj
Expand Down
5 changes: 3 additions & 2 deletions src/formatting_stack/formatters/clean_ns/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
[clojure.tools.reader :as tools.reader]
[clojure.tools.reader.reader-types :refer [push-back-reader]]
[clojure.walk :as walk]
[formatting-stack.util :refer [ensure-coll rcomp try-require]]
[formatting-stack.util :refer [ensure-coll rcomp]]
[nedap.speced.def :as speced]
[refactor-nrepl.config]
[refactor-nrepl.ns.clean-ns :refer [clean-ns]])
(:import
(clojure.lang Namespace)
(java.io File)))

(speced/defn ns-form-of [^string? filename]
Expand All @@ -20,7 +21,7 @@
nil
(throw e))))))

(speced/defn safely-read-ns-contents [^string? buffer, ^clojure.lang.Namespace ns-obj]
(speced/defn safely-read-ns-contents [^string? buffer, ^Namespace ns-obj]
(binding [tools.reader/*alias-map* (ns-aliases ns-obj)]
(tools.reader/read-string {:read-cond :allow
:features #{:clj}}
Expand Down
4 changes: 2 additions & 2 deletions src/formatting_stack/formatters/cljfmt/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(locking require-lock
(require namespace))
(ns-map namespace)
(catch Exception e
(catch Exception _
{})))

(spec/def ::indent-key #{:style/indent :style.cljfmt/indent :style.cljfmt/type})
Expand Down Expand Up @@ -106,7 +106,7 @@
(doseq [[sym var-ref] ns-mappings
:when (var? var-ref)
:let [fqn (fully-qualified-name-of var-ref)]
:when (some (fn [[k v]]
:when (some (fn [[k _]]
(= k fqn))
@result)
:let [indent (get @result fqn)]]
Expand Down
50 changes: 39 additions & 11 deletions src/formatting_stack/linters/kondo.clj
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
(ns formatting-stack.linters.kondo
(:require
[formatting-stack.linters.kondo.impl :as impl]
[clj-kondo.core :as clj-kondo]
[formatting-stack.protocols.linter :as linter]
[medley.core :refer [deep-merge]]
[nedap.utils.modular.api :refer [implement]]))

(def off {:level :off})

(def default-options
(list "--config" (pr-str {:linters {:invalid-arity off
:cond-without-else off}})))
{:linters {:cond-else off ;; undesired
:missing-docstring off ;; undesired
:unused-symbol off ;; undesired because potentially unreliable
:unused-private-var off ;; undesired because potentially unreliable
:consistent-alias off ;; duped by how-to-ns
:duplicate-require off ;; duped by clean-ns
:unused-import off ;; duped by clean-ns
:unused-namespace off ;; duped by clean-ns
:unused-referred-var off ;; duped by clean-ns
:unresolved-namespace off} ;; duped by clean-ns
:lint-as '{nedap.speced.def/def-with-doc clojure.core/defonce
nedap.speced.def/defn clojure.core/defn
nedap.speced.def/defprotocol clojure.core/defprotocol
nedap.speced.def/doc clojure.repl/doc
nedap.speced.def/fn clojure.core/fn
nedap.speced.def/let clojure.core/let
nedap.speced.def/letfn clojure.core/letfn}
:output {:exclude-files ["test-resources/*"
"test/unit/formatting_stack/formatters/cljfmt/impl/sample_data.clj"]}})

(defn lint! [this filenames]
(->> filenames
(cons "--lint")
(concat default-options)
(impl/parse-opts)
impl/lint!))
(def clj-options
"CLJ files are also linted by eastwood, disable duplicate linters"
{:linters {:misplaced-docstring off
:deprecated-var off
:redefined-var off}})

(defn new []
(implement {}
(defn lint! [{:keys [kondo-options]} filenames]
(let [{cljs-files true
clj-files false} (group-by (fn [f] (boolean (re-find #"\.cljs$" f))) filenames)
findings (->> [(clj-kondo/run! {:lint clj-files
:config (deep-merge default-options clj-options (or kondo-options {}))})
(clj-kondo/run! {:lint cljs-files
:config (deep-merge default-options (or kondo-options {}))})]
(map :findings)
(reduce into))]
(clj-kondo/print! {:findings findings})))

(defn new [{:keys [kondo-options] :as options}]
(implement options
linter/--lint! lint!))
Loading