
Clojure代碼的靜態分析儀和Linter引起歡樂。

非常感謝Clj-Kondo。這就像我的同伴。這使Clojure再次變得有趣。
- @geraldodev on clojurians懈怠
Clj-Kondo對Clojure,Clojurescript和EDN進行靜態分析。它會在輸入時通知您可能出現的潛在錯誤(無執行程序)。
CLJ-KONDO檢測到:
def表達式do , let包裝let and letfn綁定呼叫recur )clojure.test/deftest中缺少斷言在您的表格命中率之前。
它建議了幾種樣式指南建議,例如:
:else作為cond中的全部測試表達式(請參閱Clojure樣式指南)seq而不是(not (empty? ,,,)) (請參閱Clojure樣式指南)
它支持對clojure.core.async/alt!! , schema.core/defn potemkin/import-vars
它檢測到deps.edn和bb.edn中的常見錯誤
它提供分析數據,使您可以構建自己的自定義襯裡。
在此處查看所有可用的襯裡。
這個林格是:
.clj , .cljs , .cljc和.edn文件兼容在互動遊樂場嘗試CLJ-KONDO。
觀看談話:
您可以通過GitHub贊助商,OpenCollective,Ko-Fi或間接通過Clojurists一起支持此項目。
來自stdin的皮棉:
$ echo ' (def x (def x 1)) ' | clj-kondo --lint -
<stdin>:1:8: warning: inline def棉絨文件:
$ echo ' (def x (def x 1)) ' > /tmp/foo.clj
$ clj-kondo --lint /tmp/foo.clj
/tmp/foo.clj:1:8: warning: inline def棉絨目錄:
$ clj-kondo --lint src
src/clj_kondo/test.cljs:7:1: warning: redundant do
src/clj_kondo/calls.clj:291:3: error: Wrong number of args (1) passed to clj-kondo.calls/analyze-calls索具項目類Path:
$ clj-kondo --lint " $( lein classpath ) "幫助:
$ clj-kondo --help
clj-kondo v2024.11.14
Options:
--lint <file>: a file can either be a normal file, directory or classpath. In the
case of a directory or classpath, only .clj, .cljs and .cljc will be
processed. Use - as filename for reading from stdin.
--lang <lang>: if lang cannot be derived from the file extension this option will be
used. Supported values: clj, cljs, cljc.
--filename <file>: in case stdin is used for linting, use this to set the
reported filename.
--cache-dir: when this option is provided, the cache will be resolved to this
directory. If --cache is false, this option will be ignored.
--cache: if false, won't use cache. Otherwise, will try to resolve cache
using `--cache-dir`. If `--cache-dir` is not set, cache is resolved using the
nearest `.clj-kondo` directory in the current and parent directories.
--config <config>: extra config that is merged. May be a file or an EDN expression. See https://github.com/clj-kondo/clj-kondo/blob/master/doc/config.md.
--config-dir <config-dir>: use this config directory instead of auto-detected
.clj-kondo dir.
--parallel: lint sources in parallel.
--dependencies: don't report any findings. Useful for populating cache while linting dependencies.
--copy-configs: copy configs from dependencies while linting.
--skip-lint: skip lint/analysis, still check for other tasks like copy-configs.
--fail-level <level>: minimum severity for exit with error code. Supported values:
warning, error. The default level if unspecified is warning.
--report-level <level>: minimum severity for which to report. Supported values:
info, warning, error. The default level if unspecified is info.
--debug: print debug information. 為了檢測項目中命名空間的棉布錯誤,需要緩存。為了讓Clj-Kondo知道在哪裡創建一個,請在項目的根部製作.clj-kondo目錄,這意味著與您的project.clj , deps.edn或build.boot :buil.boot:
$ mkdir -p .clj-kondo當您運行clj-kondo時,將在其內部創建一個緩存。在編輯器內部覆蓋之前,建議您使用整個class Path教授clj-kondo有關您使用的所有庫,包括Clojure和/或Clojurescript本身。一些庫帶有配置。要導入它們,首先運行:
$ clj-kondo --lint " <classpath> " --dependencies --copy-configs --skip-lint --copy-configs標誌將搜索並複制從依賴關係中的配置中,並將其搜索到.clj-kondo目錄中,linting(請參閱config.md)。
使用配置,現在我們可以正確分析依賴項:
$ clj-kondo --lint " <classpath> " --dependencies --parallel --dependencies標誌表明CLJ-KONDO用於分析源以填充緩存。啟用後,CLJ-KONDO將抑制警告並跳過已經凸起的.jar文件以供性能。
--parallel選項將使用多個線程來提起您的來源,並更快地通過它們。
注意:在2024.05.24之後的版本中,複製配置和覆蓋依賴項可以一次使用:
$ clj-kondo --lint " <classpath> " --dependencies --parallel --copy-configs構建工具特定的方法來獲取類路徑:
lein classpathboot with-cp -w -f -clojure -Spathnpx shadow-cljs classpath因此,對於lein整個命令將是:
$ clj-kondo --lint "$(lein classpath)" --dependencies --parallel --copy-configs
現在,您可以使用編輯器集成來提高單個文件。對編輯器中編輯文件時發生的模擬:
$ echo ' (select-keys) ' | clj-kondo --lang cljs --lint -
<stdin>:1:1: error: Wrong number of args (0) passed to cljs.core/select-keys由於Clj-Kondo現在通過緩存知道您的Clojurescript版本,因此它檢測到您傳遞給select-keys參數數量無效。每次您編輯文件時,緩存都會逐步更新,因此CLJ-KONDO會知道您剛剛編寫的新功能。
如果要使用其他目錄來讀取緩存,請使用--cache-dir選項。即使您有一個.clj-kondo目錄,請禁用緩存,請使用--cache false 。
退出代碼可以由--fail-level <level>選項控制。默認失敗級別是warning ,該警告返回出口代碼如下:
0 :找不到錯誤或警告2 :發現一個或多個警告3 :發現一個或多個錯誤如果提供了--fail-level error ,則警告不會導致非零退出代碼:
0 :找不到錯誤0 :發現一個或多個警告3 :發現一個或多個錯誤除0和3以外的所有退出2都均表示錯誤,因為CLJ-KONDO中的錯誤或CLJ-KONDO的控制之外的其他一些意外錯誤。
由於Clj-Kondo是靜態分析儀,因此不需要運行時(JVM,瀏覽器,node.js等)。它不會執行您的代碼。因此,它可以是使用運行時(例如Eastwood)的Linters的更快替代方法。這種方法具有Clj-Kondo無法執行宏的限制,因為宏可以使用運行時的任意功能。 Clj-Kondo支持Clojure Core宏和社區的一些受歡迎的圖書館。可以使用配置支持不支持的宏。配置宏的方法之一是為它們編寫鉤子(也請參見此Blogpost)。對於許多庫,您已經可以導入一種配置。還可以查看包含第三方庫配置的CLJ-KONDO配置。
Clj-Kondo可以被稱為Babashka Pod。
#! /usr/bin/env bb
( ns script
( :require [babashka.pods :as pods]))
( pods/load-pod " clj-kondo " )
( require '[pod.borkdude.clj-kondo :as clj-kondo])
( clj-kondo/merge-configs
'{ :linters { :unresolved-symbol { :exclude [( foo1.bar )]}}}
'{ :linters { :unresolved-symbol { :exclude [( foo2.bar )]}}})
; ;=> {:linters {:unresolved-symbol {:exclude [(foo1.bar) (foo2.bar)]}}}
( -> ( clj-kondo/run! { :lint [ " src " ]})
:summary )
; ;=> {:error 0, :warning 0, :info 0, :type :summary, :duration 779} var-info.edn和靈感版權所有©2019-2023 Michiel Borkent
根據EPL許可分發,與Clojure相同。請參閱許可證。
該目錄inlined目錄包含來自tools.reader的來源。讀取器,該讀取器已獲得EPL許可證的許可。
目錄parser包含從MIT許可證獲得許可的rewrite-clj的修改源。