Class: SyntaxTree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree_ext.rb,
lib/syntax_tree_ext/source_ext.rb,
lib/syntax_tree_ext/parent_node_ext.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parent_nodeObject

Returns the value of attribute parent_node.



15
16
17
# File 'lib/syntax_tree_ext/parent_node_ext.rb', line 15

def parent_node
  @parent_node
end

#sourceObject

Returns the value of attribute source.



15
16
17
# File 'lib/syntax_tree_ext/source_ext.rb', line 15

def source
  @source
end

Instance Method Details

#set_parent_node(source) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/syntax_tree_ext/parent_node_ext.rb', line 17

def set_parent_node(source)
  self.deconstruct_keys([]).filter { |key, _value| ![:location, :comments].include?(key) }.values.each do |child_node|
    if child_node.is_a?(Array)
      child_node.each do |child_child_node|
        next unless child_child_node.is_a?(Node)

        child_child_node.parent_node = self
        child_child_node.set_parent_node(source)
      end
    end

    next unless child_node.is_a?(Node)

    child_node.parent_node = self
    child_node.set_parent_node(source)
  end
end

#set_source(source) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/syntax_tree_ext/source_ext.rb', line 17

def set_source(source)
  self.source = source
  self.deconstruct_keys([]).filter { |key, _value| ![:location, :comments].include?(key) }.values.each do |child_node|
    if child_node.is_a?(Array)
      child_node.each do |child_child_node|
        next unless child_child_node.is_a?(Node)

        child_child_node.set_source(source)
      end
    end

    next unless child_node.is_a?(Node)

    child_node.set_source(source)
  end
end

#to_sourceObject



113
114
115
# File 'lib/syntax_tree_ext.rb', line 113

def to_source
  source[location.start_char...location.end_char]
end

#to_valueObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/syntax_tree_ext.rb', line 90

def to_value
  case self
  when SymbolLiteral
    value.value.to_sym
  when StringLiteral
    parts.map(&:to_value).join
  when FloatLiteral
    value.to_f
  when Int
    value.to_i
  when Kw
    value == 'true'
  when VarRef
    value.to_value
  when Const, Label, TStringContent, Ident
    value
  when ArrayLiteral
    contents ? contents.parts.map { |part| part.to_value } : []
  else
    self
  end
end