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 cmd_line_opts
].freeze
11 attr_writer
:eslint_executable, :cmd_line_opts
14 @eslint_executable || 'eslint'
18 @files_to_lint || /(\.js|\.es6)$/
25 def files_to_lint
=(regexp
)
26 @files_to_lint = regexp
.is_a
?(Regexp
) && regexp
|| Regexp
.new(regexp
)
32 config_file
= File
.join(repo_path
, CONFIG_FILE
)
33 File
.exist
?(config_file
) && YAML
.load_file(config_file
) || {}
38 config_options
.each
do |key
, val
|
39 next unless CONFIG_KEYS
.include?(key
.to_s
)
45 return [] if !
@patches || @patches.count
.zero
?
50 .select
{ |patch
| patch
.additions
> 0 }
51 .select
{ |patch
| js_file
?(patch
.new_file_full_path
) }
52 .map
{ |patch
| inspect(patch
) }
59 @repo_path ||= @patches.first
.repo
.path
63 offences
= run_eslint(patch
)
64 clean_up_eslint_output(offences
)
68 .select
{ |line
| line
.new_lineno
== offence
['line'] }
69 .map
{ |line
| new_message(offence
, line
) }
73 def new_message(offence
, line
)
74 path
= line
.patch
.delta
.new_file
[:path]
77 Message
.new(path
, line
, level
, offence
['message'], nil, self.class)
81 files_to_lint
=~ path
.to_s
85 Dir
.chdir(repo_path
) do
86 JSON
.parse
`#{eslint_command_line(patch.new_file_full_path.to_s)}`
90 def eslint_command_line(path
)
91 "#{eslint_executable} #{cmd_line_opts} #{Shellwords.escape(path)} -f json"
94 def clean_up_eslint_output(output
)
95 # 1. Filter out offences without a warning or error
96 # 2. Get the messages for that file
97 # 3. Ignore errors without a line number for now
99 .select
{ |offence
| offence
['errorCount'] + offence
['warningCount'] > 0 }
100 .map
{ |offence
| offence
['messages'] }
101 .flatten
.select
{ |offence
| offence
['line'] }