Sha256: 96ffac59120c75a30214b03a3a96c2bde13c3fc25ecfb738a554259560064847

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

require 'pre-commit/base'
require 'pre-commit/utils'
require 'execjs'

module PreCommit
  class JsCheck

    def call
      js_files = reject_non_js(files_to_check)
      if should_run?(js_files)
        run(js_files)
      else
        # pretend the check passed and move on
        true
      end
    end

    def run(js_files)
      errors = []

      js_files.each do |file|
        error_list = Array(run_check(file))
        error_list.each { |error_object| errors << display_error(error_object, file) }
      end

      if errors.empty?
        true
      else
        $stderr.puts errors.join("\n")
        $stderr.puts
        $stderr.puts 'pre-commit: You can bypass this check using `git commit -n`'
        $stderr.puts
        false
      end
    end

    def should_run?(js_files)
      js_files.any?
    end

    def reject_non_js(staged_files)
      staged_files.select { |f| f =~ /\.js$/ }
    end

    def check_name
      raise "Must be defined by subclass"
    end

    def linter_src
      raise "Must be defined by subclass"
    end

    def display_error(error_object, file)
      return "" unless error_object

      line = error_object['line'].to_i + 1
      "pre-commit: #{check_name.upcase} #{error_object['reason']}\n#{file}:#{line} #{error_object['evidence']}"
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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