class RuboCopConfigUpdater::CLI::Command::FixOutdatedCops

Constants

COP_RENAMED_REGEXP

Error: The `Layout/AlignArray` cop has been renamed to `Layout/ArrayAlignment`.

NO_DEPARTMENT_REGEXP

.rubocop.yml: Warning: no department given for AccessModifierIndentation.

PROPERTY_RENAMED_REGEXP

`EnforcedStyle: rails` has been renamed to `EnforcedStyle: indented_internal_methods`

WRONG_NAMESPACE_REGEXP

.rubocop.yml: Metrics/LineLength has the wrong namespace - should be Layout

Attributes

errors[R]
unknown_errors[R]

Public Class Methods

fixes_error?(stderr) click to toggle source
# File lib/rubocop_config_updater/cli/command/fix_errors.rb, line 21
def self.fixes_error?(stderr)
  [
    COP_RENAMED_REGEXP,
    PROPERTY_RENAMED_REGEXP,
    WRONG_NAMESPACE_REGEXP,
    NO_DEPARTMENT_REGEXP
  ].any? { |regexp| regexp.match(stderr) }
end

Public Instance Methods

run() click to toggle source
# File lib/rubocop_config_updater/cli/command/fix_errors.rb, line 30
def run
  @errors = []
  @unknown_errors = []

  collect_errors
  fix_errors

  log_changes
end

Private Instance Methods

all_cops() click to toggle source

Style/ZeroLengthPredicate:

Description: 'Use #empty? when testing for objects of length 0.'
Enabled: true
Safe: false
VersionAdded: '0.37'
VersionChanged: '0.39'
# File lib/rubocop_config_updater/cli/command/fix_errors.rb, line 86
def all_cops
  @all_cops ||= `#{options[:cmd]} --show-cops --force-default-config`.lines.select { |l| l.match(/^\w/) }
end
collect_errors() click to toggle source
# File lib/rubocop_config_updater/cli/command/fix_errors.rb, line 44
def collect_errors
  stderr.each_line do |line|
    if COP_RENAMED_REGEXP.match?(line)
      matched = line.scan(%r{`(\w+/\w+)`})

      errors << [matched[0], matched[1]].flatten
    elsif PROPERTY_RENAMED_REGEXP.match?(line)
      matched = line.scan(/`(\w+:?\s?\w+)`/)

      errors << [matched[0], matched[1]].flatten
    elsif WRONG_NAMESPACE_REGEXP.match?(line)
      to_replace = line.scan(%r{\w+/\w+}).first
      replace_with = "#{line.split.last}/#{to_replace.split('/').last}"

      errors << [to_replace, replace_with]
    elsif NO_DEPARTMENT_REGEXP.match?(line)
      to_replace = line.split.last.chop
      replace_with = all_cops.grep(%r{/#{to_replace}:}).first.strip.chop

      errors << [to_replace, replace_with]
    else
      unknown_errors << line
    end
  end
end
fix_errors() click to toggle source
# File lib/rubocop_config_updater/cli/command/fix_errors.rb, line 70
def fix_errors
  rubocop_config = File.read(options[:config_path])

  errors.each do |array|
    rubocop_config.gsub!(array[0], array[1])
  end

  File.write(options[:config_path], rubocop_config)
end
log_changes() click to toggle source
# File lib/rubocop_config_updater/cli/command/fix_errors.rb, line 90
def log_changes
  if errors.any?
    puts 'Following changes were applied to configuration file:'

    errors.each do |array|
      puts "  - #{array[0]} replaced with #{array[1]}"
    end
  end

  return unless unknown_errors.any?

  puts
  puts "Following errors weren't fixed:"

  unknown_errors.each do |e|
    # doesn't print an insignificant line
    next if /please update it/.match?(e)

    puts "  - #{e}"
  end
end