Class: Moult::Parser::Visitor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/moult/parser.rb

Overview

Walks the AST tracking lexical class/module nesting and class << self context so each def gets a fully-qualified lexical name.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVisitor

Returns a new instance of Visitor.



41
42
43
44
45
46
# File 'lib/moult/parser.rb', line 41

def initialize
  @namespace = []         # stack of constant-path strings, e.g. ["A", "B::C"]
  @singleton_context = [] # truthy frame == inside `class << self`
  @methods = []
  super
end

Instance Attribute Details

#methodsObject (readonly)

Returns the value of attribute methods.



39
40
41
# File 'lib/moult/parser.rb', line 39

def methods
  @methods
end

Instance Method Details

#visit_class_node(node) ⇒ Object



48
49
50
51
52
# File 'lib/moult/parser.rb', line 48

def visit_class_node(node)
  @namespace.push(node.constant_path.slice)
  super
  @namespace.pop
end

#visit_def_node(node) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/moult/parser.rb', line 69

def visit_def_node(node)
  @methods << MethodDef.new(
    name: qualified_name(node),
    span: span_for(node),
    node: node
  )
  super
end

#visit_module_node(node) ⇒ Object



54
55
56
57
58
# File 'lib/moult/parser.rb', line 54

def visit_module_node(node)
  @namespace.push(node.constant_path.slice)
  super
  @namespace.pop
end

#visit_singleton_class_node(node) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/moult/parser.rb', line 60

def visit_singleton_class_node(node)
  # `class << self` makes nested defs singleton (class) methods of the
  # enclosing namespace. Other `class << obj` forms are visited but not
  # specially qualified.
  @singleton_context.push(node.expression.is_a?(Prism::SelfNode))
  super
  @singleton_context.pop
end