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, #pretty_print, #to_json

Constructor Details

#initialize(arguments:, location:) ⇒ YieldNode

Returns a new instance of YieldNode.



12111
12112
12113
12114
12115
# File 'lib/syntax_tree/node.rb', line 12111

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

Instance Attribute Details

#argumentsObject (readonly)

nil | Args | Paren

the arguments passed to the yield



12106
12107
12108
# File 'lib/syntax_tree/node.rb', line 12106

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12109
12110
12111
# File 'lib/syntax_tree/node.rb', line 12109

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



12165
12166
12167
# File 'lib/syntax_tree/node.rb', line 12165

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

#accept(visitor) ⇒ Object



12117
12118
12119
# File 'lib/syntax_tree/node.rb', line 12117

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

#child_nodesObject Also known as: deconstruct



12121
12122
12123
# File 'lib/syntax_tree/node.rb', line 12121

def child_nodes
  [arguments]
end

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



12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
# File 'lib/syntax_tree/node.rb', line 12125

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



12138
12139
12140
# File 'lib/syntax_tree/node.rb', line 12138

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

#format(q) ⇒ Object



12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
# File 'lib/syntax_tree/node.rb', line 12142

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