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.



14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
# File 'lib/prism/node.rb', line 14069

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



14119
14120
14121
# File 'lib/prism/node.rb', line 14119

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



14148
14149
14150
# File 'lib/prism/node.rb', line 14148

def value
  @value
end

Class Method Details

.typeObject

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



14166
14167
14168
# File 'lib/prism/node.rb', line 14166

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.



14172
14173
14174
14175
14176
14177
14178
14179
# File 'lib/prism/node.rb', line 14172

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



14081
14082
14083
# File 'lib/prism/node.rb', line 14081

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



14086
14087
14088
# File 'lib/prism/node.rb', line 14086

def child_nodes
  [value]
end

#comment_targetsObject

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



14096
14097
14098
# File 'lib/prism/node.rb', line 14096

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



14091
14092
14093
# File 'lib/prism/node.rb', line 14091

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



14101
14102
14103
# File 'lib/prism/node.rb', line 14101

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 }



14109
14110
14111
# File 'lib/prism/node.rb', line 14109

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



14156
14157
14158
# File 'lib/prism/node.rb', line 14156

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



14122
14123
14124
14125
14126
# File 'lib/prism/node.rb', line 14122

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



14151
14152
14153
# File 'lib/prism/node.rb', line 14151

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



14135
14136
14137
14138
14139
# File 'lib/prism/node.rb', line 14135

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)


14114
14115
14116
# File 'lib/prism/node.rb', line 14114

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.



14130
14131
14132
# File 'lib/prism/node.rb', line 14130

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.



14143
14144
14145
# File 'lib/prism/node.rb', line 14143

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

#typeObject

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



14161
14162
14163
# File 'lib/prism/node.rb', line 14161

def type
  :optional_parameter_node
end