class Pronto::Rubocop::PatchCop

Attributes

patch[R]
runner[R]

Public Class Methods

new(patch, runner) click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 8
def initialize(patch, runner)
  @patch = patch
  @runner = runner
end

Public Instance Methods

messages() click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 13
def messages
  return [] unless valid?

  offenses
    .map { |offense| first_relevant_message(patch, offense) }
    .compact
end
processed_source() click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 21
def processed_source
  @processed_source ||= begin
    processed_source = ::RuboCop::ProcessedSource.from_file(
      path,
      rubocop_config.target_ruby_version
    )
    processed_source.registry = registry if processed_source.respond_to?(:registry=)
    processed_source.config = rubocop_config if processed_source.respond_to?(:config=)
    processed_source
  end
end
registry() click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 33
def registry
  @registry ||= ::RuboCop::Cop::Registry.new(RuboCop::Cop::Cop.all)
end
rubocop_config() click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 37
def rubocop_config
  @rubocop_config ||= begin
    store = ::RuboCop::ConfigStore.new
    store.options_config = ENV['RUBOCOP_CONFIG'] if ENV['RUBOCOP_CONFIG']
    store.for(path)
  end
end

Private Instance Methods

first_relevant_line(patch, offense) click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 82
def first_relevant_line(patch, offense)
  patch.added_lines.detect do |line|
    offense_includes?(offense, line.new_lineno)
  end
end
first_relevant_message(patch, offense) click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 88
def first_relevant_message(patch, offense)
  offending_line = first_relevant_line(patch, offense)
  return nil unless offending_line

  OffenseLine.new(self, offense, offending_line).message
end
offense_includes?(offense, line_number) click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 67
def offense_includes?(offense, line_number)
  offense_range = (offense.location.first_line..offense.location.last_line)
  offense_range.include?(line_number)
end
offenses() click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 60
def offenses
  team
    .inspect_file(processed_source)
    .sort
    .reject(&:disabled?)
end
path() click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 56
def path
  @path ||= patch.new_file_full_path.to_s
end
team() click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 72
def team
  @team ||=
    if ::RuboCop::Cop::Team.respond_to?(:mobilize)
      # rubocop v0.85.0 and later
      ::RuboCop::Cop::Team.mobilize(registry, rubocop_config)
    else
      ::RuboCop::Cop::Team.new(registry, rubocop_config)
    end
end
valid?() click to toggle source
# File lib/pronto/rubocop/patch_cop.rb, line 49
def valid?
  return false if rubocop_config.file_to_exclude?(path)
  return true if rubocop_config.file_to_include?(path)

  true
end