Sha256: cc601079476f0ec774626cd1efcb3ebf326d3c801921dbd8928afc10034b86f6

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

require 'stringio'
require 'pre-commit/checks/plugin'

module PreCommit
  module Checks
    class Rubocop < Plugin

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

      def self.excludes
        [ :ruby_symbol_hashrocket ]
      end

      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

      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

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

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

      def alternate_config_file
        '.rubocop.yml'
      end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pre-commit-0.22.1 lib/plugins/pre_commit/checks/rubocop.rb
pre-commit-0.22.0 lib/plugins/pre_commit/checks/rubocop.rb
pre-commit-0.21.0 lib/plugins/pre_commit/checks/rubocop.rb
pre-commit-0.20.0 lib/plugins/pre_commit/checks/rubocop.rb