Class: Prism::YieldNode

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

Overview

Represents the use of the ‘yield` keyword.

yield 1
^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword_loc, lparen_loc, arguments, rparen_loc, location) ⇒ YieldNode

def initialize: (keyword_loc: Location, lparen_loc: Location?, arguments: ArgumentsNode?, rparen_loc: Location?, location: Location) -> void



17168
17169
17170
17171
17172
17173
17174
# File 'lib/prism/node.rb', line 17168

def initialize(keyword_loc, lparen_loc, arguments, rparen_loc, location)
  @keyword_loc = keyword_loc
  @lparen_loc = lparen_loc
  @arguments = arguments
  @rparen_loc = rparen_loc
  @location = location
end

Instance Attribute Details

#argumentsObject (readonly)

attr_reader arguments: ArgumentsNode?



17162
17163
17164
# File 'lib/prism/node.rb', line 17162

def arguments
  @arguments
end

#keyword_locObject (readonly)

attr_reader keyword_loc: Location



17156
17157
17158
# File 'lib/prism/node.rb', line 17156

def keyword_loc
  @keyword_loc
end

#lparen_locObject (readonly)

attr_reader lparen_loc: Location?



17159
17160
17161
# File 'lib/prism/node.rb', line 17159

def lparen_loc
  @lparen_loc
end

#rparen_locObject (readonly)

attr_reader rparen_loc: Location?



17165
17166
17167
# File 'lib/prism/node.rb', line 17165

def rparen_loc
  @rparen_loc
end

Class Method Details

.typeObject

Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol



17271
17272
17273
# File 'lib/prism/node.rb', line 17271

def self.type
  :yield_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



17177
17178
17179
# File 'lib/prism/node.rb', line 17177

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

#child_nodesObject Also known as: deconstruct

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



17182
17183
17184
# File 'lib/prism/node.rb', line 17182

def child_nodes
  [arguments]
end

#comment_targetsObject

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



17194
17195
17196
# File 'lib/prism/node.rb', line 17194

def comment_targets
  [keyword_loc, *lparen_loc, *arguments, *rparen_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



17187
17188
17189
17190
17191
# File 'lib/prism/node.rb', line 17187

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

#copy(**params) ⇒ Object

def copy: (**params) -> YieldNode



17199
17200
17201
17202
17203
17204
17205
17206
17207
# File 'lib/prism/node.rb', line 17199

def copy(**params)
  YieldNode.new(
    params.fetch(:keyword_loc) { keyword_loc },
    params.fetch(:lparen_loc) { lparen_loc },
    params.fetch(:arguments) { arguments },
    params.fetch(:rparen_loc) { rparen_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



17213
17214
17215
# File 'lib/prism/node.rb', line 17213

def deconstruct_keys(keys)
  { keyword_loc: keyword_loc, lparen_loc: lparen_loc, arguments: arguments, rparen_loc: rparen_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
# File 'lib/prism/node.rb', line 17233

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── keyword_loc: #{inspector.location(keyword_loc)}\n"
  inspector << "├── lparen_loc: #{inspector.location(lparen_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 << "└── rparen_loc: #{inspector.location(rparen_loc)}\n"
  inspector.to_str
end

#keywordObject

def keyword: () -> String



17218
17219
17220
# File 'lib/prism/node.rb', line 17218

def keyword
  keyword_loc.slice
end

#lparenObject

def lparen: () -> String?



17223
17224
17225
# File 'lib/prism/node.rb', line 17223

def lparen
  lparen_loc&.slice
end

#rparenObject

def rparen: () -> String?



17228
17229
17230
# File 'lib/prism/node.rb', line 17228

def rparen
  rparen_loc&.slice
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



17261
17262
17263
# File 'lib/prism/node.rb', line 17261

def type
  :yield_node
end