Sha256: 01f5cb2c676cdf9f4cfe6626d3ac4a3157d937f66aa0d6e58e855c24e0e978b1

Contents?: true

Size: 685 Bytes

Versions: 1

Compression:

Stored size: 685 Bytes

Contents

# -*- encoding : utf-8 -*-
require 'json'
require 'pre-commit/checks/plugin'

module PreCommit
  module Checks
    # Checks JSON to make sure it's parsable.
    class Json < Plugin
      def call(staged_files)
        staged_files = staged_files.grep(/\.json$/)
        return if staged_files.empty?

        errors = staged_files.map {|file| load_file(file)}.compact

        errors.join("\n") unless errors.empty?
      end

      def load_file(file)
        File.open(file) {|io| JSON.load(io)}
        nil
      rescue StandardError => e
        "#{e.message} parsing #{file}"
      end

      def self.description
        'Runs json to detect errors.'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gazelle_styleguide-0.0.1 lib/plugins/pre_commit/checks/json.rb