class Pronto::ESLintNpm

Constants

CONFIG_FILE
CONFIG_KEYS
SEVERITY_LEVELS

Attributes

cmd_line_opts[W]
eslint_executable[W]

Public Instance Methods

cmd_line_opts() click to toggle source
# File lib/pronto/eslint_npm.rb, line 22
def cmd_line_opts
  @cmd_line_opts || ''
end
config_options() click to toggle source
# File lib/pronto/eslint_npm.rb, line 30
def config_options
  @config_options ||=
    begin
      config_file = File.join(repo_path, CONFIG_FILE)
      File.exist?(config_file) && YAML.load_file(config_file) || {}
    end
end
eslint_executable() click to toggle source
# File lib/pronto/eslint_npm.rb, line 14
def eslint_executable
  @eslint_executable || 'eslint'
end
files_to_lint() click to toggle source
# File lib/pronto/eslint_npm.rb, line 18
def files_to_lint
  @files_to_lint || /(\.js|\.es6)$/
end
files_to_lint=(regexp) click to toggle source
# File lib/pronto/eslint_npm.rb, line 26
def files_to_lint=(regexp)
  @files_to_lint = regexp.is_a?(Regexp) && regexp || Regexp.new(regexp)
end
read_config() click to toggle source
# File lib/pronto/eslint_npm.rb, line 38
def read_config
  config_options.each do |key, val|
    next unless CONFIG_KEYS.include?(key.to_s)
    send("#{key}=", val)
  end
end
run() click to toggle source
# File lib/pronto/eslint_npm.rb, line 45
def run
  return [] if !@patches || @patches.count.zero?

  read_config

  @patches
    .select { |patch| patch.additions > 0 }
    .select { |patch| js_file?(patch.new_file_full_path) }
    .map { |patch| inspect(patch) }
    .flatten.compact
end

Private Instance Methods

clean_up_eslint_output(output) click to toggle source
# File lib/pronto/eslint_npm.rb, line 95
def clean_up_eslint_output(output)
  # 1. Filter out offences without a warning or error
  # 2. Get the messages for that file
  # 3. Ignore errors without a line number for now
  output
    .select { |offence| offence['errorCount'] + offence['warningCount'] > 0 }
    .map { |offence| offence['messages'] }
    .flatten.select { |offence| offence['line'] }
end
eslint_command_line(path) click to toggle source
# File lib/pronto/eslint_npm.rb, line 91
def eslint_command_line(path)
  "#{eslint_executable} #{cmd_line_opts} #{Shellwords.escape(path)} -f json"
end
inspect(patch) click to toggle source
# File lib/pronto/eslint_npm.rb, line 63
def inspect(patch)
  lines = patch.added_lines
  offences = run_eslint(patch)
  clean_up_eslint_output(offences)
    .map do |offence|
      range = offence['line']..(offence['endLine'] || offence['line'])
      line = lines.select { |line| range.cover?(line.new_lineno) }.last
      new_message(offence, line) if line
    end
end
js_file?(path) click to toggle source
# File lib/pronto/eslint_npm.rb, line 81
def js_file?(path)
  files_to_lint =~ path.to_s
end
new_message(offence, line) click to toggle source
# File lib/pronto/eslint_npm.rb, line 74
def new_message(offence, line)
  path  = line.patch.delta.new_file[:path]
  level = SEVERITY_LEVELS.fetch(offence['severity'], :warning)

  Message.new(path, line, level, offence['message'], nil, self.class)
end
repo_path() click to toggle source
# File lib/pronto/eslint_npm.rb, line 59
def repo_path
  @repo_path ||= @patches.first.repo.path
end
run_eslint(patch) click to toggle source
# File lib/pronto/eslint_npm.rb, line 85
def run_eslint(patch)
  Dir.chdir(repo_path) do
    JSON.parse `#{eslint_command_line(patch.new_file_full_path.to_s)}`
  end
end