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.



6339
6340
6341
6342
6343
6344
# File 'lib/syntax_tree/node.rb', line 6339

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

Instance Attribute Details

#comma ⇒ Object

[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



6334
6335
6336
# File 'lib/syntax_tree/node.rb', line 6334

def comma
  @comma
end

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



6337
6338
6339
# File 'lib/syntax_tree/node.rb', line 6337

def comments
  @comments
end

#contents ⇒ Object (readonly)

[MLHS | MLHSParen] the contents inside of the parentheses



6329
6330
6331
# File 'lib/syntax_tree/node.rb', line 6329

def contents
  @contents
end

Instance Method Details

#accept(visitor) ⇒ Object



6346
6347
6348
# File 'lib/syntax_tree/node.rb', line 6346

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

#child_nodes ⇒ Object Also known as: deconstruct



6350
6351
6352
# File 'lib/syntax_tree/node.rb', line 6350

def child_nodes
  [contents]
end

#deconstruct_keys(_keys) ⇒ Object



6356
6357
6358
# File 'lib/syntax_tree/node.rb', line 6356

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

#format(q) ⇒ Object



6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
# File 'lib/syntax_tree/node.rb', line 6360

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