class ESLintRails::Runner

Constants

JAVASCRIPT_EXTENSIONS

Public Class Methods

new(file) click to toggle source
# File lib/eslint-rails/runner.rb, line 9
def initialize(file)
  @file = normalize_infile(file)
end

Public Instance Methods

run() click to toggle source
# File lib/eslint-rails/runner.rb, line 13
def run
  warnings = assets.map do |asset|
    generate_warnings(asset).tap { |warnings| output_progress(warnings) }
  end

  puts

  warnings.flatten
end

Private Instance Methods

assets() click to toggle source
# File lib/eslint-rails/runner.rb, line 32
def assets
  all_js_assets = Rails.application.assets.each_file.to_a.map { |path| Pathname.new(path) }.select do |asset|
    JAVASCRIPT_EXTENSIONS.include?(asset.extname)
  end

  assets = all_js_assets.select{|a| is_descendant?(@file, a)}

  assets.reject{|a| a.to_s =~ /eslint.js|vendor|gems|min.js|editorial/ }
end
eslint_js() click to toggle source
# File lib/eslint-rails/runner.rb, line 42
def eslint_js
  @eslint_js ||= Rails.application.assets['eslint'].to_s
end
eslint_plugin_js() click to toggle source
# File lib/eslint-rails/runner.rb, line 46
def eslint_plugin_js
  @eslint_plugin_js ||= begin
    plugins.map do |plugin_name|
      Rails.application.assets["plugins/eslint-plugin-#{plugin_name}"].to_s
    end.join('\n')
  end
end
file_severity(warnings) click to toggle source
# File lib/eslint-rails/runner.rb, line 89
def file_severity(warnings)
  warnings.map(&:severity).uniq.sort.first
end
generate_warnings(asset) click to toggle source
# File lib/eslint-rails/runner.rb, line 69
def generate_warnings(asset)
  relative_path = asset.relative_path_from(Pathname.new(Dir.pwd))
  file_content = asset.read

  warning_hashes(file_content).map do |hash|
    ESLintRails::Warning.new(relative_path, hash)
  end
end
is_descendant?(a, b) click to toggle source
# File lib/eslint-rails/runner.rb, line 93
def is_descendant?(a, b)
  a_list = a.to_s.split('/')
  b_list = b.to_s.split('/')

  b_list[0..a_list.size-1] == a_list
end
normalize_infile(file) click to toggle source
# File lib/eslint-rails/runner.rb, line 25
def normalize_infile(file)
  file = file.to_s.gsub(/^app\/assets\/javascripts\//, '') # Remove beginning of asset path
  file = Pathname.new("#{Dir.pwd}/app/assets/javascripts/#{file}") # Ensure path is absolute
  file = Pathname.new("#{file}.js") if !file.directory? && file.extname.empty? # Make sure it has an extension
  file
end
output_progress(warnings) click to toggle source
# File lib/eslint-rails/runner.rb, line 78
def output_progress(warnings)
  print case file_severity(warnings)
        when :high
          'H'.red
        when :low
          'L'.yellow
        else
          '.'.green
        end
end
plugins() click to toggle source
# File lib/eslint-rails/runner.rb, line 54
def plugins
  JSON.parse(Config.read)['plugins'] || []
end
warning_hashes(file_content) click to toggle source
# File lib/eslint-rails/runner.rb, line 58
    def warning_hashes(file_content)
      ExecJS.eval <<-JS
        function () {
          window = this;
          #{eslint_js};
          #{eslint_plugin_js};
          return eslint.verify('#{escape_javascript(file_content)}', #{Config.read});
        }()
      JS
    end