• Stars
    star
    275
  • Rank 149,796 (Top 3 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Remove unused Clojure vars

Carve

Clojars Project CircleCI bb compatible

Carve out the essentials of your Clojure app.

Rationale

Carve will search through your code for unused vars and will remove them.

Installation

Add to your deps.edn:

:aliases {
  ...
  :carve {:extra-deps {io.github.borkdude/carve {:git/url "https://github.com/borkdude/carve"
                                                 :git/sha "<SHA>"}}
          :main-opts  ["-m" "carve.main"]}
  ...
}

or to your bb.edn:

io.github.borkdude/carve {:git/url "https://github.com/borkdude/carve"
                          :git/sha "<SHA>"}

where the latest SHA can be found with:

$ git ls-remote https://github.com/borkdude/carve.git refs/heads/master

Babashka script

You can install carve as a babashka script that you can invoke as carve with:

$ bbin install io.github.borkdude/carve

See bbin for details.

Clojure tool

To use as a clojure tool:

$ clj -Ttools install io.github.borkdude/carve '{:git/tag "v0.3.5"}' :as carve

How does it work?

Carve invokes clj-kondo and uses the analysis information to check which vars are unused. To remove the relevant bits of code it uses rewrite-cljc.

Usage

The usage for a typical Clojure app looks like:

Babashka

To see help:

bb -x carve.api/carve! --help

To run with options:

bb -x carve.api/carve! --paths src test

Clojure

On the JVM:

clojure -M:carve --paths src test

You may provide options as an EDN literal with --opts, e.g.:

clojure -M:carve --opts '{:paths ["src" "test"] :report {:format :text}}'

To also load the config file in .carve/config.edn when passing --opts, set :merge-config:

clojure -M:carve --opts '{:interactive true :merge-config true}'

Clojure tool

As a clojure tool:

$ clj -Tcarve carve! '{:paths ["src"] :report true :report-format :text}'

Options

Run carve --help to see options.

You can also store the config for your project in .carve/config.edn. When invoking carve with no options, the options in .carve/config.edn will be used. When providing options, the CLI options will take precedence over the configuration in .carve/config.edn and the file will be ignored. Pass :merge-config from the CLI to also load and merge .carve/config.edn.

All options:

  • :paths: a list of paths to analyze. Can be a mix of individual files and directories.
  • :ignore-vars: a list of vars to ignore. Useful for when the analyzer has it wrong or you just want to keep the var for whatever reason.
  • :api-namespaces: a list of namespaces of which only unused private vars will be reported.
  • :carve-ignore-file: a file where ignored vars can be stored, .carve/ignore by default.
  • :interactive: ask what to do with an unused var: remove from the file, add to .carve/ignore or continue. Set to true by default.
  • :merge-config: Merge the CLI options and the config file together. Default is false.
  • :out-dir: instead of writing back to the original file, write to this dir.
  • :dry-run: just print the unused var expression.
  • :aggressive: runs multiple times until no unused vars are left. Defaults to false.
  • :report: when truthy, prints unused vars to stdout. Implies :dry-run true. The output format may be set using :report {:format ...} where format can be :edn, :text or :ignore. The text output can be interpreted by editors like Emacs. This option can be combined with :aggressive.
  • :silent: when truthy, does not write to stdout. Implies :interactive false.
  • :clj-kondo/config: a map of clj-kondo config opts that are passed on to clj-kondo, which is used to analyze usages. e.g.: passing {:skip-comments true} will ignore function usage in (comment) forms. Note that the config in .clj-kondo/config.edn is used as well - options passed with this key will override options set in the clj-kondo config file.
$ clojure -M:carve --paths "test-resources" --dry-run true
Carving test-resources/app.clj

Found unused var:
(defn unused-function [])

...

Carving test-resources/api.clj

Found unused var:
(defn- private-lib-function [])

...
$ clojure -M:carve --paths "test-resources"
Carving test-resources/app.clj

Found unused var:
(defn unused-function [])

Type Y to remove or i to add app/unused-function to .carve/ignore
n
Found unused var:
(defn another-unused-function [])

Type Y to remove or i to add app/another-unused-function to .carve/ignore
i
...

$ cat .carve/ignore
app/another-unused-function

Keep in mind that if you ran carve with {:paths ["src" "test"]}, there might still be potentially lots of unused code, which wasn't detected simply because there are tests for it.

So after a first cycle of carving you might want to do another run with simply {:paths ["src"]}, which will help deleting the rest of the unused code. Just beware that this will break all the tests using the code you just deleted, and you'll have to fix/delete them manually.*

Carve also removes any unused refers from namespace :require forms. This means any unused refer, not just refers for functions determined to be unused by carve.

CI integration

A good use case for Carve is the CI integration, to ensure that no one can introduce dead code into a codebase. This example shows how to add this step into CircleCI, but any other CI configuration will be similar.

First add this configuration into a .circleci/deps.edn file:

{:aliases
 {:carve {:extra-deps {io.github.borkdude/carve {:git/sha "$LATEST_CARVE_SHA"}}
          :main-opts ["-m" "carve.main"]}}}

Then configure your build step like this:

find_dead_code:
  working_directory: ~/$your-project
  docker:
    - image: circleci/clojure:openjdk-11-tools-deps

  steps:
    - checkout
    - run: mkdir -p ~/.clojure && cp .circleci/deps.edn ~/.clojure/deps.edn
    - run: clojure -M:carve --opts '{:paths ["src" "test"] :report {:format :text}}'

If the report step finds any dead code it exits with status code 1, thus failing the build step.

Emacs

carve.el

A simple emacs integration is provided by carve.el.

It lets you run carve with a simple key binding and opens a result buffer where you can navigate to the results and add them to your ignore file with a single keystroke.

Report mode

Running carve with in report mode (for example clojure -M:carve --paths src test --report true --report-format :text}') you can make all the links clickable by switching to compilation-mode.

Using :report-format :ignore returns the list of unused vars in the same format as .carve/ignore so you can create the initial ignore file or append to an existing one. For example with:

carve --paths "src" "test" --report true --report-format :ignore' > .carve/ignore

Articles

Dev

Running tests

If you want to run tests in Emacs and Cider you need to use the test alias, or it will fail while trying to load the test.check library. You can place this in your .dir-locals.el file in the root directory to always use the test alias:

((clojure-mode . ((cider-clojure-cli-global-options . "-R:test"))))

or alter the command used by cider-jack-in by prefixing the invocation with C-u.

License

Copyright ยฉ 2019-2023 Michiel Borkent

Distributed under the EPL License. See LICENSE.

More Repositories

1

jet

CLI to transform between JSON, EDN, YAML and Transit using Clojure
Clojure
584
star
2

grasp

Grep Clojure code using clojure.spec regexes
Clojure
233
star
3

deps.clj

A faithful port of the clojure CLI bash script to Clojure
Clojure
225
star
4

speculative

Unofficial community-driven specs for clojure.core
Clojure
187
star
5

edamame

Configurable EDN/Clojure parser with location metadata
Clojure
155
star
6

clojure-rust-graalvm

An example of Clojure program calling a Rust library, all combined into one executable using GraalVM.
Rust
120
star
7

re-find

Find functions by matching specs
Clojure
118
star
8

quickblog

Light-weight static blog engine for Clojure and babashka
Clojure
117
star
9

quickdoc

Quick and minimal API doc generation for Clojure
Clojure
116
star
10

bebo

Run Clojure scripts on deno
Clojure
98
star
11

flycheck-clj-kondo

Emacs integration for clj-kondo via flycheck
Emacs Lisp
84
star
12

rewrite-edn

Utility lib on top of rewrite-clj with common operations to update EDN while preserving whitespace and comments.
Clojure
76
star
13

clj2el

Transpile Clojure to Emacs Lisp!
Clojure
63
star
14

glam

A cross-platform package manager.
Clojure
62
star
15

api-diff

Print API diffs between library versions
Clojure
59
star
16

deflet

Make let-expressions REPL-friendly!
Clojure
57
star
17

cljtree-graalvm

Tree version in Clojure built with GraalVM
Clojure
53
star
18

respeced

Testing library for clojure.spec fdefs
Clojure
52
star
19

dynaload

The dynaload logic from clojure.spec.alpha as a library
Clojure
52
star
20

jayfu

Jayfu is a tutorial on how to create a Clojure CLI with GraalVM native-image and SCI.
Clojure
52
star
21

lein2deps

Lein project.clj to deps.edn converter
Clojure
50
star
22

blog

HTML
49
star
23

plsci

PostgreSQL procedural language handler for Clojure via SCI
Rust
48
star
24

advent-of-cljc

Cross platform Clojure Advent of Code solutions
Clojure
44
star
25

boot-bundle

boot-bundle: managed dependencies for boot, the clojure build tool
Clojure
43
star
26

deps-infer

Infer mvn deps from sources
Clojure
39
star
27

puget-cli

A CLI version of puget
Shell
37
star
28

specter-cli

A native Specter CLI, compiled with GraalVM native-image and executed by SCI.
Clojure
33
star
29

draggable-button-in-reagent

Very simple example of a draggable button in Reagent
Clojure
31
star
30

spartan.spec

A spartan version of clojure.spec compatible with babashka
Clojure
30
star
31

lein-new-liberagent

leiningen template for apps that use reagent and liberator
Clojure
29
star
32

re-find.web

Web version of re-find
Clojure
25
star
33

nbb-action-example

An example of writing a Github action with nbb
Clojure
24
star
34

lein2boot

The goal of this exercise is to convert a leiningen project to boot and have exactly the same workflow.
Clojure
24
star
35

gh-release-artifact

Upload artifacts to Github releases idempotently
Clojure
24
star
36

balcony

Should I water my balcony?
Clojure
23
star
37

generate-podcast

A babashka script to create a podcast from a local directory with mp3 files
Clojure
22
star
38

tools

Tools
Clojure
19
star
39

fly_io_clojure

A fly.io example for Clojure
Clojure
17
star
40

refl

Clean up generated reflection configs for GraalVM native-image compiled Clojure programs
Clojure
17
star
41

analyze-reify

Analyze occurrences of reify in Clojure code. Implemented using tree-sitter-clojure and Rust.
Rust
17
star
42

advent-of-babashka

Advent of Code using babashka and nbb
Clojure
16
star
43

aoc2017

Advent of Code 2017
Clojure
15
star
44

lein-new-wrom

Webjars + Ring + Om leiningen template
Clojure
15
star
45

clj-reflector-graal-java11-fix

A fix for an issue with clojure.lang.Reflector in GraalVM native-image JDK11.
Clojure
15
star
46

cljs-showcase

Clojure
13
star
47

clj.el

Clojure-like macros and functions in elisp
Emacs Lisp
13
star
48

balcony-hs

Should I water my balcony?
Haskell
13
star
49

babashka-docker-action-example

Dockerfile
12
star
50

who-follows-me

Check who follows you, but you're not following back and vice versa. https://twitter.michielborkent.nl
Clojure
12
star
51

trickle-playground

A Truffle Clojure Interpreter (playground)
Java
12
star
52

finitize

Limit and realize possibly infinite seqs
Clojure
12
star
53

nrepl-server

Proof of concept nREPL server
Clojure
10
star
54

full-stack-clojure-han-okt-2015

Talk about developing full stack Clojure applications at HAN University of Applied Sciences
Clojure
10
star
55

clojurecursus

Introductiecursus Functioneel Programmeren met Clojure
CSS
10
star
56

missing.test.assertions

A library that detects missing test assertions in clojure.test tests
Clojure
9
star
57

deps.add-lib

Clojure 1.12's add-lib feature for leiningen and/or other environments without a specific version of the clojure CLI
Clojure
8
star
58

figwheel-keep-om-turning

Code for blog post at http://blog.michielborkent.nl/2014/09/25/figwheel-keep-Om-turning/
Clojure
8
star
59

cljtree-planck

A ClojureScript version of `tree` in Planck
Shell
6
star
60

bun-squint-loader

JavaScript
6
star
61

simple-cljs-examples

Curated list of ClojureScript sample applications suited for those starting with ClojureScript
6
star
62

graal.locking

A workaround for CLJ-1472 as a library
Clojure
6
star
63

tictactoe

tictactoe (clojure)
Clojure
5
star
64

bb-cherry-example

A web app showing real time JS compilation with babashka and cherry
Clojure
5
star
65

immutable-webapp

Immutable Webapp!
HTML
5
star
66

spartan.test

A spartan test framework compatible with babashka.
Clojure
5
star
67

speculative-kaocha-plugin

speculative kaocha plugin
Clojure
5
star
68

aoc2015_day7

Solution of Advent of Code Day 7 using clojure.spec
Clojure
4
star
69

oredev2014

Slides and code for ร˜redev 2014
Clojure
4
star
70

tictactoe-cljs

Tictactoe in Clojurescript and Om
Clojure
4
star
71

cherry-action-example

Github action implemented with cherry
Clojure
4
star
72

homebrew-brew

Homebrew formulas
Shell
4
star
73

datalevin-native

Clojure
4
star
74

sci-wallpaper-downloader

A port of @yogthos's wallpaper downloader to the Small Clojure Interpreter on NodeJS
Clojure
4
star
75

domcode-cljs-react

Slides and code for presentation at the DomCode meetup
Clojure
4
star
76

clj-jdbc

A hypothetical Clojure command tool focusing around SQL interaction via JDBC
Clojure
4
star
77

sci-birch

tree CLI using sci, ported from lambdaisland's birch
Clojure
3
star
78

clj-native-sound-demo

A demo of using the Java sound API with GraalVM native-image
Clojure
3
star
79

bb-php-guestbook

A guestbook using babashka and PHP
Clojure
2
star
80

michielborkent.nl

Home page sources
Clojure
2
star
81

clj-kondo-configurator

CLI tool to merge clj-kondo configurations.
Clojure
2
star
82

boot.bundle.edn

My boot.bundle.edn file
Clojure
2
star
83

my-aob

Clojure
2
star
84

FP-AMS-ClojureScript-talk

Slides and code for talk at FP AMS about Clojurescript
Clojure
2
star
85

react-amsterdam

Talk for React-Amsterdam, February 12th 2015
Clojure
2
star
86

pprint

Clojure
2
star
87

sytac-core-async

Slides and code for presentation at Sytac Devjam
Clojure
2
star
88

SnakeYAML

Temporary fork of SnakeYAML
Java
1
star
89

leiningen.org

Leiningen's web site
Clojure
1
star
90

try_git

1
star
91

clj-1472-repro

A repro of CLJ-1472
Clojure
1
star
92

cljs-macro

Example of usage of a macro in ClojureScript.
Clojure
1
star
93

itunes2dropbox

Share the current playing song or selected songs from iTunes to your Dropbox public folder.
AppleScript
1
star
94

squint-bun-cloudflare

JavaScript
1
star
95

aoc2016

Advent of Clojure 2016
Clojure
1
star
96

fp-hu-may-2016

Talk about Functional Programming at Hogeschool Utrecht, May 2016
Clojure
1
star
97

Clojure-FinalAssignment

Quiz
Clojure
1
star
98

ns-parser

Helper library for parsing ns forms in combination with edamame and rewrite-clj
Clojure
1
star
99

clj-kondo.web

playground for clj-kondo
Clojure
1
star
100

test-repo

Repo to test Github features
Clojure
1
star