Class: PreCommit::Checks::Rubocop

Inherits:
Plugin
  • Object
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

Constructor Details

This class inherits a constructor from PreCommit::Checks::Plugin

Class Method Details

.aliasesObject



8
9
10
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 8

def self.aliases
  [ :rubocop_all, :rubocop_new ]
end

.descriptionObject



48
49
50
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 48

def self.description
  "Runs rubocop to detect errors."
end

.excludesObject



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_fileObject



44
45
46
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 44

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 + staged_files

  success, captured = capture { ::Rubocop::CLI.new.run(args) == 0 }
  captured unless success
end

#captureObject



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_flagObject



40
41
42
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 40

def config_file_flag
  config_file ? ['-c', config_file] : []
end