module CopHelper
def _investigate(cop, processed_source)
def _investigate(cop, processed_source) team = RuboCop::Cop::Team.new([cop], nil, raise_error: true) report = team.investigate(processed_source) @last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source) report.offenses end
def autocorrect_source(source, file = nil)
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) @last_corrector.rewrite end
def autocorrect_source_file(source)
def autocorrect_source_file(source) Tempfile.open('tmp') { |f| autocorrect_source(source, f) } end
def inspect_source(source, file = nil)
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
def inspect_source_file(source)
def inspect_source_file(source) Tempfile.open('tmp') { |f| inspect_source(source, f) } end
def parse_source(source, file = nil)
def parse_source(source, file = nil) if file.respond_to?(:write) file.write(source) file.rewind file = file.path end RuboCop::ProcessedSource.new(source, ruby_version, file) end