lib/pronto/eslint_npm.rb in pronto-eslint_npm-0.9.0 vs lib/pronto/eslint_npm.rb in pronto-eslint_npm-0.9.1
- old
+ new
@@ -1,35 +1,45 @@
+# frozen_string_literal: true
+
require 'pronto'
require 'shellwords'
module Pronto
class ESLintNpm < Runner
CONFIG_FILE = '.pronto_eslint_npm.yml'.freeze
- CONFIG_KEYS = %w(eslint_executable files_to_lint).freeze
+ CONFIG_KEYS = %w[eslint_executable files_to_lint cmd_line_opts].freeze
- attr_writer :eslint_executable
+ attr_writer :eslint_executable, :cmd_line_opts
def eslint_executable
- @eslint_executable || 'eslint'.freeze
+ @eslint_executable || 'eslint'
end
def files_to_lint
@files_to_lint || /(\.js|\.es6)$/
end
+ def cmd_line_opts
+ @cmd_line_opts || ''
+ end
+
def files_to_lint=(regexp)
@files_to_lint = regexp.is_a?(Regexp) && regexp || Regexp.new(regexp)
end
- def read_config
- config_file = File.join(repo_path, CONFIG_FILE)
- return unless File.exist?(config_file)
- config = YAML.load_file(config_file)
+ 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
- CONFIG_KEYS.each do |config_key|
- next unless config[config_key]
- send("#{config_key}=", config[config_key])
+ def read_config
+ config_options.each do |key, val|
+ next unless CONFIG_KEYS.include?(key.to_s)
+ send("#{key}=", val)
end
end
def run
return [] if !@patches || @patches.count.zero?
@@ -44,11 +54,11 @@
end
private
def repo_path
- @_repo_path ||= @patches.first.repo.path
+ @repo_path ||= @patches.first.repo.path
end
def inspect(patch)
offences = run_eslint(patch)
clean_up_eslint_output(offences)
@@ -59,11 +69,11 @@
.map { |line| new_message(offence, line) }
end
end
def new_message(offence, line)
- path = line.patch.delta.new_file[:path]
+ path = line.patch.delta.new_file[:path]
level = :warning
Message.new(path, line, level, offence['message'], nil, self.class)
end
@@ -71,14 +81,15 @@
files_to_lint =~ path.to_s
end
def run_eslint(patch)
Dir.chdir(repo_path) do
- escaped_file_path = Shellwords.escape(patch.new_file_full_path.to_s)
- JSON.parse(
- `#{eslint_executable} #{escaped_file_path} -f json`
- )
+ JSON.parse `#{eslint_command_line(patch.new_file_full_path.to_s)}`
end
+ end
+
+ def eslint_command_line(path)
+ "#{eslint_executable} #{cmd_line_opts} #{Shellwords.escape(path)} -f json"
end
def clean_up_eslint_output(output)
# 1. Filter out offences without a warning or error
# 2. Get the messages for that file