Module: Hashira::Analysis::References

Defined in:
lib/hashira/analysis/references.rb

Class Method Summary collapse

Class Method Details

.collect(node, nesting, accumulator, roots, home = nesting) ⇒ Object



18
19
20
21
22
23
# File 'lib/hashira/analysis/references.rb', line 18

def collect(node, nesting, accumulator, roots, home = nesting)
  return unless node
  return accumulator << sighting(node, nesting, home) if constant?(node)
  return enter(node, nesting, accumulator, roots) if definition?(node)
  node.compact_child_nodes.each { collect(it, nesting, accumulator, roots, home) }
end

.constant?(node) ⇒ Boolean

Returns:

  • (Boolean)


29
# File 'lib/hashira/analysis/references.rb', line 29

def constant?(node) = node.is_a?(Prism::ConstantPathNode) || node.is_a?(Prism::ConstantReadNode)

.definition?(node) ⇒ Boolean

Returns:

  • (Boolean)


31
# File 'lib/hashira/analysis/references.rb', line 31

def definition?(node) = node.is_a?(Prism::ClassNode) || node.is_a?(Prism::ModuleNode)

.enter(node, nesting, accumulator, roots) ⇒ Object



33
34
35
36
37
# File 'lib/hashira/analysis/references.rb', line 33

def enter(node, nesting, accumulator, roots)
  opened = nesting + [Syntax.anchor(nesting, Syntax.segments(node.constant_path), roots)]
  collect(node.superclass, nesting, accumulator, roots, opened) if node.is_a?(Prism::ClassNode)
  collect(node.body, opened, accumulator, roots)
end

.list(tree) ⇒ Object



10
11
12
# File 'lib/hashira/analysis/references.rb', line 10

def list(tree)
  sightings(tree).map(&:first)
end

.sighting(node, nesting, home) ⇒ Object



25
26
27
# File 'lib/hashira/analysis/references.rb', line 25

def sighting(node, nesting, home)
  [Syntax.segments(node), node.location.start_line, Syntax.cbase?(node) ? nil : nesting, home]
end

.sightings(tree, roots = nil) ⇒ Object



14
15
16
# File 'lib/hashira/analysis/references.rb', line 14

def sightings(tree, roots = nil)
  [].tap { collect(tree, [], it, roots) }
end