Class: SyntaxTree::MLHS

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MLHS.



8156
8157
8158
8159
8160
8161
# File 'lib/syntax_tree.rb', line 8156

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



8148
8149
8150
# File 'lib/syntax_tree.rb', line 8148

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8154
8155
8156
# File 'lib/syntax_tree.rb', line 8154

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



8151
8152
8153
# File 'lib/syntax_tree.rb', line 8151

def location
  @location
end

#partsObject (readonly)

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



8143
8144
8145
# File 'lib/syntax_tree.rb', line 8143

def parts
  @parts
end

Instance Method Details

#child_nodesObject



8163
8164
8165
# File 'lib/syntax_tree.rb', line 8163

def child_nodes
  parts
end

#format(q) ⇒ Object



8167
8168
8169
8170
# File 'lib/syntax_tree.rb', line 8167

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

#pretty_print(q) ⇒ Object



8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
# File 'lib/syntax_tree.rb', line 8172

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



8183
8184
8185
8186
8187
8188
8189
8190
8191
# File 'lib/syntax_tree.rb', line 8183

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