Class: Sloprb::Parser::Visitor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, comments) ⇒ Visitor

Returns a new instance of Visitor.



10
11
12
13
14
15
16
# File 'lib/sloprb/parser.rb', line 10

def initialize(source, comments)
  super()
  @source = source
  @comments = comments
  @context_stack = []
  @slop_methods = []
end

Instance Attribute Details

#slop_methodsObject (readonly)

Returns the value of attribute slop_methods.



8
9
10
# File 'lib/sloprb/parser.rb', line 8

def slop_methods
  @slop_methods
end

Instance Method Details

#visit_class_node(node) ⇒ Object



18
19
20
21
22
# File 'lib/sloprb/parser.rb', line 18

def visit_class_node(node)
  @context_stack.push(constant_name(node))
  super
  @context_stack.pop
end

#visit_def_node(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sloprb/parser.rb', line 30

def visit_def_node(node)
  def_line = node.location.start_line

  trailing = @comments.find { |c|
    c.location.start_line == def_line &&
      c.location.start_column > node.name_loc.end_column
  }

  if trailing&.location&.slice&.include?(":slop:")
    params = node.parameters ? node.location.slice[/\(.*?\)/] : nil
    leading = collect_leading_comments(def_line)
    sketch = extract_body_sketch(node)

    @slop_methods << SlopMethod.new(
      name: node.name.to_s,
      parameters: params,
      start_offset: node.location.start_offset,
      end_offset: node.location.end_offset,
      leading_comments: leading,
      enclosing_context: @context_stack.dup,
      start_column: node.location.start_column,
      body_sketch: sketch
    )
  end

  super
end

#visit_module_node(node) ⇒ Object



24
25
26
27
28
# File 'lib/sloprb/parser.rb', line 24

def visit_module_node(node)
  @context_stack.push(constant_name(node))
  super
  @context_stack.pop
end