Sha256: 5485b232b66db9369fdfc29c95d65c0c946e773575051db4ff38557c3547c616

Contents?: true

Size: 1.78 KB

Versions: 213

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require 'json'

module RuboCop
  # Converts RuboCop objects to and from the serialization format JSON.
  # @api private
  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)
      status = :uncorrected if %i[corrected corrected_with_todo].include?(offense.status)
      {
        # 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:   status || offense.status
      }
    end

    def message(offense)
      # JSON.dump will fail if the offense message contains text which is not
      # valid UTF-8
      offense.message.dup.force_encoding(::Encoding::UTF_8).scrub
    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

213 entries across 204 versions & 19 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cached_data.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cached_data.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cached_data.rb
rubocop-1.63.2 lib/rubocop/cached_data.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cached_data.rb
rubocop-1.63.1 lib/rubocop/cached_data.rb
rubocop-1.63.0 lib/rubocop/cached_data.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cached_data.rb
rubocop-1.62.1 lib/rubocop/cached_data.rb
rubocop-1.62.0 lib/rubocop/cached_data.rb
rubocop-1.61.0 lib/rubocop/cached_data.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cached_data.rb
rubocop-1.60.2 lib/rubocop/cached_data.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cached_data.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cached_data.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cached_data.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cached_data.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cached_data.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cached_data.rb
rubocop-1.60.1 lib/rubocop/cached_data.rb