Class: PDK::Validate::Rubocop

Inherits:
BaseValidator show all
Defined in:
lib/pdk/validate/ruby/rubocop.rb

Constant Summary

Constants inherited from BaseValidator

BaseValidator::INVOKE_STYLE

Class Method Summary collapse

Methods inherited from BaseValidator

cmd_path, invoke, parse_targets, process_invalid, process_skipped

Class Method Details

.cmdObject



15
16
17
# File 'lib/pdk/validate/ruby/rubocop.rb', line 15

def self.cmd
  'rubocop'
end

.nameObject



11
12
13
# File 'lib/pdk/validate/ruby/rubocop.rb', line 11

def self.name
  'rubocop'
end

.parse_options(options, targets) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/pdk/validate/ruby/rubocop.rb', line 27

def self.parse_options(options, targets)
  cmd_options = ['--format', 'json']

  if options[:auto_correct]
    cmd_options << '--auto-correct'
  end

  cmd_options.concat(targets)
end

.parse_output(report, result, _targets) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pdk/validate/ruby/rubocop.rb', line 37

def self.parse_output(report, result, _targets)
  return if result[:stdout].empty?

  begin
    json_data = JSON.parse(result[:stdout])
  rescue JSON::ParserError
    raise PDK::Validate::ParseOutputError, result[:stdout]
  end

  return unless json_data.key?('files')

  json_data['files'].each do |file_info|
    next unless file_info.key?('offenses')
    result = {
      file: file_info['path'],
      source: 'rubocop',
    }

    if file_info['offenses'].empty?
      report.add_event(result.merge(state: :passed, severity: :ok))
    else
      file_info['offenses'].each do |offense|
        report.add_event(
          result.merge(
            line:     offense['location']['line'],
            column:   offense['location']['column'],
            message:  offense['message'],
            severity: (offense['corrected']) ? 'corrected' : offense['severity'],
            test:     offense['cop_name'],
            state:    :failure,
          ),
        )
      end
    end
  end
end

.patternObject



19
20
21
# File 'lib/pdk/validate/ruby/rubocop.rb', line 19

def self.pattern
  '**/**.rb'
end

.spinner_text(_targets = nil) ⇒ Object



23
24
25
# File 'lib/pdk/validate/ruby/rubocop.rb', line 23

def self.spinner_text(_targets = nil)
  _('Checking Ruby code style (%{pattern}).') % { pattern: pattern }
end