Class: Prism::OptionalParameterNode

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

Overview

Represents an optional parameter to a method, block, or lambda definition.

def a(b = 1)
      ^^^^^
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, name, name_loc, operator_loc, value) ⇒ OptionalParameterNode

Initialize a new OptionalParameterNode node.



12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
# File 'lib/prism/node.rb', line 12555

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

Instance Attribute Details

#nameObject (readonly)

attr_reader name: Symbol



12605
12606
12607
# File 'lib/prism/node.rb', line 12605

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



12622
12623
12624
# File 'lib/prism/node.rb', line 12622

def value
  @value
end

Class Method Details

.typeObject

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



12640
12641
12642
# File 'lib/prism/node.rb', line 12640

def self.type
  :optional_parameter_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.



12646
12647
12648
12649
12650
12651
12652
12653
# File 'lib/prism/node.rb', line 12646

def ===(other)
  other.is_a?(OptionalParameterNode) &&
    (flags === other.flags) &&
    (name === other.name) &&
    (name_loc.nil? == other.name_loc.nil?) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (value === other.value)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



12567
12568
12569
# File 'lib/prism/node.rb', line 12567

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

#child_nodesObject Also known as: deconstruct

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



12572
12573
12574
# File 'lib/prism/node.rb', line 12572

def child_nodes
  [value]
end

#comment_targetsObject

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



12582
12583
12584
# File 'lib/prism/node.rb', line 12582

def comment_targets
  [name_loc, operator_loc, value] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



12577
12578
12579
# File 'lib/prism/node.rb', line 12577

def compact_child_nodes
  [value]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, name: self.name, name_loc: self.name_loc, operator_loc: self.operator_loc, value: self.value) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?name: Symbol, ?name_loc: Location, ?operator_loc: Location, ?value: Prism::node) -> OptionalParameterNode



12587
12588
12589
# File 'lib/prism/node.rb', line 12587

def copy(node_id: self.node_id, location: self.location, flags: self.flags, name: self.name, name_loc: self.name_loc, operator_loc: self.operator_loc, value: self.value)
  OptionalParameterNode.new(source, node_id, location, flags, name, name_loc, operator_loc, value)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, name: Symbol, name_loc: Location, operator_loc: Location, value: Prism::node }



12595
12596
12597
# File 'lib/prism/node.rb', line 12595

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

#inspectObject

def inspect -> String



12630
12631
12632
# File 'lib/prism/node.rb', line 12630

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



12608
12609
12610
12611
12612
# File 'lib/prism/node.rb', line 12608

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



12625
12626
12627
# File 'lib/prism/node.rb', line 12625

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



12615
12616
12617
12618
12619
# File 'lib/prism/node.rb', line 12615

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

#repeated_parameter?Boolean

def repeated_parameter?: () -> bool

Returns:

  • (Boolean)


12600
12601
12602
# File 'lib/prism/node.rb', line 12600

def repeated_parameter?
  flags.anybits?(ParameterFlags::REPEATED_PARAMETER)
end

#typeObject

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



12635
12636
12637
# File 'lib/prism/node.rb', line 12635

def type
  :optional_parameter_node
end