Module: CopHelper

Extended by:
RSpec::SharedContext
Defined in:
lib/rubocop/rspec/cop_helper.rb

Overview

This module provides methods that make it easier to test Cops.

Instance Method Summary collapse

Instance Method Details

#_investigate(cop, processed_source) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rubocop/rspec/cop_helper.rb', line 67

def _investigate(cop, processed_source)
  forces = RuboCop::Cop::Force.all.each_with_object([]) do |klass, instances|
    next unless cop.join_force?(klass)

    instances << klass.new([cop])
  end

  commissioner =
    RuboCop::Cop::Commissioner.new([cop], forces, raise_error: true)
  commissioner.investigate(processed_source)
  commissioner
end

#autocorrect_source(source, file = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/rspec/cop_helper.rb', line 40

def autocorrect_source(source, file = nil)
  RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
  RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
  cop.instance_variable_get(:@options)[:auto_correct] = true
  processed_source = parse_source(source, file)
  _investigate(cop, processed_source)

  corrector =
    RuboCop::Cop::Corrector.new(processed_source.buffer, cop.corrections)
  corrector.rewrite
end

#autocorrect_source_file(source) ⇒ Object



36
37
38
# File 'lib/rubocop/rspec/cop_helper.rb', line 36

def autocorrect_source_file(source)
  Tempfile.open('tmp') { |f| autocorrect_source(source, f) }
end

#autocorrect_source_with_loop(source, file = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubocop/rspec/cop_helper.rb', line 52

def autocorrect_source_with_loop(source, file = nil)
  cnt = 0
  loop do
    cop.instance_variable_set(:@corrections, [])
    new_source = autocorrect_source(source, file)
    return new_source if new_source == source

    source = new_source
    cnt += 1
    if cnt > RuboCop::Runner::MAX_ITERATIONS
      raise RuboCop::Runner::InfiniteCorrectionLoop.new(file, [])
    end
  end
end

#inspect_source(source, file = nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/rubocop/rspec/cop_helper.rb', line 17

def inspect_source(source, file = nil)
  RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
  RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
  processed_source = parse_source(source, file)
  raise 'Error parsing example code' unless processed_source.valid_syntax?

  _investigate(cop, processed_source)
end

#inspect_source_file(source) ⇒ Object



13
14
15
# File 'lib/rubocop/rspec/cop_helper.rb', line 13

def inspect_source_file(source)
  Tempfile.open('tmp') { |f| inspect_source(source, f) }
end

#parse_source(source, file = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rubocop/rspec/cop_helper.rb', line 26

def parse_source(source, file = nil)
  if file && file.respond_to?(:write)
    file.write(source)
    file.rewind
    file = file.path
  end

  RuboCop::ProcessedSource.new(source, ruby_version, file)
end