Class: SyntaxTree::YieldNode

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Yield represents using the yield keyword with arguments.

yield value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(arguments:, location:) ⇒ YieldNode

Returns a new instance of YieldNode.



12296
12297
12298
12299
12300
# File 'lib/syntax_tree/node.rb', line 12296

def initialize(arguments:, location:)
  @arguments = arguments
  @location = location
  @comments = []
end

Instance Attribute Details

#argumentsObject (readonly)

nil | Args | Paren

the arguments passed to the yield



12291
12292
12293
# File 'lib/syntax_tree/node.rb', line 12291

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12294
12295
12296
# File 'lib/syntax_tree/node.rb', line 12294

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



12350
12351
12352
# File 'lib/syntax_tree/node.rb', line 12350

def ===(other)
  other.is_a?(YieldNode) && arguments === other.arguments
end

#accept(visitor) ⇒ Object



12302
12303
12304
# File 'lib/syntax_tree/node.rb', line 12302

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

#child_nodesObject Also known as: deconstruct



12306
12307
12308
# File 'lib/syntax_tree/node.rb', line 12306

def child_nodes
  [arguments]
end

#copy(arguments: nil, location: nil) ⇒ Object



12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
# File 'lib/syntax_tree/node.rb', line 12310

def copy(arguments: nil, location: nil)
  node =
    YieldNode.new(
      arguments: arguments || self.arguments,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



12323
12324
12325
# File 'lib/syntax_tree/node.rb', line 12323

def deconstruct_keys(_keys)
  { arguments: arguments, location: location, comments: comments }
end

#format(q) ⇒ Object



12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
# File 'lib/syntax_tree/node.rb', line 12327

def format(q)
  if arguments.nil?
    q.text("yield")
    return
  end

  q.group do
    q.text("yield")

    if arguments.is_a?(Paren)
      q.format(arguments)
    else
      q.if_break { q.text("(") }.if_flat { q.text(" ") }
      q.indent do
        q.breakable_empty
        q.format(arguments)
      end
      q.breakable_empty
      q.if_break { q.text(")") }
    end
  end
end