kibit/README.md

177 lines
5.3 KiB
Markdown
Raw Normal View History

2014-09-24 10:50:17 +00:00
[![Build Status](https://travis-ci.org/jonase/kibit.svg?branch=master)](https://travis-ci.org/jonase/kibit)
[![Dependencies Status](http://jarkeeper.com/jonase/kibit/status.svg)](http://jarkeeper.com/jonase/kibit)
2014-09-24 10:50:17 +00:00
2012-03-04 16:41:53 +00:00
# kibit
*There's a function for that!*
2014-11-25 02:10:57 +00:00
`kibit` is a static code analyzer for Clojure, ClojureScript, [cljx](https://github.com/lynaghk/cljx)
and other Clojure variants. It uses [`core.logic`](https://github.com/clojure/core.logic)
to search for patterns of code that could be rewritten with a more idiomatic function
2012-04-01 16:44:26 +00:00
or macro. For example if kibit finds the code
2012-03-04 16:41:53 +00:00
2013-11-06 06:52:15 +00:00
```clojure
(if (some test)
(some action)
nil)
2014-11-22 07:57:41 +00:00
```
2012-03-04 16:41:53 +00:00
2014-11-08 08:53:08 +00:00
it will suggest using `when` instead:
```clojure
(when (some test)
(some action))
```
2012-03-04 16:41:53 +00:00
## Usage
Add `[lein-kibit "0.1.0"]` to your `:plugins` vector in your `:user`
2013-03-09 19:48:13 +00:00
profile. Then you can run
2012-03-04 16:41:53 +00:00
2012-03-04 22:09:59 +00:00
$ lein kibit
2012-03-04 16:41:53 +00:00
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:
2014-11-25 02:10:57 +00:00
$ 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
2012-11-11 13:41:51 +00:00
2012-03-04 16:41:53 +00:00
2013-04-18 18:28:13 +00:00
If you want to know how the Kibit rule system works there are some slides available at [http://jonase.github.io/kibit-demo/](http://jonase.github.io/kibit-demo/).
2014-11-25 02:10:57 +00:00
## Exit codes
If `lein kibit` returns any suggestions to forms then it's 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
2015-04-20 01:37:36 +00:00
## Automatically rerunning when files change
You can use [lein-auto](https://github.com/weavejester/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.
2014-11-23 21:29:12 +00:00
## 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")))
```
2014-11-25 02:10:57 +00:00
2014-11-23 21:29:12 +00:00
----
##### `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:
```clojure
(when true (println "hi"))
```
instead of:
```clojure
(if true (do (println "hi")))
```
...
----
2012-05-31 13:38:50 +00:00
### Usage from inside Emacs
2012-06-11 04:43:23 +00:00
If you use Emacs for hacking Clojure, here's a way to use kibit from
2014-11-08 08:53:08 +00:00
inside Emacs with all the fanciness you are used to from `M-x compile`.
2012-06-11 04:43:23 +00:00
Put the following into your `~/.emacs`:
2012-05-31 13:38:50 +00:00
2013-11-06 06:52:15 +00:00
```clojure
2012-05-31 13:38:50 +00:00
;; Teach compile the syntax of the kibit output
(require 'compile)
(add-to-list 'compilation-error-regexp-alist-alist
'(kibit "At \\([^:]+\\):\\([[:digit:]]+\\):" 1 2 nil 0))
(add-to-list 'compilation-error-regexp-alist 'kibit)
2012-06-11 04:43:23 +00:00
;; A convenient command to run "lein kibit" in the project to which
;; the current emacs buffer belongs to.
2012-05-31 13:38:50 +00:00
(defun kibit ()
"Run kibit on the current project.
Display the results in a hyperlinked *compilation* buffer."
(interactive)
(compile "lein kibit"))
(defun kibit-current-file ()
"Run kibit on the current file.
Display the results in a hyperlinked *compilation* buffer."
(interactive)
(compile (concat "lein kibit " buffer-file-name)))
2014-11-22 07:57:41 +00:00
```
2012-06-11 04:47:18 +00:00
This will give you a new command `M-x kibit RET`, and the properly
2012-06-11 04:43:23 +00:00
highlighted and hyperlinked kibit output is presented in a
`*compilation*` buffer.
## Known limitations
Kibit
[reads](http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/read)
source code without any macro expansion or evaluation. A macro can
2014-11-08 08:53:08 +00:00
therefore easily invalidate a rule. Also, kibit will not know if the
2012-06-11 04:43:23 +00:00
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.
2012-05-31 13:38:50 +00:00
2012-03-04 16:41:53 +00:00
## Contributing
2012-03-04 18:42:20 +00:00
It is very easy to write new patterns for `kibit`. Take a look at
2012-04-01 18:56:28 +00:00
[`control-structures.clj`](https://github.com/jonase/kibit/blob/master/src/kibit/rules/control_structures.clj)
2012-03-04 19:01:33 +00:00
to see how new patterns are created. If you know of a recurring
2012-03-04 18:42:20 +00:00
pattern of code that can be simplified, please consider sending me a
pull request.
2012-03-04 16:41:53 +00:00
2014-11-22 07:57:41 +00:00
Bugs can be reported using the GitHub [issue tracker](https://github.com/jonase/kibit/issues/).
2012-03-04 16:41:53 +00:00
## Contributors
2013-01-26 06:29:49 +00:00
Thanks to all who have [contributed](https://github.com/jonase/kibit/graphs/contributors) to kibit!
2012-03-04 16:41:53 +00:00
## TODO
* Leiningen project.clj setting for rule exclusion
* Leiningen project.clj setting for a directory of rules to include
2012-03-04 16:41:53 +00:00
## License
2012-03-04 22:09:59 +00:00
Copyright © 2012 Jonas Enlund
2012-03-04 16:41:53 +00:00
Distributed under the Eclipse Public License, the same as Clojure.