Class: Pronto::RubocopAutocorrect

Inherits:
Runner
  • Object
show all
Defined in:
lib/pronto/rubocop-autocorrect.rb

Instance Method Summary collapse

Constructor Details

#initialize(_, _ = nil) ⇒ RubocopAutocorrect

Returns a new instance of RubocopAutocorrect.



6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Method Details

#config_store_for(patch) ⇒ Object



57
58
59
60
# 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) ⇒ Object



67
68
69
70
71
72
73
74
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



39
40
41
42
43
44
# 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) ⇒ Object



62
63
64
65
# 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

#runObject



18
19
20
21
22
23
24
25
26
# 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) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
# 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