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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of MLHSParen.



6137
6138
6139
6140
6141
6142
# File 'lib/syntax_tree/node.rb', line 6137

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



6132
6133
6134
# File 'lib/syntax_tree/node.rb', line 6132

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6135
6136
6137
# File 'lib/syntax_tree/node.rb', line 6135

def comments
  @comments
end

#contentsObject (readonly)

MLHS | MLHSParen

the contents inside of the parentheses



6127
6128
6129
# File 'lib/syntax_tree/node.rb', line 6127

def contents
  @contents
end

Instance Method Details

#accept(visitor) ⇒ Object



6144
6145
6146
# File 'lib/syntax_tree/node.rb', line 6144

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

#child_nodesObject Also known as: deconstruct



6148
6149
6150
# File 'lib/syntax_tree/node.rb', line 6148

def child_nodes
  [contents]
end

#deconstruct_keys(keys) ⇒ Object



6154
6155
6156
# File 'lib/syntax_tree/node.rb', line 6154

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

#format(q) ⇒ Object



6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
# File 'lib/syntax_tree/node.rb', line 6158

def format(q)
  parent = q.parent

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

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