Class: CukeLinter::PrettyFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cuke_linter/formatters/pretty_formatter.rb

Overview

Formats linting data into organized, user readable text

Instance Method Summary collapse

Instance Method Details

#format(data) ⇒ Object

Formats the given linting data



8
9
10
11
12
13
14
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
# File 'lib/cuke_linter/formatters/pretty_formatter.rb', line 8

def format(data)
  categorized_problems = Hash.new { |linters, linter_name| linters[linter_name] = Hash.new { |problems, problem| problems[problem] = [] } }

  data.each do |lint_item|
    categorized_problems[lint_item[:linter]][lint_item[:problem]] << lint_item[:location]
  end

  formatted_data = ''

  categorized_problems.each_pair do |linter, problems|
    formatted_data << linter + "\n"

    problems.each_pair do |problem, locations|
      formatted_data << "  #{problem}" + "\n"

      sorted_locations = locations.sort do |a, b|
        file_name_1   = a.match(/(.*):\d+$/)[1]
        line_number_1 = a.match(/:(\d+)$/)[1].to_i
        file_name_2   = b.match(/(.*):\d+$/)[1]
        line_number_2 = b.match(/:(\d+)$/)[1].to_i

        case
          when (file_name_1 < file_name_2) ||
              (file_name_1 == file_name_2) && (line_number_1 < line_number_2)
            -1
          when (file_name_1 > file_name_2) ||
              (file_name_1 == file_name_2) && (line_number_1 > line_number_2)
            1
          else
            0
        end
      end

      sorted_locations.each do |location|
        formatted_data << "    #{location}\n"
      end
    end
  end

  total_problems = data.count
  formatted_data << "\n" unless total_problems.zero?

  formatted_data << "#{total_problems} issues found"

  formatted_data
end