Skip to content

Rubocop

Lukas Krause edited this page Jun 20, 2024 · 6 revisions

Linters

Rubocop

We have two Rubocop setups, one for the rails application (located in src/api/) and one for all the other ruby code (located in the root directory of the repository). Each setup has two rake tasks, one for generating the Rubocop todo configuration and one for running Rubocop with this configuration.

All the Rubocop configuration that affects both, the rails code and the other ruby code, goes into .rubocop.yml. All configuration that only affects rails, like rails cops, goes into src/api/.rubocop.yml.

Rubocop tasks

Running Rubocop

This runs Rubocop with the current configuration.

rake dev:lint:rubocop:all # Run both Rubocop tasks
rake dev:lint:rubocop:rails
rake dev:lint:rubocop:root

Updating the Rubocop configuration

This updates the .rubocop_todo.yml file.

rake dev:lint:rubocop:auto_gen_config:all # Update both configurations
rake dev:lint:rubocop:auto_gen_config:rails
rake dev:lint:rubocop:auto_gen_config:root

Autocorrecting offenses

rake dev:lint:rubocop:autocorrect # Autocorrects offenses for both configurations

Running Rubocop in your own

Apart from using the rake task, you can as normally run Rubocop in your own. When running it from the src/api directory, do not forget to include the option --ignore-parent-exclusion. Otherwise the .rubocop.yml in the root directory will be loaded, consecuently excluding the src/api directory and not inspecting any file.

For example, if you want to autocorrect the offenses only in the src/api directory showing the name of the cops which produces the offenses, you could run from that directory:

bundle exec rubocop -aD --ignore-parent-exclusion

Making Fixes Easy to Review

Splitting the fixes per RuboCop offense makes this a lot easier to review and understand what happened. This PR is a good example.

Clone this wiki locally