Class: PinkSpoon::DocExtractor::ConstantDefinitionFinder

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/pink_spoon/doc_extractor.rb

Overview

Walks a Prism AST and records the line where target is defined. Tracks the full qualified name via a scope stack, so a nested ‘module Restrictions` inside `module Routes` only matches “Routes::Restrictions”, never bare “Restrictions”.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ ConstantDefinitionFinder

Returns a new instance of ConstantDefinitionFinder.



361
362
363
364
365
# File 'lib/pink_spoon/doc_extractor.rb', line 361

def initialize(target)
  @target = target.delete_prefix("::")
  @scope  = []
  @line   = nil
end

Instance Attribute Details

#lineObject (readonly)

Returns the value of attribute line.



359
360
361
# File 'lib/pink_spoon/doc_extractor.rb', line 359

def line
  @line
end

Instance Method Details

#visit_class_node(node) ⇒ Object



371
372
373
# File 'lib/pink_spoon/doc_extractor.rb', line 371

def visit_class_node(node)
  push_scope(node.constant_path.slice.delete_prefix("::"), node.location.start_line) { super }
end

#visit_constant_write_node(node) ⇒ Object



375
376
377
378
379
# File 'lib/pink_spoon/doc_extractor.rb', line 375

def visit_constant_write_node(node)
  fqn = (@scope + [node.name.to_s]).join("::")
  @line ||= node.location.start_line if fqn == @target
  super
end

#visit_module_node(node) ⇒ Object



367
368
369
# File 'lib/pink_spoon/doc_extractor.rb', line 367

def visit_module_node(node)
  push_scope(node.constant_path.slice.delete_prefix("::"), node.location.start_line) { super }
end