Class: ESLintRails::TextFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/eslint-rails-ee/text_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(warnings) ⇒ TextFormatter

Returns a new instance of TextFormatter.



7
8
9
# File 'lib/eslint-rails-ee/text_formatter.rb', line 7

def initialize(warnings)
  @warnings = warnings
end

Instance Method Details

#format(should_autocorrect = false) ⇒ Object



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
# File 'lib/eslint-rails-ee/text_formatter.rb', line 11

def format(should_autocorrect=false)
  max_line_column_length = max_length_of_attribute(:location)
  max_rule_id_length = max_length_of_attribute(:rule_id)
  max_message_length = max_length_of_attribute(:message)
  @warnings.each do |warning|
    message = [
      warning.location.ljust(max_line_column_length + 1),
      warning.severity.to_s.ljust(6),
      warning.rule_id.ljust(max_rule_id_length),
      warning.message.ljust(max_message_length)
    ].join(" ")
    colorized_message =
      case warning.severity
      when :low
        message.yellow
      when :high
        message.red
      else
        raise 'Couldn\'t figure out how to format this mess. Stahp.'
      end
    puts colorized_message
  end

  puts "#{@warnings.size} warning(s) found #{should_autocorrect ? 'after auto-correcting some issues' : ''}"
end