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.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.integrated_plugins ⇒ Object

Returns the value of attribute integrated_plugins.



17
18
19
# File 'lib/rubocop/rspec/cop_helper.rb', line 17

def integrated_plugins
  @integrated_plugins
end

Class Method Details

.prism_engine? ⇒ Boolean

Whether the specs run on one of the Prism parser engines.

Returns:

  • (Boolean)


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

def self.prism_engine?
  %w[parser_prism prism].include?(ENV.fetch('PARSER_ENGINE', nil))
end

Instance Method Details

#_investigate(cop, processed_source) ⇒ Object



103
104
105
106
107
108
# File 'lib/rubocop/rspec/cop_helper.rb', line 103

def _investigate(cop, processed_source)
  team = RuboCop::Cop::Team.new([cop], configuration, raise_error: true)
  report = team.investigate(processed_source)
  @last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source)
  report.offenses.reject(&:disabled?)
end

#autocorrect_source(source, file = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/rubocop/rspec/cop_helper.rb', line 93

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

  @last_corrector.rewrite
end

#autocorrect_source_file(source) ⇒ Object



89
90
91
# File 'lib/rubocop/rspec/cop_helper.rb', line 89

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

#configuration ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rubocop/rspec/cop_helper.rb', line 69

def configuration
  @configuration ||= if defined?(config)
                       config
                     else
                       RuboCop::Config.new({}, "#{Dir.pwd}/.rubocop.yml")
                     end
end

#inspect_source(source, file = nil) ⇒ Object



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

def inspect_source(source, file = nil)
  RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
  RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
  processed_source = parse_source(source, file)
  unless processed_source.valid_syntax?
    raise 'Error parsing example code: ' \
          "#{processed_source.diagnostics.map(&:render).join("\n")}"
  end

  _investigate(cop, processed_source)
end

#parse_source(source, file = nil) ⇒ Object



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

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

  processed_source = RuboCop::ProcessedSource.new(
    source, ruby_version, file, parser_engine: parser_engine
  )
  processed_source.config = configuration
  processed_source.registry = registry
  processed_source
end

#registry ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubocop/rspec/cop_helper.rb', line 77

def registry
  @registry ||= begin
    keys = configuration.keys
    cops =
      keys.map { |directive| RuboCop::Cop::Registry.global.find_cops_by_directive(directive) }
          .flatten
    cops << cop_class if defined?(cop_class) && !cops.include?(cop_class)
    cops.compact!
    RuboCop::Cop::Registry.new(cops)
  end
end