• Stars
    star
    1,765
  • Rank 26,377 (Top 0.6 %)
  • Language
    Clojure
  • Created over 12 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

There's a function for that!

kibit

There's a function for that!

kibit is a static code analyzer for Clojure, ClojureScript, cljx and other Clojure variants. It uses core.logic to search for patterns of code that could be rewritten with a more idiomatic function or macro. For example if kibit finds the code

(if (some test)
  (some action)
  nil)

it will suggest using when instead:

(when (some test)
  (some action))

Usage

Add [lein-kibit "0.1.8"] to your :plugins vector in your :user profile. Then you can run

$ lein kibit

to analyze a Leiningen project's namespaces. Kibit will automatically pick up source paths from your project.clj from the following keyseqs: [:source-paths], [:cljsbuild :builds], and [:cljx :builds]. You can also run Kibit manually on individual files or folders (even if there is no Leiningen project.clj) by running:

$ lein kibit path/to/some/file.clj #or
$ lein kibit path/to/src/ #or
$ lein kibit path/to/src/clj/ path/to/src/cljs/util.cljs some/combo/of/files/and/folders.cljx

If you want to know how the Kibit rule system works there are some slides available at http://jonase.github.io/kibit-demo/.

Exit codes

If lein kibit returns any suggestions to forms then its exit code will be 1. Otherwise it will exit 0. This can be useful to add in a build step for automated testing.

$ lein kibit
... suggestions follow

$ echo $?
1

Automatically rerunning when files change

You can use lein-auto to run kibit automatically when files change. Visit lein-auto's README for installation instructions. Note that this will run kibit over all of your files, not just the ones that have changed.

$ lein auto kibit
auto> Files changed: project.clj, [...]
auto> Running: lein kibit
... suggestions follow
auto> Failed.
auto> Files changed: test/my/test/misc.clj
auto> Running: lein kibit
... suggestions follow
auto> Failed.

Automatically replacing suggestions in source file

You can have kibit automatically apply suggestions to your source files.

Given a file:

(ns example)

(+ 1 a)
$ lein kibit --replace

will rewrite the file as:

(ns example)

(inc a)

Replacement can also be run interactively:

$ lein kibit --replace --interactive
 Would you like to replace
   (+ 1 a)
  with
   (inc a)
 in example.clj:3? [yes/no]

Reporters

Kibit comes with two reporters, the default plaintext reporter, and a GitHub Flavoured Markdown reporter. To specify a reporter, use the -r or --reporter commandline argument. For example:

lein kibit --reporter markdown
----
##### `test/project/core.clj:31`
Consider using:
```clojure
  (when true (println "hi"))
```
instead of:
```clojure
  (if true (do (println "hi")))
```

----
##### `test/project/core.clj:32`
Consider using:
```clojure
  (println "hi")
```
instead of:
```clojure
  (do (println "hi"))
```

which renders to:


test/project/core.clj:31

Consider using:

  (when true (println "hi"))

instead of:

  (if true (do (println "hi")))

...

Usage from deps.edn

Add the following to your aliases

:kibit {:extra-deps {jonase/kibit {:mvn/version "0.X.Y"}}
        :exec-fn kibit.driver/exec
        :exec-args {:paths ["."]}

Then run clojure -X:kibit. For more options, please see the docstring on kibit.driver/exec.

NOTE: At least Clojure v1.9 and Kibit v0.X.Y are required to use this exec-fn method.

For alternative options to use kibit from inside deps.edn, check out this issue

Usage from inside Emacs

If you use Emacs for hacking Clojure, here's a way to use kibit from inside Emacs with all the fanciness you are used to from M-x compile. The kibit-helper package available from MELPA provides several handy commands. First, make sure you have MELPA available as a source of packages (which you may well already have done). As described in their Getting started section, put the following into your ~/.emacs:

(add-to-list 'package-archives
             '("melpa-stable" . "http://stable.melpa.org/packages/") t)

(If you want to be more on the cutting edge, you can include unreleased versions of packages using the non-stable URL, as explained in the MELPA instructions, but kibit-helper is also available from the less exciting stable repository.)

This will give you three new commands,

M-x kibit
M-x kibit-current-file
M-x kibit-accept-proposed-change

The first two cause the properly highlighted and hyperlinked kibit output to be presented in a *Kibit Suggestions* buffer. The third lets you automatically apply most of those suggestions to your source. (Suggestions which cite large blocks of code including comments cannot be automatically applied, as Kibit discards comments during processing.)

You will likely want to bind the last function to C-x C-` so it is easy to alternate with the next-error function (conventionally C-x `) as you walk through the suggestions made by Kibit:

(global-set-key (kbd "C-x C-`") 'kibit-accept-proposed-change)

Usage from inside vim (through Leiningen plugin)

kibit-vim allows you to analyze the current opened file by running:

:Kibit

Usage from inside vim (with vim-fireplace)

If you have vim-fireplace installed, you can use vim-kibit to run Kibit on your current buffer through the running REPL session.

vim-kibit is invoked by running:

:Kibit

Known limitations

Kibit reads source code without any macro expansion or evaluation. A macro can therefore easily invalidate a rule. Also, kibit will not know if the symbol + in the form (+ x 1) actually refers to a local or to a function in a namespace other than clojure.core. Expect some false positives.

Contributing

It is very easy to write new patterns for kibit. Take a look at control-structures.clj to see how new patterns are created. If you know of a recurring pattern of code that can be simplified, please consider sending me a pull request.

Bugs can be reported using the GitHub issue tracker.

Contributors

Thanks to all who have contributed to kibit!

TODO

  • Leiningen project.clj setting for rule exclusion
  • Leiningen project.clj setting for a directory of rules to include

License

Copyright ยฉ 2012 Jonas Enlund

Distributed under the Eclipse Public License, the same as Clojure.

More Repositories

1

aleph

Asynchronous streaming communication for Clojure - web server, web client, and raw TCP/UDP
Clojure
2,544
star
2

seesaw

Seesaw turns the Horror of Swing into a friendly, well-documented, Clojure library
Clojure
1,445
star
3

manifold

A compatibility layer for event-driven abstractions
Clojure
1,017
star
4

etaoin

Pure Clojure Webdriver protocol implementation
Clojure
913
star
5

marginalia

Ultra-lightweight literate programming for clojure inspired by docco
Clojure
816
star
6

secretary

A client-side router for ClojureScript.
Clojure
773
star
7

hickory

HTML as data
Clojure
634
star
8

claypoole

Claypoole: Threadpool tools for Clojure
Clojure
607
star
9

pretty

Library for helping print things prettily, in Clojure - ANSI fonts, formatted exceptions
Clojure
597
star
10

rewrite-clj

Rewrite Clojure code and edn
Clojure
576
star
11

potemkin

some ideas which are almost good
Clojure
568
star
12

pomegranate

A sane Clojure API for Maven Artifact Resolver + dynamic runtime modification of the classpath
Clojure
504
star
13

gloss

speaks in bytes, so you don't have to
Clojure
483
star
14

camel-snake-kebab

A Clojure[Script] library for word case conversions
Clojure
476
star
15

cljss

Clojure Style Sheets โ€” CSS-in-JS for ClojureScript
Clojure
455
star
16

clooj

clooj, a lightweight IDE for clojure
Clojure
421
star
17

byte-streams

A Rosetta stone for JVM byte representations
Clojure
417
star
18

durable-queue

a disk-backed queue for clojure
Clojure
381
star
19

useful

Some Clojure functions we use all the time, and so can you.
Clojure
365
star
20

metrics-clojure

A thin faรงade around Coda Hale's metrics library.
Clojure
343
star
21

virgil

Recompile Java code without restarting the REPL
Clojure
302
star
22

citrus

State management library for Rum
Clojure
274
star
23

ordered

Ordered sets and maps, implemented in pure clojure
Clojure
253
star
24

clj-ssh

SSH commands via jsch
Clojure
227
star
25

dirigiste

centrally-planned object and thread pools
Java
204
star
26

iapetos

A Clojure Prometheus Client
Clojure
176
star
27

primitive-math

for the discerning arithmetician
Clojure
170
star
28

humanize

Produce human readable strings in clojure
Clojure
156
star
29

digest

Digest algorithms (md5, sha1 ...) for Clojure
Clojure
156
star
30

clj-yaml

YAML encoding and decoding for Clojure
Clojure
120
star
31

byte-transforms

methods for hashing, compressing, and encoding bytes
Clojure
104
star
32

ring-buffer

A persistent ring-buffer in Clojure
Clojure
96
star
33

tentacles

An Octocat is nothing without his tentacles
Clojure
80
star
34

fs

File system utilities for Clojure. (forked from Raynes/fs)
Clojure
72
star
35

lein-marginalia

A Marginalia plugin to Leiningen
HTML
69
star
36

meta

A meta-repo for clj-commons discussions
46
star
37

ring-gzip-middleware

GZIP your Ring responses
Clojure
41
star
38

rewrite-cljs

Traverse and rewrite Clojure/ClojureScript/EDN from ClojureScript
Clojure
41
star
39

formatter

Building blocks and discussion for building a common Clojure code formatter
36
star
40

vizdeps

Visualize Leiningen dependencies using Graphviz
Clojure
33
star
41

zprint-clj

Node.js wrapper for ZPrint Clojure source code formatter
Clojure
13
star
42

infra

Infrastructure for clj-commons
Clojure
2
star
43

clj-commons.github.io

Clojure Commons Site
HTML
1
star