Class: Pronto::Rubocop::PatchCop

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patch, runner) ⇒ PatchCop

Returns a new instance of PatchCop.



8
9
10
11
# File 'lib/pronto/rubocop/patch_cop.rb', line 8

def initialize(patch, runner)
  @patch = patch
  @runner = runner
end

Instance Attribute Details

#runnerObject (readonly)

Returns the value of attribute runner.



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

def runner
  @runner
end

Instance Method Details

#messagesObject



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

def messages
  return [] unless valid?

  offenses.flat_map do |offense|
    patch
      .added_lines
      .select { |line| line.new_lineno == offense.line }
      .map { |line| OffenseLine.new(self, offense, line).message }
  end
end

#processed_sourceObject



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

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

#registryObject



36
37
38
# File 'lib/pronto/rubocop/patch_cop.rb', line 36

def registry
  @registry ||= ::RuboCop::Cop::Registry.new(RuboCop::Cop::Cop.all)
end

#rubocop_configObject



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

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