Module: Rigor::Source::ConstantPath

Defined in:
lib/rigor/source/constant_path.rb

Overview

Flattens a Prism constant-reference node (‘ConstantReadNode` / `ConstantPathNode`) to its source-qualified `“A::B::C”` string.

Two nil policies for the one edge case that distinguishes the call sites — a constant path rooted in a dynamic base (‘expr::Bar`, where the left side is a runtime expression rather than a constant):

  • ConstantPath.qualified_name / ConstantPath.render are LENIENT — they drop the dynamic segment and render the trailing constant names (‘expr::Bar` => “Bar”). The scope indexer and statement evaluator feed only genuine class/module path nodes and want a best-effort name.

  • ConstantPath.qualified_name_or_nil is STRICT — a dynamic base anywhere in the chain yields ‘nil`, so a caller that statically names constants can treat the path as opaque rather than guessing.

A leading ‘::` (absolute root, `::Foo`) renders as `“Foo”` under both policies. A node that is neither a `ConstantReadNode` nor a `ConstantPathNode` yields `nil` under both.

Class Method Summary collapse

Class Method Details

.qualified_name(node) ⇒ Object

Lenient dispatch over a constant-reference node.



27
28
29
30
31
32
# File 'lib/rigor/source/constant_path.rb', line 27

def qualified_name(node)
  case node
  when Prism::ConstantReadNode then node.name.to_s
  when Prism::ConstantPathNode then render(node)
  end
end

.qualified_name_or_nil(node) ⇒ Object

Strict dispatch: a dynamic base anywhere in the path yields nil.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rigor/source/constant_path.rb', line 46

def qualified_name_or_nil(node)
  case node
  when Prism::ConstantReadNode
    node.name.to_s
  when Prism::ConstantPathNode
    parent = node.parent
    return node.name.to_s if parent.nil?

    parent_name = qualified_name_or_nil(parent)
    return nil if parent_name.nil?

    "#{parent_name}::#{node.name}"
  end
end

.render(node) ⇒ Object

Lenient render of a ‘ConstantPathNode`; never nil for a path node.



35
36
37
38
39
40
41
42
43
# File 'lib/rigor/source/constant_path.rb', line 35

def render(node)
  prefix =
    case node.parent
    when Prism::ConstantReadNode then "#{node.parent.name}::"
    when Prism::ConstantPathNode then "#{render(node.parent)}::"
    else ""
    end
  "#{prefix}#{node.name}"
end