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.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2227
2228
2229
# File 'lib/syntax_tree/node.rb', line 2227

def comments
  @comments
end

#nameObject (readonly)

nil | Ident

the name of the block argument



2224
2225
2226
# File 'lib/syntax_tree/node.rb', line 2224

def name
  @name
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [name]
end

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



2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
# File 'lib/syntax_tree/node.rb', line 2243

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



2256
2257
2258
# File 'lib/syntax_tree/node.rb', line 2256

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

#format(q) ⇒ Object



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

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