Last Updated on October 21, 2020 by Christopher G Mendla
Rubocop is a code linter for Ruby on Rails. When installed, configured and used properly it ensures that code is formatted properly and will identify some security issues. This post will cover installation and fixing basic auto-correctable errors.Â
Why Rubocop?
There are a number of other tools that can be used for code linting such as Reek, SonarQube, RSpec, and ESLint. However, Rubocop is one of the more popular tools. Almost any mid-sized to large company will require Rubocop in order to build to the test, stage and production environments.
I have seen smaller companies and projects that do not integrate lint checking into their development process. the result is ugly, sloppy code.
What does Rubocop check?
Rubocop can be used with the default set of “Cops” or it can be configured by the user or organization. It will check for things such as:
- Proper indentation
- Trailing spaces
- Proper spacing with brackets
- Duplicate gems
- Block alignment
- Block Length
- Class Length
- … and more
Installation
The installation instructions can be found at the Rubocop Repo. The simplest way is to do a `gem install rubocop’. Note – you may need to reinstall it when you upgrade your Ruby version.
Usage.
This is where things get a little muddy. I found a workflow that seems to work .
You run Rubocop from the command line. There are a number of command line parameters you can use.
The simplest way
Just type ‘rubocop’ in your project’s root folder. That will show you a list of all of the errors. Unfortunately the list isn’t that friendly to work with.
Set up rubocop.yml and rubocop_todo.yml files.
Generate a blank rubocop.yml file
Run rubocop --init
as per the command line parameters referenced above. This will generate a default rubocop.yml file in the root of your project. Note – rubocop commands should always be run in the root. Notice that the first line of this file is
inherit_from: .rubocop_todo.yml
That requires that we generate the rubocop_todo.yml file
Generate the rubocop_todo.yml file
Run rubocop --auto-gen-config
. This will create the rubocop_todo.yml file.
Correct the errors
WARNING – using autocorrect in the non-safe mode can break your application. Run your tests and check the app as you make corrections
Open the rubocop_todo.yml file
This is the file that will be your todo file. You will see a number of blocks similar to the one below. Each block is a cop that found violations
# Offense count: 22
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: normal, indented_internal_methods
Layout/IndentationConsistency:
Exclude:
- 'Gemfile'
- 'app/controllers/application_controller.rb'
- 'app/controllers/contacts_controller.rb'
- 'app/controllers/pages_controller.rb'
- 'config/environments/production.rb'
- 'config/routes.rb'
The first line tells you how many errors it found for that particular cop.
The second line tells you if you can auto-correct.
The fourth line tells you which cop found the violation.
The files listed under ‘Exclude are the files with issues.
An important concept is that this file acts as a config file that EXCLUDES these errors from showing up the next time you run Rubocop. It is assuming that you will be working on the errors that were identified. You need to manually work through this file to fix the errors.
Run SAFE autocorrect
There are two autocorrect options:
-a
which is a SAFE autocorrect. This SHOULD not break anything when you run it.A
is a non safe autocorrect. This can (AND WILL) break things. Of course you are using versioning control and a branch , right?
Remove the exclude.
You need to tell rubocop to stop ignoring the block of errors you are working on. You do that by commenting out the Exclude line and the line above it as shown below. You don’t need to do anything with the file list.
# Offense count: 22
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: normal, indented_internal_methods
# Layout/IndentationConsistency:
# Exclude:
- 'Gemfile'
- 'app/controllers/application_controller.rb'
- 'app/controllers/contacts_controller.rb'
- 'app/controllers/pages_controller.rb'
- 'config/environments/production.rb'
- 'config/routes.rb'
Run rubocop
You can simply run rubocop
without the -a
parameter. This will allow you to see what it is going to correct. You will also see message as to how many errors can be autocorrected. The example below is from a different block than shown above.
Inspecting 41 files
C....................C...................
Offenses:
Gemfile:32:121: C: Layout/LineLength: Line is too long. [155/120]
gem 'spring', '~> 2.1.1' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
config/environments/production.rb:74:5: C: [Corrected] Layout/HashAlignment: Align the keys of a hash literal if they span more than one line.
:address => 'smtp.sendgrid.net',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[some code removed for clarity
41 files inspected, 8 offenses detected, 7 offenses corrected, 1 more offense can be corrected with `rubocop -A`
chris@chris-VirtualBox:~/Documents/Projects/christopherg$
We are seeing that Rubocop found 8 offenses and can autocorrect 7.
My plan was to do a branch or two to fix all of the issues that can be fixed with the safe autocorrect, then go back and work on the issues that need to be manually resolved. That will be addressed in another post.
Run rubocop autocorrect
Now we can run rubocop -a
which will autocorrect that group of errors for the errors that are safe for autocorrect.
Run your tests and check the app.
If you have tests, run your test suite. It is also a good idea to run the application and check to make sure everything is functioning.
Commit the branch, push and deploy
If everything checks out, in other words if your app isn’t a smoking ruin, commit the branch. Then push it, merge it with your master and deploy.
Rebuild your rubcocop_todo file.
You can rebuild your todo file by running `rubocop –regenerate-todo’
That will rebuild the todo file. The items you fixed should be gone which makes it easier to work with.
To check in to the repo or to not check in.
In most cases, you will probably want to track the rubocop.yml
and rubocop_todo.yml
files. As always, you should consider your unique needs in order to determine if a file should be tracked.
Summary
Rubocop will help de-uglify your code and will check for some security issues. It is worth installing and running even in small rails projects.
Rubocop isn’t just for production apps. It could also be useful for coding challenges or homework assignments.