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



69
70
71
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 69

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



65
66
67
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 65

def alternate_config_file
  '.rubocop.yml'
end

#call(staged_files) ⇒ Object



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
# 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
  allowed_files_regex = /\.gemspec\Z|
                         \.podspec\Z|
                         \.jbuilder\Z|
                         \.rake\Z|
                         \.opal\Z|
                         \.rb\Z|
                         Gemfile\Z|
                         Rakefile\Z|
                         Capfile\Z|
                         Guardfile\Z|
                         Podfile\Z|
                         Thorfile\Z|
                         Vagrantfile\Z|
                         Berksfile\Z|
                         Cheffile\Z|
                         Vagabondfile\Z
                        /x
  staged_files = staged_files.grep(allowed_files_regex)
  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

#captureObject



47
48
49
50
51
52
53
54
55
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 47

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



57
58
59
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 57

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

#user_supplied_flagsObject



61
62
63
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 61

def user_supplied_flags
  Array(config.get('rubocop.flags')).reject(&:empty?)
end