Class: SyntaxTree::ExcessedComma

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



5764
5765
5766
5767
5768
# File 'lib/syntax_tree.rb', line 5764

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



5762
5763
5764
# File 'lib/syntax_tree.rb', line 5762

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5759
5760
5761
# File 'lib/syntax_tree.rb', line 5759

def location
  @location
end

#valueObject (readonly)

String

the comma



5756
5757
5758
# File 'lib/syntax_tree.rb', line 5756

def value
  @value
end

Instance Method Details

#child_nodesObject



5770
5771
5772
# File 'lib/syntax_tree.rb', line 5770

def child_nodes
  []
end

#format(q) ⇒ Object



5774
5775
5776
# File 'lib/syntax_tree.rb', line 5774

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

#pretty_print(q) ⇒ Object



5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
# File 'lib/syntax_tree.rb', line 5778

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



5789
5790
5791
5792
5793
5794
5795
5796
# File 'lib/syntax_tree.rb', line 5789

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