Module: Overcommit::Utils::MessagesUtils

Defined in:
lib/overcommit/utils/messages_utils.rb

Overview

Utility to process messages

Class Method Summary collapse

Class Method Details

.extract_messages(output_messages, regex, type_categorizer = nil) ⇒ Array<Message>

Extract file, line number, and type of message from an error/warning messages in output.

Assumes each element of `output` is a separate error/warning with all information necessary to identify it.

Raises:


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/overcommit/utils/messages_utils.rb', line 23

def extract_messages(output_messages, regex, type_categorizer = nil)
  output_messages.map.with_index do |message, index|
    unless match = message.match(regex)
      raise Overcommit::Exceptions::MessageProcessingError,
            'Unexpected output: unable to determine line number or type ' \
            "of error/warning for output:\n" \
            "#{output_messages[index..-1].join("\n")}"
    end

    file = extract_file(match, message)
    line = extract_line(match, message) if match.names.include?('line') && match[:line]
    type = extract_type(match, message, type_categorizer)

    Overcommit::Hook::Message.new(type, file, line, message)
  end
end