Class: Pronto::Rubocop

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

Instance Method Summary collapse

Constructor Details

#initializeRubocop

Returns a new instance of Rubocop.



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

def initialize
  @config_store = ::RuboCop::ConfigStore.new
  @inspector = ::RuboCop::Runner.new({}, @config_store)
end

Instance Method Details

#config_store_for(patch) ⇒ Object



49
50
51
52
# File 'lib/pronto/rubocop.rb', line 49

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

#inspect(patch) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/pronto/rubocop.rb', line 31

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



59
60
61
62
63
64
65
66
# File 'lib/pronto/rubocop.rb', line 59

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

#new_message(offence, line) ⇒ Object



42
43
44
45
46
47
# File 'lib/pronto/rubocop.rb', line 42

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

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

#processed_source_for(patch) ⇒ Object



54
55
56
57
# File 'lib/pronto/rubocop.rb', line 54

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(patches, _) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/pronto/rubocop.rb', line 11

def run(patches, _)
  return [] unless patches

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

#valid_patch?(patch) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pronto/rubocop.rb', line 19

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