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.



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

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



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

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7730
7731
7732
# File 'lib/syntax_tree/node.rb', line 7730

def comments
  @comments
end

#contentsObject (readonly)

MLHS | MLHSParen

the contents inside of the parentheses



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

def contents
  @contents
end

Instance Method Details

#===(other) ⇒ Object



7785
7786
7787
# File 'lib/syntax_tree/node.rb', line 7785

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

#accept(visitor) ⇒ Object



7739
7740
7741
# File 'lib/syntax_tree/node.rb', line 7739

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [contents]
end

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



7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
# File 'lib/syntax_tree/node.rb', line 7747

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



7760
7761
7762
# File 'lib/syntax_tree/node.rb', line 7760

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

#format(q) ⇒ Object



7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
# File 'lib/syntax_tree/node.rb', line 7764

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