Class: YARP::ClassVariableOrWriteNode

Inherits:
YARPNode
  • Object
show all
Defined in:
lib/yarp/node.rb,
ext/yarp/api_node.c

Overview

Represents the use of the ‘||=` operator for assignment to a class variable.

@@target ||= value
^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, name_loc, operator_loc, value, location) ⇒ ClassVariableOrWriteNode

def initialize: (name: Symbol, name_loc: Location, operator_loc: Location, value: Node, location: Location) -> void



2308
2309
2310
2311
2312
2313
2314
# File 'lib/yarp/node.rb', line 2308

def initialize(name, name_loc, operator_loc, value, location)
  @name = name
  @name_loc = name_loc
  @operator_loc = operator_loc
  @value = value
  @location = location
end

Instance Attribute Details

#nameObject (readonly)

attr_reader name: Symbol



2296
2297
2298
# File 'lib/yarp/node.rb', line 2296

def name
  @name
end

#name_locObject (readonly)

attr_reader name_loc: Location



2299
2300
2301
# File 'lib/yarp/node.rb', line 2299

def name_loc
  @name_loc
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



2302
2303
2304
# File 'lib/yarp/node.rb', line 2302

def operator_loc
  @operator_loc
end

#valueObject (readonly)

attr_reader value: Node



2305
2306
2307
# File 'lib/yarp/node.rb', line 2305

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



2317
2318
2319
# File 'lib/yarp/node.rb', line 2317

def accept(visitor)
  visitor.visit_class_variable_or_write_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



2322
2323
2324
# File 'lib/yarp/node.rb', line 2322

def child_nodes
  [value]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



2327
2328
2329
# File 'lib/yarp/node.rb', line 2327

def comment_targets
  [name_loc, operator_loc, value]
end

#copy(**params) ⇒ Object

def copy: (**params) -> ClassVariableOrWriteNode



2332
2333
2334
2335
2336
2337
2338
2339
2340
# File 'lib/yarp/node.rb', line 2332

def copy(**params)
  ClassVariableOrWriteNode.new(
    params.fetch(:name) { name },
    params.fetch(:name_loc) { name_loc },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:value) { value },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



2346
2347
2348
# File 'lib/yarp/node.rb', line 2346

def deconstruct_keys(keys)
  { name: name, name_loc: name_loc, operator_loc: operator_loc, value: value, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



2355
2356
2357
2358
2359
2360
2361
2362
2363
# File 'lib/yarp/node.rb', line 2355

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── name: #{name.inspect}\n"
  inspector << "├── name_loc: #{inspector.location(name_loc)}\n"
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector << "└── value:\n"
  inspector << inspector.child_node(value, "    ")
  inspector.to_str
end

#operatorObject

def operator: () -> String



2351
2352
2353
# File 'lib/yarp/node.rb', line 2351

def operator
  operator_loc.slice
end