Sha256: 245a3f73029f41a07128a0a850f10d2a074df5488da17312fa774920dac3d24e

Contents?: true

Size: 1.81 KB

Versions: 18

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require 'json'

module RuboCop
  # Converts RuboCop objects to and from the serialization format JSON.
  class CachedData
    def initialize(filename)
      @filename = filename
    end

    def from_json(text)
      deserialize_offenses(JSON.parse(text))
    end

    def to_json(offenses)
      JSON.dump(offenses.map { |o| serialize_offense(o) })
    end

    private

    def serialize_offense(offense)
      {
        # Calling #to_s here ensures that the serialization works when using
        # other json serializers such as Oj. Some of these gems do not call
        # #to_s implicitly.
        severity: offense.severity.to_s,
        location: {
          begin_pos: offense.location.begin_pos,
          end_pos: offense.location.end_pos
        },
        message:  message(offense),
        cop_name: offense.cop_name,
        status:   offense.status
      }
    end

    def message(offense)
      # JSON.dump will fail if the offense message contains text which is not
      # valid UTF-8
      message = offense.message
      if message.respond_to?(:scrub)
        message.scrub
      else
        message.chars.select(&:valid_encoding?).join
      end
    end

    # Restore an offense object loaded from a JSON file.
    def deserialize_offenses(offenses)
      source_buffer = Parser::Source::Buffer.new(@filename)
      source_buffer.source = File.read(@filename, encoding: Encoding::UTF_8)
      offenses.map! do |o|
        location = Parser::Source::Range.new(source_buffer,
                                             o['location']['begin_pos'],
                                             o['location']['end_pos'])
        Cop::Offense.new(o['severity'], location,
                         o['message'],
                         o['cop_name'], o['status'].to_sym)
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cached_data.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cached_data.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cached_data.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cached_data.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cached_data.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cached_data.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cached_data.rb
rubocop-0.50.0 lib/rubocop/cached_data.rb
rubocop-0.49.1 lib/rubocop/cached_data.rb
rubocop-0.49.0 lib/rubocop/cached_data.rb
rubocop-0.48.1 lib/rubocop/cached_data.rb
rubocop-0.48.0 lib/rubocop/cached_data.rb
rubocop-0.47.1 lib/rubocop/cached_data.rb
rubocop-0.47.0 lib/rubocop/cached_data.rb
rubocop-0.46.0 lib/rubocop/cached_data.rb
rubocop-0.45.0 lib/rubocop/cached_data.rb
rubocop-0.44.1 lib/rubocop/cached_data.rb
rubocop-0.44.0 lib/rubocop/cached_data.rb