123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::ProjectIndexHelp

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/mixin/project_index_help.rb

Overview

Common helpers for cops that consult the project-wide static-analysis index via Base#project_index.

Mixed-in cops gain the #external_dependency_checksum override that invalidates the ::RuboCop::ResultCache whenever the indexed project files change on disk. To run index-backed analysis, cops should simply check whether project_index is non-nil; the runner only exposes a non-nil index when the user opted in via AllCops/UseProjectIndex and the underlying gem is available.

Constant Summary

Instance Method Summary

Instance Method Details

#external_dependency_checksum

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/project_index_help.rb', line 20

def external_dependency_checksum
  return nil unless project_index

  @external_dependency_checksum ||= Digest::SHA1.hexdigest(
    project_index_signature.join("\n")
  )
end

#project_index_signature (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/project_index_help.rb', line 30

def project_index_signature
  project_index.documents.filter_map do |doc|
    uri = doc.uri
    next if uri == BUILTIN_DOCUMENT_URI

    path = uri.delete_prefix(FILE_URI_PREFIX).sub(WINDOWS_DRIVE_PREFIX, '')
    mtime, size = begin
      stat = File.stat(path)
      [stat.mtime.to_f, stat.size]
    rescue StandardError
      [0, 0]
    end

    "#{path}:#{mtime}:#{size}"
  end.sort
end