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:, location:, comma: false) ⇒ MLHSParen

Returns a new instance of MLHSParen.



7716
7717
7718
7719
7720
7721
# File 'lib/syntax_tree/node.rb', line 7716

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



7711
7712
7713
# File 'lib/syntax_tree/node.rb', line 7711

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7714
7715
7716
# File 'lib/syntax_tree/node.rb', line 7714

def comments
  @comments
end

#contentsObject (readonly)

MLHS | MLHSParen

the contents inside of the parentheses



7706
7707
7708
# File 'lib/syntax_tree/node.rb', line 7706

def contents
  @contents
end

Instance Method Details

#===(other) ⇒ Object



7769
7770
7771
# File 'lib/syntax_tree/node.rb', line 7769

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

#accept(visitor) ⇒ Object



7723
7724
7725
# File 'lib/syntax_tree/node.rb', line 7723

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [contents]
end

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



7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
# File 'lib/syntax_tree/node.rb', line 7731

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



7744
7745
7746
# File 'lib/syntax_tree/node.rb', line 7744

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

#format(q) ⇒ Object



7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
# File 'lib/syntax_tree/node.rb', line 7748

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