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.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#nameObject (readonly)

nil | Ident

the name of the parameter



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

def name
  @name
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [name]
end

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



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

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



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

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

#format(q) ⇒ Object



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

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