1 # frozen_string_literal: true
7 class ESLintNpm
< Runner
8 CONFIG_FILE
= '.pronto_eslint_npm.yml'.freeze
9 CONFIG_KEYS
= %w
[eslint_executable files_to_lint
].freeze
11 attr_writer
:eslint_executable
14 @eslint_executable || 'eslint'
18 @files_to_lint || /(\.js|\.es6)$/
21 def files_to_lint
=(regexp
)
22 @files_to_lint = regexp
.is_a
?(Regexp
) && regexp
|| Regexp
.new(regexp
)
28 config_file
= File
.join(repo_path
, CONFIG_FILE
)
29 File
.exist
?(config_file
) && YAML
.load_file(config_file
) || {}
34 config_options
.each
do |key
, val
|
35 next unless CONFIG_KEYS
.include?(key
.to_s
)
41 return [] if !
@patches || @patches.count
.zero
?
46 .select
{ |patch
| patch
.additions
> 0 }
47 .select
{ |patch
| js_file
?(patch
.new_file_full_path
) }
48 .map
{ |patch
| inspect(patch
) }
55 @repo_path ||= @patches.first
.repo
.path
59 offences
= run_eslint(patch
)
60 clean_up_eslint_output(offences
)
64 .select
{ |line
| line
.new_lineno
== offence
['line'] }
65 .map
{ |line
| new_message(offence
, line
) }
69 def new_message(offence
, line
)
70 path
= line
.patch
.delta
.new_file
[:path]
73 Message
.new(path
, line
, level
, offence
['message'], nil, self.class)
77 files_to_lint
=~ path
.to_s
81 Dir
.chdir(repo_path
) do
82 JSON
.parse
`#{eslint_command_line(patch.new_file_full_path.to_s)}`
86 def eslint_command_line(path
)
87 "#{eslint_executable} #{Shellwords.escape(path)} -f json"
90 def clean_up_eslint_output(output
)
91 # 1. Filter out offences without a warning or error
92 # 2. Get the messages for that file
93 # 3. Ignore errors without a line number for now
95 .select
{ |offence
| offence
['errorCount'] + offence
['warningCount'] > 0 }
96 .map
{ |offence
| offence
['messages'] }
97 .flatten
.select
{ |offence
| offence
['line'] }