Module: RuboCop::ProjectIndexLoader
| Relationships & Source Files | |
| Defined in: | lib/rubocop/project_index_loader.rb |
Overview
Defensive loader for the optional rubydex gem.
When AllCops/UseProjectIndex is enabled in the user’s configuration, RuboCop builds
a project-wide index using Rubydex::Graph and exposes it to cops that opt in.
The gem is intentionally not a runtime dependency; if it is not installed,
or if the running Ruby is older than what rubydex supports, RuboCop falls back to
its standard file-local behavior.
Constant Summary
-
MINIMUM_RUBY_VERSION =
# File 'lib/rubocop/project_index_loader.rb', line 12'3.2'
Class Attribute Summary
-
.available? ⇒ Boolean
readonly
mod_func
Returns whether the
rubydexgem can be loaded. - .supported_ruby? ⇒ Boolean readonly mod_func
Class Method Summary
-
.build_index(paths)
mod_func
Builds and resolves a
Rubydex::Graphfor the given file paths. - .try_require mod_func
- .warn_unavailable mod_func
Class Attribute Details
.available? ⇒ Boolean (readonly, mod_func)
Returns whether the rubydex gem can be loaded. The result is memoized
for the lifetime of the process.
# File 'lib/rubocop/project_index_loader.rb', line 18
def available? return @available if defined?(@available) @available = supported_ruby? && try_require end
.supported_ruby? ⇒ Boolean (readonly, mod_func)
[ GitHub ]
# File 'lib/rubocop/project_index_loader.rb', line 24
def supported_ruby? RUBY_VERSION >= MINIMUM_RUBY_VERSION end
Class Method Details
.build_index(paths) (mod_func)
Builds and resolves a Rubydex::Graph for the given file paths. Returns the resolved graph,
or nil if the build fails. This is the only place in RuboCop that depends on the concrete
Rubydex::Graph API, so callers (e.g. Runner) do not need to know which Rubydex classes
or methods are involved.
# File 'lib/rubocop/project_index_loader.rb', line 57
def build_index(paths) graph = Rubydex::Graph.new graph.index_all(paths.map(&:to_s)) graph.resolve graph rescue StandardError => e warn Rainbow("rubydex index build failed: #{e.}. Continuing without it.").yellow end
.try_require (mod_func)
[ GitHub ]# File 'lib/rubocop/project_index_loader.rb', line 46
def try_require require 'rubydex' true rescue LoadError false end