class ESLintRails::TextFormatter

Public Class Methods

new(warnings) click to toggle source
# File lib/eslint-rails-ee/text_formatter.rb, line 7
def initialize(warnings)
  @warnings = warnings
end

Public Instance Methods

format(should_autocorrect=false) click to toggle source
# 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

Private Instance Methods

max_length_of_attribute(attr_key) click to toggle source
# File lib/eslint-rails-ee/text_formatter.rb, line 39
def max_length_of_attribute(attr_key)
  @warnings.map { |warning| warning.send(attr_key).size }.max
end