Class: Prism::CallOperatorWriteNode

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

Overview

Represents the use of an assignment operator on a call.

foo.bar += baz
^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, call_operator_loc, message_loc, opening_loc, arguments, closing_loc, flags, read_name, write_name, operator, operator_loc, value, location) ⇒ CallOperatorWriteNode

def initialize: (receiver: Node?, call_operator_loc: Location?, message_loc: Location?, opening_loc: Location?, arguments: ArgumentsNode?, closing_loc: Location?, flags: Integer, read_name: String, write_name: String, operator: Symbol, operator_loc: Location, value: Node, location: Location) -> void



2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
# File 'lib/prism/node.rb', line 2148

def initialize(receiver, call_operator_loc, message_loc, opening_loc, arguments, closing_loc, flags, read_name, write_name, operator, operator_loc, value, location)
  @receiver = receiver
  @call_operator_loc = call_operator_loc
  @message_loc = message_loc
  @opening_loc = opening_loc
  @arguments = arguments
  @closing_loc = closing_loc
  @flags = flags
  @read_name = read_name
  @write_name = write_name
  @operator = operator
  @operator_loc = operator_loc
  @value = value
  @location = location
end

Instance Attribute Details

#argumentsObject (readonly)

attr_reader arguments: ArgumentsNode?



2124
2125
2126
# File 'lib/prism/node.rb', line 2124

def arguments
  @arguments
end

#call_operator_locObject (readonly)

attr_reader call_operator_loc: Location?



2115
2116
2117
# File 'lib/prism/node.rb', line 2115

def call_operator_loc
  @call_operator_loc
end

#closing_locObject (readonly)

attr_reader closing_loc: Location?



2127
2128
2129
# File 'lib/prism/node.rb', line 2127

def closing_loc
  @closing_loc
end

#message_locObject (readonly)

attr_reader message_loc: Location?



2118
2119
2120
# File 'lib/prism/node.rb', line 2118

def message_loc
  @message_loc
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



2121
2122
2123
# File 'lib/prism/node.rb', line 2121

def opening_loc
  @opening_loc
end

#operatorObject (readonly)

attr_reader operator: Symbol



2139
2140
2141
# File 'lib/prism/node.rb', line 2139

def operator
  @operator
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



2142
2143
2144
# File 'lib/prism/node.rb', line 2142

def operator_loc
  @operator_loc
end

#read_nameObject (readonly)

attr_reader read_name: String



2133
2134
2135
# File 'lib/prism/node.rb', line 2133

def read_name
  @read_name
end

#receiverObject (readonly)

attr_reader receiver: Node?



2112
2113
2114
# File 'lib/prism/node.rb', line 2112

def receiver
  @receiver
end

#valueObject (readonly)

attr_reader value: Node



2145
2146
2147
# File 'lib/prism/node.rb', line 2145

def value
  @value
end

#write_nameObject (readonly)

attr_reader write_name: String



2136
2137
2138
# File 'lib/prism/node.rb', line 2136

def write_name
  @write_name
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



2165
2166
2167
# File 'lib/prism/node.rb', line 2165

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

#call_operatorObject

def call_operator: () -> String?



2216
2217
2218
# File 'lib/prism/node.rb', line 2216

def call_operator
  call_operator_loc&.slice
end

#child_nodesObject Also known as: deconstruct

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



2170
2171
2172
# File 'lib/prism/node.rb', line 2170

def child_nodes
  [receiver, arguments, value]
end

#closingObject

def closing: () -> String?



2231
2232
2233
# File 'lib/prism/node.rb', line 2231

def closing
  closing_loc&.slice
end

#comment_targetsObject

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



2184
2185
2186
# File 'lib/prism/node.rb', line 2184

def comment_targets
  [*receiver, *call_operator_loc, *message_loc, *opening_loc, *arguments, *closing_loc, operator_loc, value]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



2175
2176
2177
2178
2179
2180
2181
# File 'lib/prism/node.rb', line 2175

def compact_child_nodes
  compact = []
  compact << receiver if receiver
  compact << arguments if arguments
  compact << value
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> CallOperatorWriteNode



2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
# File 'lib/prism/node.rb', line 2189

def copy(**params)
  CallOperatorWriteNode.new(
    params.fetch(:receiver) { receiver },
    params.fetch(:call_operator_loc) { call_operator_loc },
    params.fetch(:message_loc) { message_loc },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:arguments) { arguments },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:flags) { flags },
    params.fetch(:read_name) { read_name },
    params.fetch(:write_name) { write_name },
    params.fetch(:operator) { operator },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:value) { value },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



2211
2212
2213
# File 'lib/prism/node.rb', line 2211

def deconstruct_keys(keys)
  { receiver: receiver, call_operator_loc: call_operator_loc, message_loc: message_loc, opening_loc: opening_loc, arguments: arguments, closing_loc: closing_loc, flags: flags, read_name: read_name, write_name: write_name, operator: operator, operator_loc: operator_loc, value: value, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
# File 'lib/prism/node.rb', line 2245

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  if (receiver = self.receiver).nil?
    inspector << "├── receiver: ∅\n"
  else
    inspector << "├── receiver:\n"
    inspector << receiver.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── call_operator_loc: #{inspector.location(call_operator_loc)}\n"
  inspector << "├── message_loc: #{inspector.location(message_loc)}\n"
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  if (arguments = self.arguments).nil?
    inspector << "├── arguments: ∅\n"
  else
    inspector << "├── arguments:\n"
    inspector << arguments.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n"
  flags = [("safe_navigation" if safe_navigation?), ("variable_call" if variable_call?)].compact
  inspector << "├── flags: #{flags.empty? ? "" : flags.join(", ")}\n"
  inspector << "├── read_name: #{read_name.inspect}\n"
  inspector << "├── write_name: #{write_name.inspect}\n"
  inspector << "├── operator: #{operator.inspect}\n"
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector << "└── value:\n"
  inspector << inspector.child_node(value, "    ")
  inspector.to_str
end

#messageObject

def message: () -> String?



2221
2222
2223
# File 'lib/prism/node.rb', line 2221

def message
  message_loc&.slice
end

#openingObject

def opening: () -> String?



2226
2227
2228
# File 'lib/prism/node.rb', line 2226

def opening
  opening_loc&.slice
end

#safe_navigation?Boolean

def safe_navigation?: () -> bool

Returns:

  • (Boolean)


2236
2237
2238
# File 'lib/prism/node.rb', line 2236

def safe_navigation?
  flags.anybits?(CallNodeFlags::SAFE_NAVIGATION)
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



2288
2289
2290
# File 'lib/prism/node.rb', line 2288

def type
  :call_operator_write_node
end

#variable_call?Boolean

def variable_call?: () -> bool

Returns:

  • (Boolean)


2241
2242
2243
# File 'lib/prism/node.rb', line 2241

def variable_call?
  flags.anybits?(CallNodeFlags::VARIABLE_CALL)
end