Class: SyntaxTree::ExcessedComma

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

Overview

ExcessedComma represents a trailing comma in a list of block parameters. It changes the block parameters such that they will destructure.

[[1, 2, 3], [2, 3, 4]].each do |first, second,|
end

In the above example, an ExcessedComma node would appear in the third position of the Params node that is used to declare that block. The third position typically represents a rest-type parameter, but in this case is used to indicate that a trailing comma was used.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ ExcessedComma

Returns a new instance of ExcessedComma.



5009
5010
5011
5012
5013
# File 'lib/syntax_tree/node.rb', line 5009

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5007
5008
5009
# File 'lib/syntax_tree/node.rb', line 5007

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5004
5005
5006
# File 'lib/syntax_tree/node.rb', line 5004

def location
  @location
end

#valueObject (readonly)

String

the comma



5001
5002
5003
# File 'lib/syntax_tree/node.rb', line 5001

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



5015
5016
5017
# File 'lib/syntax_tree/node.rb', line 5015

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



5021
5022
5023
# File 'lib/syntax_tree/node.rb', line 5021

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

#format(q) ⇒ Object



5025
5026
5027
# File 'lib/syntax_tree/node.rb', line 5025

def format(q)
  q.text(value)
end

#pretty_print(q) ⇒ Object



5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
# File 'lib/syntax_tree/node.rb', line 5029

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

    q.breakable
    q.pp(value)

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

#to_json(*opts) ⇒ Object



5040
5041
5042
5043
5044
5045
5046
5047
# File 'lib/syntax_tree/node.rb', line 5040

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