Sha256: 3f8551485efe050f14a022ced99cae6c8a0e8fc57209e641eb5acf58b0070bd8

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'active_support/core_ext/string/strip'
require 'colorize'

module ESLintRails
  class TextFormatter

    def initialize(warnings)
      @warnings = warnings
    end

    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

    def max_length_of_attribute(attr_key)
      @warnings.map { |warning| warning.send(attr_key).size }.max
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eslint-rails-ee-1.0.1 lib/eslint-rails-ee/text_formatter.rb
eslint-rails-ee-1.0 lib/eslint-rails-ee/text_formatter.rb