Class: SyntaxTree::RestParam

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

Overview

RestParam represents defining a parameter in a method definition that accepts all remaining positional parameters.

def method(*rest) 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:) ⇒ RestParam

Returns a new instance of RestParam.



9467
9468
9469
9470
9471
# File 'lib/syntax_tree/node.rb', line 9467

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9465
9466
9467
# File 'lib/syntax_tree/node.rb', line 9465

def comments
  @comments
end

#nameObject (readonly)

nil | Ident

the name of the parameter



9462
9463
9464
# File 'lib/syntax_tree/node.rb', line 9462

def name
  @name
end

Instance Method Details

#===(other) ⇒ Object



9503
9504
9505
# File 'lib/syntax_tree/node.rb', line 9503

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

#accept(visitor) ⇒ Object



9473
9474
9475
# File 'lib/syntax_tree/node.rb', line 9473

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

#child_nodesObject Also known as: deconstruct



9477
9478
9479
# File 'lib/syntax_tree/node.rb', line 9477

def child_nodes
  [name]
end

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



9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
# File 'lib/syntax_tree/node.rb', line 9481

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

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

#deconstruct_keys(_keys) ⇒ Object



9494
9495
9496
# File 'lib/syntax_tree/node.rb', line 9494

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

#format(q) ⇒ Object



9498
9499
9500
9501
# File 'lib/syntax_tree/node.rb', line 9498

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