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

Constructor Details

#initialize(name:, location:) ⇒ BlockArg

Returns a new instance of BlockArg.



2234
2235
2236
2237
2238
# File 'lib/syntax_tree/node.rb', line 2234

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2232
2233
2234
# File 'lib/syntax_tree/node.rb', line 2232

def comments
  @comments
end

#nameObject (readonly)

nil | Ident

the name of the block argument



2229
2230
2231
# File 'lib/syntax_tree/node.rb', line 2229

def name
  @name
end

Instance Method Details

#===(other) ⇒ Object



2270
2271
2272
# File 'lib/syntax_tree/node.rb', line 2270

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

#accept(visitor) ⇒ Object



2240
2241
2242
# File 'lib/syntax_tree/node.rb', line 2240

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [name]
end

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



2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
# File 'lib/syntax_tree/node.rb', line 2248

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



2261
2262
2263
# File 'lib/syntax_tree/node.rb', line 2261

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

#format(q) ⇒ Object



2265
2266
2267
2268
# File 'lib/syntax_tree/node.rb', line 2265

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