class Rubocop::Changes::Checker

Attributes

auto_correct[R]
commit[R]
format[R]
quiet[R]

Public Class Methods

new(format:, quiet:, commit:, auto_correct:) click to toggle source
# File lib/rubocop/changes/checker.rb, line 16
def initialize(format:, quiet:, commit:, auto_correct:)
  @format = format
  @quiet = quiet
  @commit = commit
  @auto_correct = auto_correct
end

Public Instance Methods

run() click to toggle source
# File lib/rubocop/changes/checker.rb, line 23
def run
  raise UnknownForkPointError if fork_point.empty?
  raise UnknownFormat if formatter_klass.nil?

  print_offenses! unless quiet

  checks.map(&:offenses).flatten
end

Private Instance Methods

auto_correct_modifier() click to toggle source
# File lib/rubocop/changes/checker.rb, line 81
def auto_correct_modifier
  '-a' if @auto_correct
end
changed_files() click to toggle source
# File lib/rubocop/changes/checker.rb, line 54
def changed_files
  patches.map(&:file)
end
checks() click to toggle source
# File lib/rubocop/changes/checker.rb, line 89
def checks
  @checks ||= ruby_changed_files.map do |file|
    analysis = rubocop_json.files.find { |item| item.path == file }
    patch = patches.find { |item| item.file == file }

    next unless analysis

    Check.new(analysis, patch)
  end.compact
end
command() click to toggle source
# File lib/rubocop/changes/checker.rb, line 40
def command
  return 'git merge-base HEAD origin/master' unless commit

  "git log -n 1 --pretty=format:\"%h\" #{commit}"
end
diff() click to toggle source
# File lib/rubocop/changes/checker.rb, line 46
def diff
  Shell.run("git diff #{fork_point}")
end
exclussion_modifier() click to toggle source
# File lib/rubocop/changes/checker.rb, line 73
def exclussion_modifier
  '--force-exclusion'
end
fork_point() click to toggle source
# File lib/rubocop/changes/checker.rb, line 36
def fork_point
  @fork_point ||= Shell.run(command)
end
formatter() click to toggle source
# File lib/rubocop/changes/checker.rb, line 118
def formatter
  @formatter ||= formatter_klass.new($stdout)
end
formatter_klass() click to toggle source
# File lib/rubocop/changes/checker.rb, line 122
def formatter_klass
  @formatter_klass ||= formatters[format]
end
formatter_modifier() click to toggle source
# File lib/rubocop/changes/checker.rb, line 77
def formatter_modifier
  "-f j #{ruby_changed_files.join(' ')}"
end
formatters() click to toggle source
# File lib/rubocop/changes/checker.rb, line 126
def formatters
  rubocop_formatters.map do |key, value|
    [key.gsub(/[\[\]]/, '').to_sym, value]
  end.to_h
end
offended_lines() click to toggle source
# File lib/rubocop/changes/checker.rb, line 100
def offended_lines
  checks.map(&:offended_lines).flatten.compact
end
patches() click to toggle source
# File lib/rubocop/changes/checker.rb, line 50
def patches
  @patches ||= GitDiffParser.parse(diff)
end
print_offenses!() click to toggle source
print_offenses_for_check(check) click to toggle source
rubocop() click to toggle source
# File lib/rubocop/changes/checker.rb, line 62
def rubocop
  shell_command = [
    'rubocop',
    exclussion_modifier,
    formatter_modifier,
    auto_correct_modifier
  ].compact.join(' ')

  Shell.run(shell_command)
end
rubocop_formatters() click to toggle source
# File lib/rubocop/changes/checker.rb, line 132
def rubocop_formatters
  RuboCop::Formatter::FormatterSet::BUILTIN_FORMATTERS_FOR_KEYS
end
rubocop_json() click to toggle source
# File lib/rubocop/changes/checker.rb, line 85
def rubocop_json
  @rubocop_json ||= JSON.parse(rubocop, object_class: OpenStruct)
end
ruby_changed_files() click to toggle source
# File lib/rubocop/changes/checker.rb, line 58
def ruby_changed_files
  changed_files.select { |changed_file| changed_file =~ /.rb$/ }
end
total_offenses() click to toggle source
# File lib/rubocop/changes/checker.rb, line 104
def total_offenses
  checks.map { |check| check.offended_lines.size }.inject(0, :+)
end