Sha256: c83620ad9735eb658279c016f45858d5becfc762684f405ca793741584a863af

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require 'pre-commit/utils'

module PreCommit
  class RubySymbolHashrockets

    attr_accessor :staged_files, :error_message

    HASHROCKET_PATTERN = '[^:](:{1}(?:\$|@|@@|[_A-Za-z])?\w*[=!?]?\s*=>\s*)'

    def self.call
      check = new
      check.staged_files = Utils.staged_files('*')
      result = check.run

      if !result
        $stderr.puts check.error_message
        $stderr.puts
        $stderr.puts 'pre-commit: You can bypass this check using `git commit -n`'
        $stderr.puts
      end

      result
    end

    def run
      # There is nothing to check
      return true if staged_files.empty?

      if detected_bad_code?
        @error_message = "pre-commit: detected :symbol => value hashrocket:\n"
        @error_message += violations[:lines]

        @passed = false
      else
        @passed = true
      end

      @passed
    end

    def detected_bad_code?
      violations[:success]
    end

    def violations
      @violations ||= begin
        lines = `#{Utils.grep} '#{HASHROCKET_PATTERN}' #{staged_files}`
        success = $?.exitstatus == 0

        { :lines => lines, :success => success}
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pre-commit-0.8.1 lib/pre-commit/checks/ruby_symbol_hashrockets.rb
pre-commit-0.8.0 lib/pre-commit/checks/ruby_symbol_hashrockets.rb