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.



14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
# File 'lib/prism/node.rb', line 14940

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



14997
14998
14999
# File 'lib/prism/node.rb', line 14997

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



15026
15027
15028
# File 'lib/prism/node.rb', line 15026

def value
  @value
end

Class Method Details

.typeObject

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



15044
15045
15046
# File 'lib/prism/node.rb', line 15044

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.



15050
15051
15052
15053
15054
15055
15056
15057
# File 'lib/prism/node.rb', line 15050

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



14952
14953
14954
# File 'lib/prism/node.rb', line 14952

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



14957
14958
14959
# File 'lib/prism/node.rb', line 14957

def child_nodes
  [value]
end

#comment_targetsObject

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



14974
14975
14976
# File 'lib/prism/node.rb', line 14974

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



14969
14970
14971
# File 'lib/prism/node.rb', line 14969

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



14979
14980
14981
# File 'lib/prism/node.rb', line 14979

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 }



14987
14988
14989
# File 'lib/prism/node.rb', line 14987

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

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

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

Yields:



14962
14963
14964
14965
14966
# File 'lib/prism/node.rb', line 14962

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield value
end

#inspectObject

def inspect -> String



15034
15035
15036
# File 'lib/prism/node.rb', line 15034

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



15000
15001
15002
15003
15004
# File 'lib/prism/node.rb', line 15000

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



15029
15030
15031
# File 'lib/prism/node.rb', line 15029

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



15013
15014
15015
15016
15017
# File 'lib/prism/node.rb', line 15013

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



14992
14993
14994
# File 'lib/prism/node.rb', line 14992

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

#save_name_loc(repository) ⇒ Object

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



15008
15009
15010
# File 'lib/prism/node.rb', line 15008

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.



15021
15022
15023
# File 'lib/prism/node.rb', line 15021

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#typeObject

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



15039
15040
15041
# File 'lib/prism/node.rb', line 15039

def type
  :optional_parameter_node
end