Class: Oxc::Node
- Inherits:
-
Object
- Object
- Oxc::Node
- Includes:
- Enumerable, Enumerable[Oxc::Node]
- Defined in:
- lib/oxc/node.rb,
sig/oxc/node.rbs
Constant Summary collapse
- ACRONYM =
/([A-Z]+)([A-Z][a-z])/- BOUNDARY =
/([a-z\d])([A-Z])/- SPAN =
["type", "start", "end"].freeze
Instance Attribute Summary collapse
- #attributes ⇒ Hash[String, untyped] readonly
- #parent ⇒ Oxc::Node? readonly
- #text ⇒ String? readonly
Instance Method Summary collapse
-
#[](key) ⇒ Object
: (String) -> untyped.
-
#ancestors ⇒ Array[Oxc::Node]
: () -> Array.
-
#at(offset) ⇒ Oxc::Node?
: (Integer) -> Oxc::Node?.
-
#child_nodes ⇒ Array[Oxc::Node]
: () -> Array.
-
#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]
: (Array?) -> Hash[Symbol, untyped].
-
#described_field(key, value) ⇒ String
: (String, untyped) -> String.
-
#each {|arg0| ... } ⇒ Object
: () { (Oxc::Node) -> void } -> void : () -> Enumerator[Oxc::Node, void].
-
#every(type) ⇒ Array[Oxc::Node]
: (String) -> Array.
-
#field_for(name) ⇒ String?
: (String) -> String?.
-
#fields ⇒ Hash[String, untyped]
: () -> Hash[String, untyped].
-
#finish ⇒ Integer
: () -> Integer.
-
#initialize(attributes, parent = nil, text = nil) ⇒ Node
constructor
: (Hash[String, untyped], ?Oxc::Node?, ?String?) -> void.
-
#inspect ⇒ String
: () -> String.
-
#keys ⇒ Array[String]
: () -> Array.
-
#method_missing(name, *arguments) ⇒ Object
: (Symbol, *untyped) -> untyped.
-
#respond_to_missing?(name, include_private = false) ⇒ Boolean
: (Symbol, ?bool) -> bool.
-
#slice(from = text) ⇒ String?
: (?String?) -> String?.
-
#start ⇒ Integer
: () -> Integer.
-
#to_h ⇒ Hash[String, untyped]
: () -> Hash[String, untyped].
-
#to_json(state = nil) ⇒ String
: (?untyped) -> String.
-
#type ⇒ String
: () -> String.
-
#underscored_type ⇒ String
: () -> String.
-
#wrap(value) ⇒ Object
: (untyped) -> untyped.
Constructor Details
#initialize(attributes, parent = nil, text = nil) ⇒ Node
: (Hash[String, untyped], ?Oxc::Node?, ?String?) -> void
21 22 23 24 25 |
# File 'lib/oxc/node.rb', line 21 def initialize(attributes, parent = nil, text = nil) @attributes = attributes @parent = parent @text = text || parent&.text end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments) ⇒ Object
: (Symbol, *untyped) -> untyped
140 141 142 143 144 145 146 |
# File 'lib/oxc/node.rb', line 140 def method_missing(name, *arguments) key = field_for(name.to_s) return super unless key wrap(attributes[key]) end |
Instance Attribute Details
#attributes ⇒ Hash[String, untyped] (readonly)
15 16 17 |
# File 'lib/oxc/node.rb', line 15 def attributes @attributes end |
#text ⇒ String? (readonly)
16 17 18 |
# File 'lib/oxc/node.rb', line 16 def text @text end |
Instance Method Details
#[](key) ⇒ Object
: (String) -> untyped
48 49 50 |
# File 'lib/oxc/node.rb', line 48 def [](key) attributes[key] end |
#ancestors ⇒ Array[Oxc::Node]
: () -> Array
111 112 113 |
# File 'lib/oxc/node.rb', line 111 def ancestors parent ? [parent, *parent.ancestors] : [] end |
#at(offset) ⇒ Oxc::Node?
: (Integer) -> Oxc::Node?
116 117 118 119 120 |
# File 'lib/oxc/node.rb', line 116 def at(offset) covering = each.select { |node| offset >= node.start && offset < node.finish } covering.min_by { |node| node.finish - node.start } end |
#child_nodes ⇒ Array[Oxc::Node]
: () -> Array
92 93 94 95 96 97 98 |
# File 'lib/oxc/node.rb', line 92 def child_nodes @child_nodes ||= attributes.each_value .flat_map { |value| value.is_a?(Array) ? value : [value] } .select { |value| value.is_a?(Hash) && value.key?("type") } .map { |value| Node.new(value, self) } .freeze end |
#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]
: (Array?) -> Hash[Symbol, untyped]
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/oxc/node.rb', line 75 def deconstruct_keys(keys) found = {} #: Hash[Symbol, untyped] if keys keys.each do |name| key = field_for(name.to_s) found[name] = wrap(attributes[key]) if key end else attributes.each_key { |key| found[key.to_sym] = wrap(attributes[key]) } end found end |
#described_field(key, value) ⇒ String
: (String, untyped) -> String
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/oxc/node.rb', line 156 def described_field(key, value) case value when Array then "#{key}=#{if value.empty? "[]" else "[... #{value.length} #{value.length == 1 ? "item" : "items"}]" end}" when Hash then "#{key}=#<#{self.class.name} #{value["type"]}>" else "#{key}=#{value.inspect}" end end |
#each ⇒ void #each ⇒ Enumerator[Oxc::Node, void]
: () { (Oxc::Node) -> void } -> void : () -> Enumerator[Oxc::Node, void]
102 103 104 105 106 107 108 |
# File 'lib/oxc/node.rb', line 102 def each(&) return enum_for(:each) unless block_given? yield self child_nodes.each { |child| child.each(&) } end |
#every(type) ⇒ Array[Oxc::Node]
: (String) -> Array
123 124 125 |
# File 'lib/oxc/node.rb', line 123 def every(type) each.select { |node| node.type == type } end |
#field_for(name) ⇒ String?
: (String) -> String?
169 170 171 172 173 174 175 |
# File 'lib/oxc/node.rb', line 169 def field_for(name) return name if attributes.key?(name) camelized = name.gsub(/_([a-z\d])/) { Regexp.last_match(1).to_s.upcase } camelized if attributes.key?(camelized) end |
#fields ⇒ Hash[String, untyped]
: () -> Hash[String, untyped]
128 129 130 |
# File 'lib/oxc/node.rb', line 128 def fields attributes.except(*SPAN) end |
#finish ⇒ Integer
: () -> Integer
38 39 40 |
# File 'lib/oxc/node.rb', line 38 def finish attributes.fetch("end") end |
#inspect ⇒ String
: () -> String
133 134 135 136 137 |
# File 'lib/oxc/node.rb', line 133 def inspect described = fields.map { |key, value| described_field(key, value) } "#<#{self.class.name} #{type} range=[#{start}, #{finish}]#{" #{described.join(" ")}" unless described.empty?}>" end |
#keys ⇒ Array[String]
: () -> Array
60 61 62 |
# File 'lib/oxc/node.rb', line 60 def keys attributes.keys end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
: (Symbol, ?bool) -> bool
149 150 151 |
# File 'lib/oxc/node.rb', line 149 def respond_to_missing?(name, include_private = false) !field_for(name.to_s).nil? || super end |
#slice(from = text) ⇒ String?
: (?String?) -> String?
53 54 55 56 57 |
# File 'lib/oxc/node.rb', line 53 def slice(from = text) raise ArgumentError, "this node does not know its source, so #slice needs it" unless from from.byteslice(start, finish - start) end |
#start ⇒ Integer
: () -> Integer
33 34 35 |
# File 'lib/oxc/node.rb', line 33 def start attributes.fetch("start") end |
#to_h ⇒ Hash[String, untyped]
: () -> Hash[String, untyped]
65 66 67 |
# File 'lib/oxc/node.rb', line 65 def to_h attributes end |
#to_json(state = nil) ⇒ String
: (?untyped) -> String
70 71 72 |
# File 'lib/oxc/node.rb', line 70 def to_json(state = nil) state ? attributes.to_json(state) : attributes.to_json end |
#type ⇒ String
: () -> String
28 29 30 |
# File 'lib/oxc/node.rb', line 28 def type attributes.fetch("type") end |