123456789_123456789_123456789_123456789_123456789_

Module: CopHelper

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, 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

Instance Method Details

#_investigate(cop, processed_source)

[ GitHub ]

  
# File 'lib/rubocop/rspec/cop_helper.rb', line 77

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)

[ GitHub ]

  
# File 'lib/rubocop/rspec/cop_helper.rb', line 67

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)

[ GitHub ]

  
# File 'lib/rubocop/rspec/cop_helper.rb', line 63

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

#configuration

[ GitHub ]

  
# File 'lib/rubocop/rspec/cop_helper.rb', line 43

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

#inspect_source(source, file = nil)

[ GitHub ]

  
# File 'lib/rubocop/rspec/cop_helper.rb', line 16

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)

[ GitHub ]

  
# File 'lib/rubocop/rspec/cop_helper.rb', line 28

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

[ GitHub ]

  
# File 'lib/rubocop/rspec/cop_helper.rb', line 51

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