kibit/README.md

105 lines
3.1 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)
2012-03-04 16:41:53 +00:00
# kibit
*There's a function for that!*
2014-11-08 08:53:08 +00:00
`kibit` is a static code analyzer for Clojure. It uses
2012-04-01 16:44:26 +00:00
[`core.logic`](https://github.com/clojure/core.logic) to search for
2014-11-08 08:53:08 +00:00
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
2013-03-09 19:48:13 +00:00
Add `[lein-kibit "0.0.8"]` to your `:plugins` vector in your `:user`
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
2012-11-11 13:41:51 +00:00
to analyze your namespaces. You can analyze individual files by
running
$ lein kibit path/to/some/file.clj
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/).
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.