Fast rubocop autocorrection alias

Posted 11 months ago. Visible to the public. Deprecated.

Parallel autocorrection is now possible and the default since Rubocop 1.32

https://github.com/rubocop/rubocop/releases/tag/v1.32.0

The rubocop binary has a few interesting flags:

  • rubocop (using the --parallel default ) scans the current repository for linting issues while using multiple CPU cores
  • rubocop -a (or --autocorrect) safely corrects most offenses while doing a sequential scan
  • rubocop -A (or --autocorrect-all) also tries to correct unsafe suggestions

Autocorrection takes significantly longer on large projects because of the sequential nature.
To speed things up, you can use the following alias. It first checks in parallel if any files need to be corrected, then passes them to the autocorrection command:

alias fixcops="bundle exec rubocop -p | grep -P \"\A[^:]+(?=:\d+:\d+: )\" --only-matching | xargs --no-run-if-empty bundle exec rubocop -a"
  • bundle exec rubocop -p quickly scans for issues, listing each affected file
  • grep -P \"\A[^:]+(?=:\d+:\d+: )\" --only-matching searches for those file names
  • xargs --no-run-if-empty bundle exec rubocop -a only runs autocorrection on selected files if there are any
Michael Leimstädtner
Last edit
11 months ago
Florian Leinsinger
License
Source code in this card is licensed under the MIT License.
Posted by Michael Leimstädtner to makandra dev (2023-06-19 08:49)