Module: Txray::NodeHelpers

Defined in:
lib/txray/node_helpers.rb

Class Method Summary collapse

Class Method Details

.block_body(call) ⇒ Object



40
41
42
# File 'lib/txray/node_helpers.rb', line 40

def block_body(call)
  call.block.body if call.block.is_a?(Prism::BlockNode)
end

.constant_name(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/txray/node_helpers.rb', line 14

def constant_name(node)
  case node
  when Prism::ConstantReadNode
    node.name.to_s
  when Prism::ConstantPathNode
    [ constant_name(node.parent), constant_path_child(node) ].compact.reject(&:empty?).join("::")
  when Prism::SelfNode
    "self"
  end
end

.constant_path_child(node) ⇒ Object



25
26
27
28
29
30
# File 'lib/txray/node_helpers.rb', line 25

def constant_path_child(node)
  return node.name.to_s if node.respond_to?(:name) && node.name
  return constant_name(node.child) if node.respond_to?(:child)

  nil
end

.dynamic_receiver_name(node) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/txray/node_helpers.rb', line 48

def dynamic_receiver_name(node)
  case node
  when Prism::CallNode
    [ receiver_name(node), node.name ].compact.join(".")
  when Prism::InstanceVariableReadNode, Prism::GlobalVariableReadNode, Prism::LocalVariableReadNode
    node.name.to_s
  end
end

.each_node(node, &block) ⇒ Object



7
8
9
10
11
12
# File 'lib/txray/node_helpers.rb', line 7

def each_node(node, &block)
  return if node.nil?

  block.call(node)
  node.compact_child_nodes.each { |child| each_node(child, &block) }
end

.positional_arguments(call) ⇒ Object



36
37
38
# File 'lib/txray/node_helpers.rb', line 36

def positional_arguments(call)
  call.arguments&.arguments.to_a
end

.receiver_name(call) ⇒ Object



44
45
46
# File 'lib/txray/node_helpers.rb', line 44

def receiver_name(call)
  constant_name(call.receiver) || dynamic_receiver_name(call.receiver)
end

.snippet(node, limit = 90) ⇒ Object



57
58
59
60
# File 'lib/txray/node_helpers.rb', line 57

def snippet(node, limit = 90)
  text = node.slice.to_s.lines.first.to_s.strip
  text.length > limit ? "#{text[0, limit - 3]}..." : text
end

.symbol_arguments(call) ⇒ Object



32
33
34
# File 'lib/txray/node_helpers.rb', line 32

def symbol_arguments(call)
  positional_arguments(call).grep(Prism::SymbolNode).map { |symbol| symbol.unescaped.to_sym }
end