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.

Parameters:

  • output_messages (Array<String>)

    unprocessed error/warning messages

  • regex (Regexp)

    regular expression defining ‘file`, `line` and `type` capture groups used to extract file locations and error/warning type from each line of output

  • type_categorizer (Proc) (defaults to: nil)

    function executed against the ‘type` capture group to convert it to a `:warning` or `:error` symbol. Assumes `:error` if `nil`.

Returns:

  • (Array<Message>)

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..].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