Install Rubocop by adding it to your Gemfile:
gem 'rubocop'
gem 'rubocop-rails'
And running:
bundle install
Place the following .rubocop.yml
file in your project root or in your WSL home folder, overwriting any existing file if one was already present. (Note: Your WSL home folder is where you navigate if you run: cd ~
)
The config file:
AllCops:
Exclude:
- db/migrate/**/*
- db/schema.rb
- config/**/*
- script/**/*
- bin/**/*
- test/**/*
- app/admin/**/*
- app/channels/**/*
- app/jobs/**/*
- node_modules/**/*
- Gemfile
- Rakefile
- config.ru
require:
- rubocop-rails
Style/Encoding:
Enabled: false
Style/Documentation:
Description: 'Document classes and non-namespace modules.'
Enabled: false
Style/InlineComment:
Description: 'Avoid inline comments.'
Enabled: false
Layout/LineLength:
Description: 'Limit lines to 100 characters. (Default is 80)'
Max: 100
Style/FrozenStringLiteralComment:
Description: To help transition from Ruby 2.3.0 to Ruby 3.0.
Enabled: false
Style/WordArray:
Description: 'Use %w or %W for arrays of words.'
Enabled: false
# Defaults all strings to double quotes. Less performant, but
# nicer for consistency, and for adding interpolation later.
Style/StringLiterals:
EnforcedStyle: double_quotes
# Prettier hashes.
Layout/HashAlignment:
EnforcedHashRocketStyle: table
EnforcedColonStyle: table
# No auto-correct for unused block arguments,
# but will still warn.
Lint/UnusedBlockArgument:
AutoCorrect: false
# No auto-correct for unused method arguments,
# but will still warn.
Lint/UnusedMethodArgument:
AutoCorrect: false
Rails/HasAndBelongsToMany:
Enabled: false
Wih the config file in place run the following command from your project root:
bundle exec rubocop
You can also auto-correct all sorts of errors with:
bundle exec rubocop --safe-auto-correct
If you are seeing lots of error please check to see if you properly saved the rubocop config file to you project root directory or to your home folder (not your Windows home, but the WSL one: ~/.rubocop.yml
).
Feel free to add other folders and files to the Exclude
section if they don't contain code that you personally wrote.
If you are getting rubocop errors due to ABC Size or Cyclomatic Complexity you can disable these. Just be sure to note that these are indicators that your methods are growing overly complex. :)
Disabling others 'cops' is also allowed if you obtain approval from your instructor.