-
Notifications
You must be signed in to change notification settings - Fork 0
/
linter.rb
75 lines (61 loc) · 2.1 KB
/
linter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true
require 'open3'
module Linter
class Linter
attr_reader :check, :json_data
def initialize(check)
@check = check
@json_data = case @check.repository.language.to_sym
when :javascript
run_javascript_linter
when :ruby
run_ruby_linter
end
end
private
def run_javascript_linter
repository_directory = @check.repository.decorate.directory_path
command = "yarn run eslint #{eslint_options.join(' ')} #{repository_directory}"
stdout, stderr, status = Open3.capture3(command)
return nil if status.exitstatus > 1 && stderr
stdout.split("\n")[2]
end
def run_ruby_linter
repository_directory = @check.repository.decorate.directory_path
command = "bundle exec rubocop #{rubocop_options.join(' ')}"
Dir.chdir(repository_directory)
stdout, stderr, status = Open3.capture3(command)
return nil if status.exitstatus > 1 && stderr
stdout
end
def config_file_lookup(language)
config_files = case language
when :javascript
Dir.glob(Rails.root.join('.eslintrc.*').to_s)
when :ruby
Dir.glob(Rails.root.join('.rubocop.yml').to_s)
else
[]
end
return false if config_files.empty?
config_files.first
end
def eslint_options
default_config = '--no-eslintrc'
config_file_path = config_file_lookup(:javascript)
options = config_file_path ? "--config #{config_file_path}" : default_config
['--format json', options]
end
def rubocop_options
default_config = "#{Gem::Specification.find_by_name('rubocop').gem_dir}/config/default.yml"
config_file_path = config_file_lookup(:ruby)
options = config_file_path ? "--config #{config_file_path}" : "--config #{default_config}"
[
'--safe',
'--ignore-unrecognized-cops',
'--format json',
options
]
end
end
end