Class: PreCommit::Checks::Rubocop
- Inherits:
-
Plugin
- Object
- Plugin
- PreCommit::Checks::Rubocop
show all
- Defined in:
- lib/plugins/pre_commit/checks/rubocop.rb
Instance Attribute Summary
Attributes inherited from Plugin
#config, #pluginator
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Plugin
#initialize, #name
Class Method Details
.aliases ⇒ Object
8
9
10
|
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 8
def self.aliases
[ :rubocop_all, :rubocop_new ]
end
|
.description ⇒ Object
52
53
54
|
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 52
def self.description
"Runs rubocop to detect errors."
end
|
.excludes ⇒ Object
12
13
14
|
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 12
def self.excludes
[ :ruby_symbol_hashrocket ]
end
|
Instance Method Details
#alternate_config_file ⇒ Object
48
49
50
|
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 48
def alternate_config_file
'.rubocop.yml'
end
|
#call(staged_files) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 16
def call(staged_files)
require 'rubocop'
rescue LoadError => e
$stderr.puts "Could not find rubocop: #{e}"
else
staged_files = staged_files.grep(/\.rb$/)
return if staged_files.empty?
args = config_file_flag + user_supplied_flags + ["--force-exclusion"] + staged_files
success, captured = capture { ::RuboCop::CLI.new.run(args) == 0 }
captured unless success
end
|
#capture ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 30
def capture
$stdout, stdout = StringIO.new, $stdout
$stderr, stderr = StringIO.new, $stderr
result = yield
[result, $stdout.string + $stderr.string]
ensure
$stdout = stdout
$stderr = stderr
end
|
#config_file_flag ⇒ Object
40
41
42
|
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 40
def config_file_flag
config_file ? ['-c', config_file] : []
end
|
#user_supplied_flags ⇒ Object
44
45
46
|
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 44
def user_supplied_flags
Array(config.get('rubocop.flags')).reject(&:empty?)
end
|