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

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



14381
14382
14383
14384
14385
14386
14387
# File 'lib/prism/node.rb', line 14381

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?



14375
14376
14377
# File 'lib/prism/node.rb', line 14375

def arguments
  @arguments
end

#keyword_locObject (readonly)

attr_reader keyword_loc: Location



14369
14370
14371
# File 'lib/prism/node.rb', line 14369

def keyword_loc
  @keyword_loc
end

#lparen_locObject (readonly)

attr_reader lparen_loc: Location?



14372
14373
14374
# File 'lib/prism/node.rb', line 14372

def lparen_loc
  @lparen_loc
end

#rparen_locObject (readonly)

attr_reader rparen_loc: Location?



14378
14379
14380
# File 'lib/prism/node.rb', line 14378

def rparen_loc
  @rparen_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



14390
14391
14392
# File 'lib/prism/node.rb', line 14390

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

#child_nodesObject Also known as: deconstruct

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



14395
14396
14397
# File 'lib/prism/node.rb', line 14395

def child_nodes
  [arguments]
end

#comment_targetsObject

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



14407
14408
14409
# File 'lib/prism/node.rb', line 14407

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



14400
14401
14402
14403
14404
# File 'lib/prism/node.rb', line 14400

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

#copy(**params) ⇒ Object

def copy: (**params) -> YieldNode



14412
14413
14414
14415
14416
14417
14418
14419
14420
# File 'lib/prism/node.rb', line 14412

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]



14426
14427
14428
# File 'lib/prism/node.rb', line 14426

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



14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
# File 'lib/prism/node.rb', line 14445

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



14431
14432
14433
# File 'lib/prism/node.rb', line 14431

def keyword
  keyword_loc.slice
end

#lparenObject

def lparen: () -> String?



14436
14437
14438
# File 'lib/prism/node.rb', line 14436

def lparen
  lparen_loc&.slice
end

#rparenObject

def rparen: () -> String?



14441
14442
14443
# File 'lib/prism/node.rb', line 14441

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



14473
14474
14475
# File 'lib/prism/node.rb', line 14473

def type
  :yield_node
end