Class: SyntaxTree::MLHSParen

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

Overview

MLHSParen represents parentheses being used to destruct values in a multiple assignment on the left hand side.

(left, right) = value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(contents:, comma: false, location:, comments: []) ⇒ MLHSParen

Returns a new instance of MLHSParen.



6576
6577
6578
6579
6580
6581
# File 'lib/syntax_tree/node.rb', line 6576

def initialize(contents:, comma: false, location:, comments: [])
  @contents = contents
  @comma = comma
  @location = location
  @comments = comments
end

Instance Attribute Details

#commaObject

boolean

whether or not there is a trailing comma at the end of this

list, which impacts destructuring. It’s an attr_accessor so that while the syntax tree is being built it can be set by its parent node



6571
6572
6573
# File 'lib/syntax_tree/node.rb', line 6571

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6574
6575
6576
# File 'lib/syntax_tree/node.rb', line 6574

def comments
  @comments
end

#contentsObject (readonly)

MLHS | MLHSParen

the contents inside of the parentheses



6566
6567
6568
# File 'lib/syntax_tree/node.rb', line 6566

def contents
  @contents
end

Instance Method Details

#accept(visitor) ⇒ Object



6583
6584
6585
# File 'lib/syntax_tree/node.rb', line 6583

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

#child_nodesObject Also known as: deconstruct



6587
6588
6589
# File 'lib/syntax_tree/node.rb', line 6587

def child_nodes
  [contents]
end

#deconstruct_keys(_keys) ⇒ Object



6593
6594
6595
# File 'lib/syntax_tree/node.rb', line 6593

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

#format(q) ⇒ Object



6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
# File 'lib/syntax_tree/node.rb', line 6597

def format(q)
  parent = q.parent

  if parent.is_a?(MAssign) || parent.is_a?(MLHSParen)
    q.format(contents)
    q.text(",") if comma
  else
    q.text("(")
    q.group do
      q.indent do
        q.breakable_empty
        q.format(contents)
      end

      q.text(",") if comma
      q.breakable_empty
    end
    q.text(")")
  end
end