class Pronto::RubocopAutocorrect

Public Class Methods

new(_, _ = nil) click to toggle source
Calls superclass method
# File lib/pronto/rubocop-autocorrect.rb, line 6
def initialize(_, _ = nil)
  super
  @auto_correct = true
  @config_store = ::RuboCop::ConfigStore.new
  if ENV['RUBOCOP_CONFIG']
    @config_store.options_config = ENV['RUBOCOP_CONFIG']
  end

  options = { auto_correct: @auto_correct }
  @inspector = ::RuboCop::Runner.new(options, @config_store)
end

Public Instance Methods

config_store_for(patch) click to toggle source
# File lib/pronto/rubocop-autocorrect.rb, line 57
 def config_store_for(patch)
   path = patch.new_file_full_path.to_s
   @config_store.for(path)
end
level(severity) click to toggle source
# File lib/pronto/rubocop-autocorrect.rb, line 67
 def level(severity)
   case severity
   when :refactor, :convention
     :warning
   when :warning, :error, :fatal
     severity
   end
end
new_message(offence, line) click to toggle source
# File lib/pronto/rubocop-autocorrect.rb, line 46
 def new_message(offence, line)
   path = line.patch.delta.new_file[:path]
   level = level(offence.severity.name)
   message = offence.message
   if offence.corrected?
     message = "[Corrected] #{message}"
     level = :info
   end
   Message.new(path, line, level, message, nil, self.class)
end
process(patch) click to toggle source
# File lib/pronto/rubocop-autocorrect.rb, line 39
 def process(patch)
   processed_source = processed_source_for(patch)
   file = patch.delta.new_file[:path]
   offences = @inspector.send(:do_inspection_loop, file, processed_source)[1]
   _messages_for_offences(offences, patch)
end
processed_source_for(patch) click to toggle source
# File lib/pronto/rubocop-autocorrect.rb, line 62
 def processed_source_for(patch)
   path = patch.new_file_full_path.to_s
   ::RuboCop::ProcessedSource.from_file(path, RUBY_VERSION[0..2].to_f)
end
run() click to toggle source
# File lib/pronto/rubocop-autocorrect.rb, line 18
 def run
   return [] unless @patches

   messages = @patches.select { |patch| valid_patch?(patch) }
     .map { |patch| process(patch) }
     .flatten.compact
   _add_corrected_message_total(messages) if @auto_correct
   messages
end
valid_patch?(patch) click to toggle source
# File lib/pronto/rubocop-autocorrect.rb, line 28
 def valid_patch?(patch)
   return false if patch.additions < 1

   config_store = config_store_for(patch)
   path = patch.new_file_full_path
   return false if config_store.file_to_exclude?(path.to_s)
   return true if config_store.file_to_include?(path.to_s)

   ruby_file?(path)
end

Private Instance Methods

_add_corrected_message_total(messages) click to toggle source
# File lib/pronto/rubocop-autocorrect.rb, line 78
 def _add_corrected_message_total(messages)
   corrected_messages = messages.select { |m| m.msg =~ /\[Corrected\]/ }
   total = "Total offenses: #{messages.size}   " \
     " Corrected: #{corrected_messages.size}"
   messages << Message.new(nil, nil, :info, total, nil, self.class)
end
_messages_for_offences(offences, patch) click to toggle source
# File lib/pronto/rubocop-autocorrect.rb, line 85
 def _messages_for_offences(offences, patch)
   offences.sort.reject(&:disabled?).map do |offence|
     patch.added_lines
       .select { |line| line.new_lineno == offence.line }
       .map { |line| new_message(offence, line) }
   end
end