Class: Prism::DefNode

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

Overview

Represents a method definition.

def method
end
^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, name, name_loc, receiver, parameters, body, locals, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, end_keyword_loc) ⇒ DefNode

Initialize a new DefNode node.



6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
# File 'lib/prism/node.rb', line 6295

def initialize(source, node_id, location, flags, name, name_loc, receiver, parameters, body, locals, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, end_keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @name = name
  @name_loc = name_loc
  @receiver = receiver
  @parameters = parameters
  @body = body
  @locals = locals
  @def_keyword_loc = def_keyword_loc
  @operator_loc = operator_loc
  @lparen_loc = lparen_loc
  @rparen_loc = rparen_loc
  @equal_loc = equal_loc
  @end_keyword_loc = end_keyword_loc
end

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: StatementsNode | BeginNode | nil



6383
6384
6385
# File 'lib/prism/node.rb', line 6383

def body
  @body
end

#localsObject (readonly)

attr_reader locals: Array



6386
6387
6388
# File 'lib/prism/node.rb', line 6386

def locals
  @locals
end

#nameObject (readonly)

attr_reader name: Symbol



6361
6362
6363
# File 'lib/prism/node.rb', line 6361

def name
  @name
end

#parametersObject (readonly)

attr_reader parameters: ParametersNode?



6380
6381
6382
# File 'lib/prism/node.rb', line 6380

def parameters
  @parameters
end

#receiverObject (readonly)

attr_reader receiver: Prism::node?



6377
6378
6379
# File 'lib/prism/node.rb', line 6377

def receiver
  @receiver
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See Node::type.



6537
6538
6539
# File 'lib/prism/node.rb', line 6537

def self.type
  :def_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
# File 'lib/prism/node.rb', line 6543

def ===(other)
  other.is_a?(DefNode) &&
    (name === other.name) &&
    (name_loc.nil? == other.name_loc.nil?) &&
    (receiver === other.receiver) &&
    (parameters === other.parameters) &&
    (body === other.body) &&
    (locals.length == other.locals.length) &&
    locals.zip(other.locals).all? { |left, right| left === right } &&
    (def_keyword_loc.nil? == other.def_keyword_loc.nil?) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (lparen_loc.nil? == other.lparen_loc.nil?) &&
    (rparen_loc.nil? == other.rparen_loc.nil?) &&
    (equal_loc.nil? == other.equal_loc.nil?) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



6315
6316
6317
# File 'lib/prism/node.rb', line 6315

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



6320
6321
6322
# File 'lib/prism/node.rb', line 6320

def child_nodes
  [receiver, parameters, body]
end

#comment_targetsObject

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



6343
6344
6345
# File 'lib/prism/node.rb', line 6343

def comment_targets
  [name_loc, *receiver, *parameters, *body, def_keyword_loc, *operator_loc, *lparen_loc, *rparen_loc, *equal_loc, *end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



6334
6335
6336
6337
6338
6339
6340
# File 'lib/prism/node.rb', line 6334

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << receiver if receiver
  compact << parameters if parameters
  compact << body if body
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, name: self.name, name_loc: self.name_loc, receiver: self.receiver, parameters: self.parameters, body: self.body, locals: self.locals, def_keyword_loc: self.def_keyword_loc, operator_loc: self.operator_loc, lparen_loc: self.lparen_loc, rparen_loc: self.rparen_loc, equal_loc: self.equal_loc, end_keyword_loc: self.end_keyword_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?name: Symbol, ?name_loc: Location, ?receiver: Prism::node?, ?parameters: ParametersNode?, ?body: StatementsNode | BeginNode | nil, ?locals: Array, ?def_keyword_loc: Location, ?operator_loc: Location?, ?lparen_loc: Location?, ?rparen_loc: Location?, ?equal_loc: Location?, ?end_keyword_loc: Location?) -> DefNode



6348
6349
6350
# File 'lib/prism/node.rb', line 6348

def copy(node_id: self.node_id, location: self.location, flags: self.flags, name: self.name, name_loc: self.name_loc, receiver: self.receiver, parameters: self.parameters, body: self.body, locals: self.locals, def_keyword_loc: self.def_keyword_loc, operator_loc: self.operator_loc, lparen_loc: self.lparen_loc, rparen_loc: self.rparen_loc, equal_loc: self.equal_loc, end_keyword_loc: self.end_keyword_loc)
  DefNode.new(source, node_id, location, flags, name, name_loc, receiver, parameters, body, locals, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, end_keyword_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, name: Symbol, name_loc: Location, receiver: Prism::node?, parameters: ParametersNode?, body: StatementsNode | BeginNode | nil, locals: Array, def_keyword_loc: Location, operator_loc: Location?, lparen_loc: Location?, rparen_loc: Location?, equal_loc: Location?, end_keyword_loc: Location? }



6356
6357
6358
# File 'lib/prism/node.rb', line 6356

def deconstruct_keys(keys)
  { node_id: node_id, location: location, name: name, name_loc: name_loc, receiver: receiver, parameters: parameters, body: body, locals: locals, def_keyword_loc: def_keyword_loc, operator_loc: operator_loc, lparen_loc: lparen_loc, rparen_loc: rparen_loc, equal_loc: equal_loc, end_keyword_loc: end_keyword_loc }
end

#def_keywordObject

def def_keyword: () -> String



6497
6498
6499
# File 'lib/prism/node.rb', line 6497

def def_keyword
  def_keyword_loc.slice
end

#def_keyword_locObject

attr_reader def_keyword_loc: Location



6389
6390
6391
6392
6393
# File 'lib/prism/node.rb', line 6389

def def_keyword_loc
  location = @def_keyword_loc
  return location if location.is_a?(Location)
  @def_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#each_child_node {|receiver| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



6325
6326
6327
6328
6329
6330
6331
# File 'lib/prism/node.rb', line 6325

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield receiver if receiver
  yield parameters if parameters
  yield body if body
end

#end_keywordObject

def end_keyword: () -> String?



6522
6523
6524
# File 'lib/prism/node.rb', line 6522

def end_keyword
  end_keyword_loc&.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location?



6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
# File 'lib/prism/node.rb', line 6478

def end_keyword_loc
  location = @end_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @end_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#equalObject

def equal: () -> String?



6517
6518
6519
# File 'lib/prism/node.rb', line 6517

def equal
  equal_loc&.slice
end

#equal_locObject

attr_reader equal_loc: Location?



6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
# File 'lib/prism/node.rb', line 6459

def equal_loc
  location = @equal_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @equal_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#inspectObject

def inspect -> String



6527
6528
6529
# File 'lib/prism/node.rb', line 6527

def inspect
  InspectVisitor.compose(self)
end

#lparenObject

def lparen: () -> String?



6507
6508
6509
# File 'lib/prism/node.rb', line 6507

def lparen
  lparen_loc&.slice
end

#lparen_locObject

attr_reader lparen_loc: Location?



6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
# File 'lib/prism/node.rb', line 6421

def lparen_loc
  location = @lparen_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @lparen_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#name_locObject

attr_reader name_loc: Location



6364
6365
6366
6367
6368
# File 'lib/prism/node.rb', line 6364

def name_loc
  location = @name_loc
  return location if location.is_a?(Location)
  @name_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#operatorObject

def operator: () -> String?



6502
6503
6504
# File 'lib/prism/node.rb', line 6502

def operator
  operator_loc&.slice
end

#operator_locObject

attr_reader operator_loc: Location?



6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
# File 'lib/prism/node.rb', line 6402

def operator_loc
  location = @operator_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#rparenObject

def rparen: () -> String?



6512
6513
6514
# File 'lib/prism/node.rb', line 6512

def rparen
  rparen_loc&.slice
end

#rparen_locObject

attr_reader rparen_loc: Location?



6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
# File 'lib/prism/node.rb', line 6440

def rparen_loc
  location = @rparen_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @rparen_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#save_def_keyword_loc(repository) ⇒ Object

Save the def_keyword_loc location using the given saved source so that it can be retrieved later.



6397
6398
6399
# File 'lib/prism/node.rb', line 6397

def save_def_keyword_loc(repository)
  repository.enter(node_id, :def_keyword_loc)
end

#save_end_keyword_loc(repository) ⇒ Object

Save the end_keyword_loc location using the given saved source so that it can be retrieved later.



6492
6493
6494
# File 'lib/prism/node.rb', line 6492

def save_end_keyword_loc(repository)
  repository.enter(node_id, :end_keyword_loc) unless @end_keyword_loc.nil?
end

#save_equal_loc(repository) ⇒ Object

Save the equal_loc location using the given saved source so that it can be retrieved later.



6473
6474
6475
# File 'lib/prism/node.rb', line 6473

def save_equal_loc(repository)
  repository.enter(node_id, :equal_loc) unless @equal_loc.nil?
end

#save_lparen_loc(repository) ⇒ Object

Save the lparen_loc location using the given saved source so that it can be retrieved later.



6435
6436
6437
# File 'lib/prism/node.rb', line 6435

def save_lparen_loc(repository)
  repository.enter(node_id, :lparen_loc) unless @lparen_loc.nil?
end

#save_name_loc(repository) ⇒ Object

Save the name_loc location using the given saved source so that it can be retrieved later.



6372
6373
6374
# File 'lib/prism/node.rb', line 6372

def save_name_loc(repository)
  repository.enter(node_id, :name_loc)
end

#save_operator_loc(repository) ⇒ Object

Save the operator_loc location using the given saved source so that it can be retrieved later.



6416
6417
6418
# File 'lib/prism/node.rb', line 6416

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc) unless @operator_loc.nil?
end

#save_rparen_loc(repository) ⇒ Object

Save the rparen_loc location using the given saved source so that it can be retrieved later.



6454
6455
6456
# File 'lib/prism/node.rb', line 6454

def save_rparen_loc(repository)
  repository.enter(node_id, :rparen_loc) unless @rparen_loc.nil?
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



6532
6533
6534
# File 'lib/prism/node.rb', line 6532

def type
  :def_node
end