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.



14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
# File 'lib/prism/node.rb', line 14106

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



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

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



14185
14186
14187
# File 'lib/prism/node.rb', line 14185

def value
  @value
end

Class Method Details

.typeObject

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



14203
14204
14205
# File 'lib/prism/node.rb', line 14203

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.



14209
14210
14211
14212
14213
14214
14215
14216
# File 'lib/prism/node.rb', line 14209

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



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

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



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

def child_nodes
  [value]
end

#comment_targetsObject

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



14133
14134
14135
# File 'lib/prism/node.rb', line 14133

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



14128
14129
14130
# File 'lib/prism/node.rb', line 14128

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



14138
14139
14140
# File 'lib/prism/node.rb', line 14138

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 }



14146
14147
14148
# File 'lib/prism/node.rb', line 14146

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



14193
14194
14195
# File 'lib/prism/node.rb', line 14193

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



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

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



14188
14189
14190
# File 'lib/prism/node.rb', line 14188

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



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

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)


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

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.



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

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.



14180
14181
14182
# File 'lib/prism/node.rb', line 14180

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

#typeObject

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



14198
14199
14200
# File 'lib/prism/node.rb', line 14198

def type
  :optional_parameter_node
end