Class: SyntaxTree::BlockArg

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

Overview

BlockArg represents declaring a block parameter on a method definition.

def method(&block); end

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(name:, location:) ⇒ BlockArg

Returns a new instance of BlockArg.



2211
2212
2213
2214
2215
# File 'lib/syntax_tree/node.rb', line 2211

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2209
2210
2211
# File 'lib/syntax_tree/node.rb', line 2209

def comments
  @comments
end

#nameObject (readonly)

nil | Ident

the name of the block argument



2206
2207
2208
# File 'lib/syntax_tree/node.rb', line 2206

def name
  @name
end

Instance Method Details

#===(other) ⇒ Object



2247
2248
2249
# File 'lib/syntax_tree/node.rb', line 2247

def ===(other)
  other.is_a?(BlockArg) && name === other.name
end

#accept(visitor) ⇒ Object



2217
2218
2219
# File 'lib/syntax_tree/node.rb', line 2217

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [name]
end

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



2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
# File 'lib/syntax_tree/node.rb', line 2225

def copy(name: nil, location: nil)
  node =
    BlockArg.new(
      name: name || self.name,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



2238
2239
2240
# File 'lib/syntax_tree/node.rb', line 2238

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

#format(q) ⇒ Object



2242
2243
2244
2245
# File 'lib/syntax_tree/node.rb', line 2242

def format(q)
  q.text("&")
  q.format(name) if name
end