Class: Gergich::Capture::RubocopCapture

Inherits:
BaseCapture show all
Defined in:
lib/gergich/capture/rubocop_capture.rb

Constant Summary collapse

SEVERITY_MAP =
{
  "I" => "info",  # info
  "R" => "info",  # refactor
  "C" => "info",  # convention
  "W" => "warn",  # warning
  "E" => "error", # error
  "F" => "error"  # fatal
}.freeze

Instance Method Summary collapse

Methods inherited from BaseCapture

inherited, normalize_captor_class_name

Instance Method Details

#run(output) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
73
74
75
76
77
78
79
80
81
# File 'lib/gergich/capture/rubocop_capture.rb', line 15

def run(output)
  # Example:
  #   bin/gergich:47:8: C: Prefer double-quoted strings
  #   if ENV['DEBUG']
  #          ^^^^^^^
  #
  # 1 file inspected, 35 offenses detected, 27 offenses auto-correctable
  #
  # Example:
  # 2 files inspected, 40 offenses detected, 31 offenses auto-correctable
  #
  # Example:
  # 1 file inspected, no offenses detected

  first_line_pattern = /^([^:\n]+):(\d+):\d+:\s(\w):\s/

  parts = output.split(first_line_pattern)

  unless parts.last.match?(/^\d+ files? inspect/)
    raise "RuboCop failed to run properly:\n\n#{output}"
  end

  # strip off the summary line from the last error
  parts[-1] = parts[-1].split("\n")[0..-2].join("\n")

  # strip off the header
  parts.shift

  messages = []

  until parts.empty?
    file = parts.shift
    line = parts.shift
    severity = parts.shift
    message = parts.shift
    # if there is code context at the end, separate it and indent it
    # so that gerrit preserves formatting
    if /(?<context>[^\n]+\n *\^+\n)/m =~ message
      message.sub!(context, "\n#{context.gsub(/^/, ' ')}")
    end
    match = message.match(
      %r{
        \A
        (?<corrected>\[Corrected\]\s)?
        (?<correctable>\[Correctable\]\s)?
        (?:(?<cop>[A-Za-z/]+):\s)?
        (?<message>.*)
        \z
        }mx
    )

    # rubocop:disable Style/DoubleNegation
    messages << {
      path: file,
      position: line.to_i,
      message: match[:message],
      rule: match[:cop],
      corrected: !!match[:corrected],
      correctable: !!match[:correctable],
      severity: SEVERITY_MAP[severity],
      source: "rubocop"
    }
    # rubocop:enable Style/DoubleNegation
  end

  messages
end