Class: Pronto::Rubocop

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

Instance Method Summary collapse

Constructor Details

#initialize(_, _ = nil) ⇒ Rubocop

Returns a new instance of Rubocop.



6
7
8
9
10
11
12
# File 'lib/pronto/rubocop.rb', line 6

def initialize(_, _ = nil)
  super

  @config_store = ::RuboCop::ConfigStore.new
  @config_store.options_config = ENV['RUBOCOP_CONFIG'] if ENV['RUBOCOP_CONFIG']
  @inspector = ::RuboCop::Runner.new({}, @config_store)
end

Instance Method Details

#config_store_for(patch) ⇒ Object



52
53
54
55
# File 'lib/pronto/rubocop.rb', line 52

def config_store_for(patch)
  path = patch.new_file_full_path.to_s
  @config_store.for(path)
end

#inspect(patch) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/pronto/rubocop.rb', line 34

def inspect(patch)
  processed_source = processed_source_for(patch)
  offences = @inspector.send(:inspect_file, processed_source).first

  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

#level(severity) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/pronto/rubocop.rb', line 62

def level(severity)
  case severity
  when :refactor, :convention
    :warning
  when :warning, :error, :fatal
    severity
  end
end

#new_message(offence, line) ⇒ Object



45
46
47
48
49
50
# File 'lib/pronto/rubocop.rb', line 45

def new_message(offence, line)
  path = line.patch.delta.new_file[:path]
  level = level(offence.severity.name)

  Message.new(path, line, level, offence.message, nil, self.class)
end

#processed_source_for(patch) ⇒ Object



57
58
59
60
# File 'lib/pronto/rubocop.rb', line 57

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



14
15
16
17
18
19
20
# File 'lib/pronto/rubocop.rb', line 14

def run
  return [] unless @patches

  @patches.select { |patch| valid_patch?(patch) }
    .map { |patch| inspect(patch) }
    .flatten.compact
end

#valid_patch?(patch) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pronto/rubocop.rb', line 22

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