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

Constructor Details

#initialize(name:, location:) ⇒ RestParam

Returns a new instance of RestParam.



9631
9632
9633
9634
9635
# File 'lib/syntax_tree/node.rb', line 9631

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9629
9630
9631
# File 'lib/syntax_tree/node.rb', line 9629

def comments
  @comments
end

#nameObject (readonly)

nil | Ident

the name of the parameter



9626
9627
9628
# File 'lib/syntax_tree/node.rb', line 9626

def name
  @name
end

Instance Method Details

#===(other) ⇒ Object



9667
9668
9669
# File 'lib/syntax_tree/node.rb', line 9667

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

#accept(visitor) ⇒ Object



9637
9638
9639
# File 'lib/syntax_tree/node.rb', line 9637

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

#child_nodesObject Also known as: deconstruct



9641
9642
9643
# File 'lib/syntax_tree/node.rb', line 9641

def child_nodes
  [name]
end

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



9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
# File 'lib/syntax_tree/node.rb', line 9645

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



9658
9659
9660
# File 'lib/syntax_tree/node.rb', line 9658

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

#format(q) ⇒ Object



9662
9663
9664
9665
# File 'lib/syntax_tree/node.rb', line 9662

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