Class: Prism::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/prism_ext.rb,
lib/prism_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/prism_ext/parent_node_ext.rb', line 15

def parent_node
  @parent_node
end

Instance Method Details

#interpolated_string?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/prism_ext.rb', line 122

def interpolated_string?
  (is_a?(InterpolatedStringNode) || is_a?(StringNode)) && opening.start_with?('<<')
end

#set_parent_nodeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/prism_ext/parent_node_ext.rb', line 17

def set_parent_node
  self.compact_child_nodes.each do |child_node|
    if child_node.is_a?(Array)
      child_node.each do |child_child_node|
        child_child_node.parent_node = self
        child_child_node.set_parent_node
      end
    else
      child_node.parent_node = self
      child_node.set_parent_node
    end
  end
end

#to_sourceObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/prism_ext.rb', line 105

def to_source
  case self
  when InterpolatedStringNode, StringNode
    if opening&.start_with?('<<')
      range = opening_loc.start_character_offset...closing_loc.end_character_offset
    end
  when CallNode
    if arguments.is_a?(ArgumentsNode) && arguments.arguments.any?(&:interpolated_string?)
      last_interpolated_string_argument = arguments.arguments.select(&:interpolated_string?).last
      range = location.start_character_offset...last_interpolated_string_argument.closing_loc.end_character_offset
    elsif block.is_a?(BlockArgumentNode)
      range = location.start_character_offset...closing_loc.end_character_offset
    end
  end
  range ? location.send(:source).source[range] : slice
end

#to_valueObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/prism_ext.rb', line 82

def to_value
  case self
  when SymbolNode
    value.to_sym
  when StringNode
    content
  when FloatNode
    value
  when IntegerNode
    value
  when TrueNode
    true
  when FalseNode
    false
  when NilNode
    nil
  when ArrayNode
    elements.map { |element| element.to_value }
  else
    self
  end
end