Class: PDK::Validate::Rubocop

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

Constant Summary collapse

ALLOW_EMPTY_TARGETS =
true

Constants inherited from BaseValidator

BaseValidator::IGNORE_DOTFILES, BaseValidator::INVOKE_STYLE

Class Method Summary collapse

Methods inherited from BaseValidator

allow_empty_targets?, cmd_path, ignore_dotfiles?, ignore_pathspec, invoke, parse_targets, process_invalid, process_skipped

Class Method Details

.cmdObject



17
18
19
# File 'lib/pdk/validate/ruby/rubocop.rb', line 17

def self.cmd
  'rubocop'
end

.nameObject



13
14
15
# File 'lib/pdk/validate/ruby/rubocop.rb', line 13

def self.name
  'rubocop'
end

.parse_options(options, targets) ⇒ Object



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

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



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
73
74
# File 'lib/pdk/validate/ruby/rubocop.rb', line 39

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



21
22
23
# File 'lib/pdk/validate/ruby/rubocop.rb', line 21

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

.spinner_text(_targets = nil) ⇒ Object



25
26
27
# File 'lib/pdk/validate/ruby/rubocop.rb', line 25

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