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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of MLHSParen.



7675
7676
7677
7678
7679
7680
# File 'lib/syntax_tree/node.rb', line 7675

def initialize(contents:, comma: false, location:)
  @contents = contents
  @comma = comma
  @location = location
  @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



7670
7671
7672
# File 'lib/syntax_tree/node.rb', line 7670

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7673
7674
7675
# File 'lib/syntax_tree/node.rb', line 7673

def comments
  @comments
end

#contentsObject (readonly)

MLHS | MLHSParen

the contents inside of the parentheses



7665
7666
7667
# File 'lib/syntax_tree/node.rb', line 7665

def contents
  @contents
end

Instance Method Details

#===(other) ⇒ Object



7728
7729
7730
# File 'lib/syntax_tree/node.rb', line 7728

def ===(other)
  other.is_a?(MLHSParen) && contents === other.contents
end

#accept(visitor) ⇒ Object



7682
7683
7684
# File 'lib/syntax_tree/node.rb', line 7682

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

#child_nodesObject Also known as: deconstruct



7686
7687
7688
# File 'lib/syntax_tree/node.rb', line 7686

def child_nodes
  [contents]
end

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



7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
# File 'lib/syntax_tree/node.rb', line 7690

def copy(contents: nil, location: nil)
  node =
    MLHSParen.new(
      contents: contents || self.contents,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



7703
7704
7705
# File 'lib/syntax_tree/node.rb', line 7703

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

#format(q) ⇒ Object



7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
# File 'lib/syntax_tree/node.rb', line 7707

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