Class: NodeQuery::PrismAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/node_query/adapter/prism.rb

Instance Method Summary collapse

Instance Method Details

#get_children(node) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/node_query/adapter/prism.rb', line 19

def get_children(node)
  keys = []
  children = []
  node.deconstruct_keys([]).each do |key, value|
    next if [:node_id, :flags, :location].include?(key)

    if key.to_s.end_with?('_loc')
      new_key = key.to_s[0..-5]
      unless keys.include?(new_key)
        keys << new_key
        children << node.send(new_key)
      end
    else
      unless keys.include?(key.to_s)
        keys << key.to_s
        children << value
      end
    end
  end
  children
end

#get_node_type(node) ⇒ Object



11
12
13
# File 'lib/node_query/adapter/prism.rb', line 11

def get_node_type(node)
  node.type
end

#get_siblings(node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/node_query/adapter/prism.rb', line 41

def get_siblings(node)
  child_nodes = get_children(node.parent_node)
  if child_nodes.is_a?(Array) && child_nodes.size == 1 && child_nodes.first.is_a?(Array)
    index = child_nodes.first.index(node)
    return child_nodes.first[index + 1...]
  end

  index = child_nodes.index(node)
  child_nodes[index + 1...]
end

#get_source(node) ⇒ Object



15
16
17
# File 'lib/node_query/adapter/prism.rb', line 15

def get_source(node)
  node.to_source
end

#is_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/node_query/adapter/prism.rb', line 7

def is_node?(node)
  node.is_a?(Prism::Node)
end