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

Constructor Details

#initialize(name:, location:, comments: []) ⇒ RestParam

Returns a new instance of RestParam.



8723
8724
8725
8726
8727
# File 'lib/syntax_tree/node.rb', line 8723

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8721
8722
8723
# File 'lib/syntax_tree/node.rb', line 8721

def comments
  @comments
end

#nameObject (readonly)

nil | Ident

the name of the parameter



8718
8719
8720
# File 'lib/syntax_tree/node.rb', line 8718

def name
  @name
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



8729
8730
8731
# File 'lib/syntax_tree/node.rb', line 8729

def child_nodes
  [name]
end

#deconstruct_keys(keys) ⇒ Object



8735
8736
8737
# File 'lib/syntax_tree/node.rb', line 8735

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

#format(q) ⇒ Object



8739
8740
8741
8742
# File 'lib/syntax_tree/node.rb', line 8739

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

#pretty_print(q) ⇒ Object



8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
# File 'lib/syntax_tree/node.rb', line 8744

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("rest_param")

    q.breakable
    q.pp(name)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



8755
8756
8757
8758
8759
# File 'lib/syntax_tree/node.rb', line 8755

def to_json(*opts)
  { type: :rest_param, name: name, loc: location, cmts: comments }.to_json(
    *opts
  )
end