Class: RubyModKit::Node::BaseNode
- Inherits:
-
Object
- Object
- RubyModKit::Node::BaseNode
show all
- Defined in:
- lib/ruby_mod_kit/node/base_node.rb
Overview
The class of transpile node.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#prev ⇒ Object
14
15
16
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 14
def prev
@prev
end
|
Instance Method Details
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 47
def ancestors
return @ancestors if @ancestors
parent = self.parent
@ancestors = if parent
[parent] + parent.ancestors
else
[]
end
end
|
26
27
28
29
30
31
32
33
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 26
def children
return @children if @children
prev = nil
@children = prism_node.child_nodes.compact.map do |prism_child_node|
prev = wrap(prism_child_node, prev: prev)
end
end
|
95
96
97
98
99
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 95
def def_node_at(offset)
node = node_at(offset) || return
[node, *node.ancestors].each { return _1 if _1.is_a?(Node::DefNode) }
nil
end
|
#def_parent_node_at(offset, allowed: nil) ⇒ Node::DefParentNode?
117
118
119
120
121
122
123
124
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 117
def def_parent_node_at(offset, allowed: nil)
node = node_at(offset) || return
[node, *node.ancestors].each do |ancestor_node|
return ancestor_node if ancestor_node.is_a?(Node::DefParentNode)
return nil if allowed && !allowed.include?(ancestor_node.class)
end
nil
end
|
#end_offset ⇒ Integer
142
143
144
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 142
def end_offset
location.end_offset
end
|
#include?(offset) ⇒ Boolean
130
131
132
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 130
def include?(offset)
self.offset <= offset && offset <= prism_node.location.end_offset
end
|
#inspect ⇒ String
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 154
def inspect
str = +"#<#{self.class} "
first = true
instance_variables.each do |ivar_name|
case ivar_name
when :@children, :@ancestors, :@parent
next
end
if first
first = false
else
str << ", "
end
str << "#{ivar_name}="
value = instance_variable_get(ivar_name)
str << (
case value
when Prism::Node
"#<#{value.class} location=#{value.location.inspect}>"
else
value.inspect
end
)
end
str << ">"
str
end
|
#location ⇒ Prism::Location
18
19
20
21
22
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 18
def location
return @location if defined?(@location)
@location = prism_node.location
end
|
#name ⇒ Symbol
66
67
68
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 66
def name
raise(RubyModKit::Error, "Expected ParameterNode but #{self.class}:#{prism_node.inspect}")
end
|
74
75
76
77
78
79
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 74
def node_at(offset)
return nil unless include?(offset)
child = children.find { _1.include?(offset) }
child&.node_at(offset) || self
end
|
#offset ⇒ Integer
136
137
138
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 136
def offset
location.start_offset
end
|
105
106
107
108
109
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 105
def parameter_node_at(offset)
node = node_at(offset) || return
[node, *node.ancestors].each { return _1 if _1.is_a?(Node::ParameterNode) }
nil
end
|
60
61
62
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 60
def parent
raise(RubyModKit::Error)
end
|
#slice ⇒ String
148
149
150
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 148
def slice
location.slice
end
|
85
86
87
88
89
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 85
def statements_node_at(offset)
node = node_at(offset) || return
[node, *node.ancestors].each { return _1 if _1.is_a?(Node::StatementsNode) }
nil
end
|
#wrap(prism_node, prev: nil) ⇒ Node::BaseNode
41
42
43
|
# File 'lib/ruby_mod_kit/node/base_node.rb', line 41
def wrap(prism_node, prev: nil)
Node.wrap(prism_node, parent: self, prev: prev)
end
|