Class: SyntaxTree::MLHS

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

MLHS represents a list of values being destructured on the left-hand side of a multiple assignment.

first, second, third = value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(parts:, comma: false, location:, comments: []) ⇒ MLHS

Returns a new instance of MLHS.



6987
6988
6989
6990
6991
6992
# File 'lib/syntax_tree/node.rb', line 6987

def initialize(parts:, comma: false, location:, comments: [])
  @parts = parts
  @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



6982
6983
6984
# File 'lib/syntax_tree/node.rb', line 6982

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6985
6986
6987
# File 'lib/syntax_tree/node.rb', line 6985

def comments
  @comments
end

#partsObject (readonly)

Array[ARefField | ArgStar | Field | Ident | MLHSParen | VarField] the parts of the left-hand side of a multiple assignment



6977
6978
6979
# File 'lib/syntax_tree/node.rb', line 6977

def parts
  @parts
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



6994
6995
6996
# File 'lib/syntax_tree/node.rb', line 6994

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



7000
7001
7002
# File 'lib/syntax_tree/node.rb', line 7000

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

#format(q) ⇒ Object



7004
7005
7006
7007
# File 'lib/syntax_tree/node.rb', line 7004

def format(q)
  q.seplist(parts) { |part| q.format(part) }
  q.text(",") if comma
end

#pretty_print(q) ⇒ Object



7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
# File 'lib/syntax_tree/node.rb', line 7009

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("mlhs")

    q.breakable
    q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } }

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



7020
7021
7022
7023
7024
7025
7026
7027
7028
# File 'lib/syntax_tree/node.rb', line 7020

def to_json(*opts)
  {
    type: :mlhs,
    parts: parts,
    comma: comma,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end