Sha256: 96dfb25b90df128f1c14f991e96c12ef879b839659a70ad411da3f306be835e2

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 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 + 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 alternate_config_file
        '.rubocop.yml'
      end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pre-commit-0.16.0 lib/plugins/pre_commit/checks/rubocop.rb
pre-commit-0.15.0 lib/plugins/pre_commit/checks/rubocop.rb