Quickly setup Rubocop on a Rails app:
bundle add rubocop && printf "AllCops:\n NewCops: enable\n SuggestExtensions: false\n\nStyle/Documentation:\n Enabled: false\n\nStyle/FetchEnvVar:\n Enabled: false\n\nMetrics/MethodLength:\n Enabled: false\n\nLayout/LineLength:\n Max: 120\n\nMetrics/BlockLength:\n Enabled: false\n\n\nLint/EmptyBlock:\n Enabled: false\n\n" >> .rubocop.yml && printf '#!/usr/bin/env ruby\n# frozen_string_literal: true\n\n#\n# This file was generated by Bundler.\n#\n# The application 'rubocop' is installed as part of a gem, and\n# this file is here to facilitate running it.\n#\n\nrequire "pathname"\nENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",\n Pathname.new(__FILE__).realpath)\n\nbundle_binstub = File.expand_path("../bundle", __FILE__)\n\nif File.file?(bundle_binstub)\n if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/\n load(bundle_binstub)\n else\n abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.\nReplace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")\n end\nend\n\nrequire "rubygems"\nrequire "bundler/setup"\n\nload Gem.bin_path("rubocop", "rubocop")' >> bin/rubocop && chmod 0755 bin/rubocop && git add . && git commit -m "adds rubocop" && (bin/rubocop -A) || git add . && git commit -m "rubocop autocorrections"
Run Rubocop with bin/ruboco
p -A . When running Rubocop, you can add the -A to make autocorrections, but this has just been done for you in the above script.
You will be left with 3 Rubocop linting errors. In bin/bundle, replace the method cli_arg_version with
def cli_arg_version
return unless invoked_as_script? # don't want to hijack other binstubs
return unless 'update'.start_with?(ARGV.first || ' ') # must be running `bundle update`
update_index = nil
loop_through_args(update_index: update_index)
end
def loop_through_args(update_index:, bundler_version: nil)
ARGV.each_with_index do |a, i|
bundler_version = a if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
bundler_version = Regexp.last_match(1)
update_index = i
end
bundler_version
end
Run bin/rubocop -A
again and you should have 1 remaining lint error, also in bin/bundle
on line 107.
The line is too long, so surround 107 with:
Layout/LineLength:
+ "bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`"
Then run bin/rubocop -A again and you will see an Asignment Branch Conditional error on the method activate_bundler inside of bin/bundle
.
Disable this on the method by adding this comment above the line def activate_bundler
# rubocop:disable Metrics/AbcSize
Be sure to re-enable the cop below the end
cooresponding to this method (around line 111)
# rubocop:enable Metrics/AbcSize
Finally, run bin/rubocop -A
again and your code should be completely linted: