
เครื่องวิเคราะห์แบบสแตติกและ linter สำหรับรหัส Clojure ที่ประกายความสุข

ขอบคุณมากสำหรับ Clj-Kondo มันเป็นเหมือนเพื่อนสำหรับฉัน มันทำให้ Clojure สนุกอีกครั้ง
- @geraldodev บน Clojurians Slack
CLJ-Kondo ทำการวิเคราะห์แบบคงที่เกี่ยวกับ Clojure, Clojurescript และ Edn จะแจ้งให้คุณทราบเกี่ยวกับข้อผิดพลาดที่อาจเกิดขึ้นในขณะที่คุณกำลังพิมพ์ (โดยไม่ต้องดำเนินการโปรแกรมของคุณ)
CLJ-Kondo ตรวจพบ:
def แบบอินไลน์do และ let ห่อlet ผล 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
มันให้ข้อมูลการวิเคราะห์เพื่อให้คุณสร้าง linters ที่กำหนดเองของคุณเอง
ดูผ้าลินินที่มีอยู่ทั้งหมดที่นี่
linter นี้คือ:
.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ผ้าสำลีโครงการ Classpath:
$ 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 :
$ mkdir -p .clj-kondo แคชจะถูกสร้างขึ้นภายในเมื่อคุณเรียกใช้ clj-kondo ก่อนที่จะเป็นผ้าสำลีในบรรณาธิการของคุณขอแนะนำให้ผ้าสำลีทั้งหมด classpath เพื่อสอน clj-kondo เกี่ยวกับห้องสมุดทั้งหมดที่คุณใช้รวมถึง Clojure และ/หรือ clojurescript เอง ห้องสมุดบางแห่งมาพร้อมกับการกำหนดค่า เพื่อนำเข้าก่อนการวิ่งครั้งแรก:
$ clj-kondo --lint " <classpath> " --dependencies --copy-configs --skip-lint ธง --copy-configs จะค้นหาและคัดลอกการกำหนดค่าจากการอ้างอิงไปยังไดเรกทอรี .clj-kondo ในขณะที่ผ้าสำลี (ดู 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สร้างวิธีการเฉพาะเครื่องมือในการรับ classpath:
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 , 2 และ 3 ระบุข้อผิดพลาดเนื่องจากข้อผิดพลาดใน CLJ-Kondo หรือข้อผิดพลาดที่ไม่คาดคิดอื่น ๆ นอกเหนือจากการควบคุมของ CLJ-Kondo
เนื่องจาก CLJ-Kondo เป็นเครื่องวิเคราะห์แบบคงที่ไม่จำเป็นต้องใช้งานรันไทม์ (JVM, เบราว์เซอร์, node.js ฯลฯ ) มันไม่ได้เรียกใช้รหัสของคุณ เช่นนี้อาจเป็นทางเลือกที่เร็วกว่าสำหรับ Linters ที่ใช้รันไทม์เช่น Eastwood วิธีการนี้มาพร้อมกับข้อ จำกัด ที่ CLJ-Kondo ไม่สามารถดำเนินการแมโครของคุณได้เนื่องจากแมโครสามารถใช้คุณสมบัติตามอำเภอใจจากรันไทม์ CLJ-Kondo มีการสนับสนุน Macros Core Core และห้องสมุดยอดนิยมจากชุมชน มาโครที่ไม่รองรับนอกกรอบสามารถรองรับได้โดยใช้การกำหนดค่า วิธีหนึ่งในการกำหนดค่ามาโครคือการเขียนตะขอให้พวกเขา (ดูบล็อกนี้) สำหรับห้องสมุดหลายแห่งมีการกำหนดค่าที่คุณสามารถนำเข้าได้แล้ว ตรวจสอบ clj-kondo configs ซึ่งมีการกำหนดค่าสำหรับไลบรารีของบุคคลที่สาม
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 ไดเรกทอรีมีแหล่งที่มาที่ดัดแปลงจาก rewrite-clj ซึ่งได้รับใบอนุญาตภายใต้ใบอนุญาต MIT