2012-03-04 16:41:53 +00:00
|
|
|
# kibit
|
|
|
|
|
|
|
|
*There's a function for that!*
|
|
|
|
|
2012-04-01 16:44:26 +00:00
|
|
|
`kibit` is a static code analyzer for Clojure which uses
|
|
|
|
[`core.logic`](https://github.com/clojure/core.logic) to search for
|
|
|
|
patterns of code for which there might exist a more idiomatic function
|
|
|
|
or macro. For example if kibit finds the code
|
2012-03-04 16:41:53 +00:00
|
|
|
|
|
|
|
(if (some test)
|
|
|
|
(some action)
|
|
|
|
nil)
|
|
|
|
|
2012-03-05 08:54:31 +00:00
|
|
|
it will make the suggestion to use the `when` macro instead of `if`.
|
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
|
|
|
|
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
|
|
|
|
inside Emacs with all the fancyness you are used from `M-x compile`.
|
|
|
|
Put the following into your `~/.emacs`:
|
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"))
|
|
|
|
|
2013-03-05 22:20:04 +00:00
|
|
|
(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)))
|
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
|
|
|
|
therefor 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.
|
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
|
|
|
|
|
|
|
Bugs can be reported using the github bug tracker.
|
|
|
|
|
2012-03-05 17:46:03 +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-05 17:46:03 +00:00
|
|
|
|
2012-03-04 16:41:53 +00:00
|
|
|
## TODO
|
|
|
|
|
2012-03-05 17:46:03 +00:00
|
|
|
* Leiningen project.clj setting for rule exclusion
|
|
|
|
* Leiningen project.clj setting for a directory of rules to include
|
2012-03-22 01:32:34 +00:00
|
|
|
* Analyse ClojureScript files
|
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.
|
2012-03-05 17:46:03 +00:00
|
|
|
|