lib/plugins/pre_commit/checks/rubocop.rb in pre-commit-0.23.0 vs lib/plugins/pre_commit/checks/rubocop.rb in pre-commit-0.24.0
- old
+ new
@@ -3,10 +3,16 @@
module PreCommit
module Checks
class Rubocop < Plugin
+ WHITELIST = [
+ ".gemspec", ".jbuilder", ".opal", ".podspec", ".rake", ".rb",
+ "Berksfile", "Capfile", "Cheffile", "Gemfile", "Guardfile", "Podfile",
+ "Rakefile", "Thorfile", "Vagabondfile", "Vagrantfile"
+ ]
+
def self.aliases
[ :rubocop_all, :rubocop_new ]
end
def self.excludes
@@ -16,33 +22,21 @@
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)
+ staged_files = filter_staged_files(staged_files)
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
+
+ def filter_staged_files(staged_files)
+ expression = Regexp.new(WHITELIST.map { |i| i + "\\Z" }.join("|"))
+ staged_files.grep(expression)
end
def capture
$stdout, stdout = StringIO.new, $stdout
$stderr, stderr = StringIO.new, $stderr