Sha256: 639de2d52bcdfda34f4bdf70c3785d3c7c7724cf9f3824c1ee349c464dd14cc3

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

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

module ESLintRails
  class TextFormatter

    def initialize(warnings)
      @warnings = warnings
    end

    def format
      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.green
          when :high
            message.yellow
          else
            raise 'BULLSHIT'
          end
        puts colorized_message
      end

      puts "#{@warnings.size} warning(s) found."
    end

    private

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eslint-rails-1.3.0 lib/eslint-rails/text_formatter.rb
eslint-rails-1.2.1 lib/eslint-rails/text_formatter.rb
eslint-rails-1.2.0 lib/eslint-rails/text_formatter.rb