Using RuboCop with Jekyll

This post was very helpful to get started: https://joanswork.com/rubocop-rails-getting-started/

Install RuboCop

Add the gem to the Gemfile:

Gemfile
gem 'rubocop'

then

$ bundle install

Setup RuboCop Config

Add an empty .rubocop.yml file to the root of the project

$ touch .rubocop.yml

Running rubocop now shows a whole lot of violations:

$ bundle exec rubocop -D

(prints out a bunch of stuff)

16 files inspected, 230 offenses detected

Use rubocop to make this process easier:

$ bundle exec rubocop --auto-gen-config

(prints a lot of stuff)

16 files inspected, 245 offenses detected
Created .rubocop_todo.yml.
Run `rubocop --config .rubocop_todo.yml`, or add `inherit_from: .rubocop_todo.yml` in a .rubocop.yml file.

That creates a .rubycop_todo.yml file, that contains config to disable all of the checks that failed

Like the message says, add inherit_from: .rubocop_todo.yml at the top of the .rubocop.yml file, so it looks like this now:

.rubocop.yml
inherit_from: .rubocop_todo.yml

Now there should be no violations when we run this:

$ bundle exec rubocop -D
Inspecting 16 files
.....E..........

Offenses:

scripts/reddit-api.rb:54:80: E: unexpected token tCOLON
(Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
refresh_response = RestClient.post(REFRESH_URL, refresh_payload, { 'User-Agent': USER_AGENT, })
                                                                               ^
scripts/reddit-api.rb:73:19: E: unexpected token tCOLON
(Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
    { 'User-Agent': USER_AGENT, 'Authorization': "bearer #{bearer_token}" }
                  ^
scripts/reddit-api.rb:73:48: E: unexpected token tCOLON
(Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
    { 'User-Agent': USER_AGENT, 'Authorization': "bearer #{bearer_token}" }
                                               ^
scripts/reddit-api.rb:73:75: E: unexpected token tRCURLY
(Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
    { 'User-Agent': USER_AGENT, 'Authorization': "bearer #{bearer_token}" }
                                                                          ^

16 files inspected, 4 offenses detected

Wrong! This is setup for the wrong version of Ruby

Fix that in .rubocop.yml, which should now look like:

.rubocop.yml
inherit_from: .rubocop_todo.yml

AllCops:
  TargetRubyVersion: 2.3

Since that was changed, need to regen the config:

$ bundle exec rubocop --auto-gen-config
Inspecting 16 files
CCCCCCCCCCCCCCCC

Offenses:

(bunch of printouts)

16 files inspected, 279 offenses detected
Created .rubocop_todo.yml.
Run `rubocop --config .rubocop_todo.yml`, or add `inherit_from: .rubocop_todo.yml` in
a .rubocop.yml file.

Now all the failing checks should be disabled, and running rubocop should show no errors:

$ bundle exec rubocop
Inspecting 16 files
................

16 files inspected, no offenses detected

Fix Style Violations

Do this over and over again:

Periodically run bundle exec rake test to make sure I haven’t screwed anything up

Once I’ve fixed everything, delete the .rubocop_todo.yml file - it’s no longer needed

Notable Errors

I had to look these up to figure out what they meant or how to easily fix them

“Assignment Branch Condition size for run is too high. [39.43/15]”

Means that the method is too complicated, and does too much

I did some refactoring, but eventually agreed to disagree with this style:

.rubocop.yml
# JekyllTemplateTests is too long and complicated - whatever
Metrics/ClassLength:
  Max: 110
Metrics/AbcSize:
  Max: 40
Metrics/MethodLength:
  Max: 40

“Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.”

This is a new feature in Ruby 2.3

Fix that with

$ bundle exec rubocop --auto-correct --only FrozenStringLiteralComment

After doing that, I get this error:

“Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.”

Fix that with

bundle exec rubocop --auto-correct --only EmptyLineAfterMagicComment

“Style/TrailingCommaInLiteral: Avoid comma after the last item of an array.”

I do this all the time, but ok rubocop, have it your way

bundle exec rubocop --auto-correct --only TrailingCommaInLiteral

Finally, run RuboCop as part of the tests

Add a line like this to the Rakefile

Rakefile
sh 'bundle exec rubocop'

Now this will run rubocop when I run

$ bundle exec rake test

locally, or when I push changes and they are tested in Travis CI