Class: Moult::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/moult/index.rb

Overview

The definition/reference index — Moult's adapter over the rubydex gem and the only file that names Rubydex. Everything downstream consumes the Moult-owned Definition value object, never a rubydex type, so the backend is swappable (the "swap, not rewrite" invariant).

rubydex has two quirks this adapter normalises away (see test/test_index.rb):

  • Its locations are 0-based; Moult/Prism are 1-based. We add 1 to line numbers so dead-code symbol ids line up with Scoring's hotspot ids.
  • Method references are not resolved to their target declaration (only constants are). So a method is considered "referenced" when its bare name appears anywhere in the call-site collection. This is deliberately conservative: a name collision can only hide a dead method, never invent a false positive — the safe direction for a confidence-graded tool.

Defined Under Namespace

Classes: Definition

Constant Summary collapse

BUILTIN_SCHEME =
"file:"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph:, root:) ⇒ Index

Returns a new instance of Index.



64
65
66
67
68
69
70
71
72
# File 'lib/moult/index.rb', line 64

def initialize(graph:, root:)
  @graph = graph
  # rubydex reports canonical (symlink-resolved) paths, so the root must be
  # canonicalised too or workspace filtering misses everything on systems
  # where e.g. /tmp -> /private/tmp.
  @root = File.realpath(root.to_s)
rescue Errno::ENOENT
  @root = root.to_s
end

Instance Attribute Details

#override_ofObject (readonly)

FQ name of the ancestor whose method this overrides/implements (reachable via that interface), else nil



33
34
35
36
37
# File 'lib/moult/index.rb', line 33

Definition = Struct.new(
  :symbol_id, :kind, :name, :unqualified_name, :owner,
  :visibility, :singleton, :span, :path,
  :reference_count, :reference_paths, :override_of
)

#reference_countObject (readonly)

resolvable usages of this definition



33
34
35
36
37
# File 'lib/moult/index.rb', line 33

Definition = Struct.new(
  :symbol_id, :kind, :name, :unqualified_name, :owner,
  :visibility, :singleton, :span, :path,
  :reference_count, :reference_paths, :override_of
)

#reference_pathsObject (readonly)

root-relative paths that use it



33
34
35
36
37
# File 'lib/moult/index.rb', line 33

Definition = Struct.new(
  :symbol_id, :kind, :name, :unqualified_name, :owner,
  :visibility, :singleton, :span, :path,
  :reference_count, :reference_paths, :override_of
)

Class Method Details

.available?Boolean

Whether the rubydex backend is loadable. Always true once the gem is a hard dependency, but kept so live integration tests can skip cleanly.

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/moult/index.rb', line 56

def available?
  require "rubydex"
  true
rescue LoadError
  false
end

.build(root:, paths:) ⇒ Index

Parameters:

  • root (String)

    absolute analysis root

  • paths (Array<String>)

    absolute paths of Ruby files to index

Returns:



45
46
47
48
49
50
51
52
# File 'lib/moult/index.rb', line 45

def build(root:, paths:)
  graph = Rubydex::Graph.new
  graph.index_all(Array(paths))
  graph.resolve
  new(graph: graph, root: root)
rescue => e
  raise Moult::Error, "rubydex indexing failed: #{e.class}: #{e.message}"
end

Instance Method Details

#definitionsArray<Index::Definition>

Returns method + constant definition sites defined within the workspace, each with its resolved reference count.

Returns:

  • (Array<Index::Definition>)

    method + constant definition sites defined within the workspace, each with its resolved reference count.



76
77
78
# File 'lib/moult/index.rb', line 76

def definitions
  @definitions ||= method_definitions + constant_definitions
end

#diagnosticsArray<String>

Returns human-readable index diagnostics (non-fatal).

Returns:

  • (Array<String>)

    human-readable index diagnostics (non-fatal).



85
86
87
88
89
# File 'lib/moult/index.rb', line 85

def diagnostics
  @graph.diagnostics.map(&:to_s)
rescue
  []
end

#kind=(value) ⇒ Object

:method or :constant



33
34
35
36
37
# File 'lib/moult/index.rb', line 33

Definition = Struct.new(
  :symbol_id, :kind, :name, :unqualified_name, :owner,
  :visibility, :singleton, :span, :path,
  :reference_count, :reference_paths, :override_of
)

#resolved?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/moult/index.rb', line 80

def resolved?
  true
end

#singleton=(value) ⇒ Object

true for Class.method / constants



33
34
35
36
37
# File 'lib/moult/index.rb', line 33

Definition = Struct.new(
  :symbol_id, :kind, :name, :unqualified_name, :owner,
  :visibility, :singleton, :span, :path,
  :reference_count, :reference_paths, :override_of
)

#visibility=(value) ⇒ Object

:public, :private, :protected



33
34
35
36
37
# File 'lib/moult/index.rb', line 33

Definition = Struct.new(
  :symbol_id, :kind, :name, :unqualified_name, :owner,
  :visibility, :singleton, :span, :path,
  :reference_count, :reference_paths, :override_of
)