ChrisMendlaTech
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.
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.
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:
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.
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.
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.
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
rubocop --init
inherit_from: .rubocop_todo.yml
That requires that we generate the rubocop_todo.yml file
Run rubocop --auto-gen-config . This will create the rubocop_todo.yml file.
rubocop --auto-gen-config
WARNING – using autocorrect in the non-safe mode can break your application. Run your tests and check the app as you make corrections
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.
There are two autocorrect options:
-a
A
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'
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.
rubocop
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` [email protected]:~/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.
Now we can run rubocop -a which will autocorrect that group of errors for the errors that are safe for autocorrect.
rubocop -a
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.
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.
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.
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.
rubocop.yml
rubocop_todo.yml
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.
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Post Comment
created with