123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::CachedData Private

Relationships & Source Files
Inherits: Object
Defined in: lib/rubocop/cached_data.rb

Overview

Converts RuboCop objects to and from the serialization format JSON.

Class Method Summary

Instance Method Summary

Constructor Details

.new(filename) ⇒ CachedData

[ GitHub ]

  
# File 'lib/rubocop/cached_data.rb', line 9

def initialize(filename)
  @filename = filename
end

Instance Method Details

#deserialize_offenses(offenses) (private)

Restore an offense object loaded from a JSON file.

[ GitHub ]

  
# File 'lib/rubocop/cached_data.rb', line 51

def deserialize_offenses(offenses)
  offenses.map! do |o|
    location = location_from_source_buffer(o)
    Cop::Offense.new(o['severity'], location, o['message'], o['cop_name'], o['status'].to_sym,
                     justification: o['justification'])
  end
end

#from_json(text)

[ GitHub ]

  
# File 'lib/rubocop/cached_data.rb', line 13

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

#location_from_source_buffer(offense) (private)

[ GitHub ]

  
# File 'lib/rubocop/cached_data.rb', line 59

def location_from_source_buffer(offense)
  begin_pos = offense['location']['begin_pos']
  end_pos = offense['location']['end_pos']
  if begin_pos.zero? && end_pos.zero?
    Cop::Offense::NO_LOCATION
  else
    Parser::Source::Range.new(source_buffer, begin_pos, end_pos)
  end
end

#message(offense) (private)

[ GitHub ]

  
# File 'lib/rubocop/cached_data.rb', line 44

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

#serialize_offense(offense) (private)

[ GitHub ]

  
# File 'lib/rubocop/cached_data.rb', line 23

def serialize_offense(offense)
  status = :uncorrected if %i[corrected corrected_with_todo].include?(offense.status)
  hash = {
    # 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
  }
  # Only offenses suppressed by a directive can carry one, so keep the key out of the
  # common case rather than writing a null for every cached offense.
  hash[:justification] = offense.justification if offense.justification
  hash
end

#source_buffer (private)

Delay creation until needed. Some type of offenses will have no buffer associated with them and be global only. For these, trying to create the buffer will likely fail, for example because of unknown encoding comments.

[ GitHub ]

  
# File 'lib/rubocop/cached_data.rb', line 72

def source_buffer
  @source_buffer ||= begin
    source = File.read(@filename, encoding: Encoding::UTF_8)
    Parser::Source::Buffer.new(@filename, source: source)
  end
end

#to_json(offenses)

[ GitHub ]

  
# File 'lib/rubocop/cached_data.rb', line 17

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