Description
We will use a CLI tool to find unused code automatically 🔎🔎🔎.
Unused
Unused is a CLI tool to find unused code, written in 100% Haskell by awesome Josh Clayton.
Install Unused on macOS
$ brew tap joshuaclayton/formulae
$ brew install unused
Ctags for macOS
Install Ctags via homebrew instead of using system built-in older Ctags:
$ brew install ctags
If you having problem that your Ctags does not load homebrew-installed one, add an alias (zsh):
alias ctags="`brew --prefix`/bin/ctags"
Generate index file
For Ruby files here is an example:
$ ctags -f .git/tags -R $(git ls-files | grep .rb)
A .tags
file under .git
folder will be generated, which contains the index of your Ruby code. Don't forget to gitignore your tags file.
Find Unused Code
$ unused -s -g none
* -s,--single-occurrence Display only single occurrences
* -g,--group-by ARG [Allowed: directory, term, file, none] Group results
unused will do the hard work, find where unused code are, and report to you:
BulkCustomerDashboard 1, 1 app/dashboards/bulk_customer_dashboard.rb used once
UserSerializer 1, 1 app/serializers/user_serializer.rb used once
authorized_repos 1, 1 lib/github_api.rb used once
create_subscription_record 1, 1 app/services/repo_subscriber.rb used once
excluded_file? 1, 1 lib/ext/scss-lint/config.rb used once
has_something? 1, 1 spec/models/linter/ruby_spec.rb used once
register_email 1, 1 spec/models/linter/ruby_spec.rb used once
stub_commit 1, 1 spec/services/build_runner_spec.rb used once
stub_customer_with_discount_find_request 1, 1 spec/support/helpers/stripe_api_helper.rb used once
stubbed_style_checker_with_config_file 1, 1 spec/services/build_runner_spec.rb used once
token= 1, 1 app/models/user.rb used once
user_names 1, 1 spec/models/linter/ruby_spec.rb used once
verified_request? 1, 1 app/controllers/application_controller.rb used once
Wow, unused code found, delete them and then sent a Pull Request!
Happy Housekeeping! 🏡
Note that Unused is meant to guide, though, not give definitive answers to what can be removed.
-- Josh Clayton @joshuaclayton