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.



8233
8234
8235
8236
8237
8238
# File 'lib/syntax_tree.rb', line 8233

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



8225
8226
8227
# File 'lib/syntax_tree.rb', line 8225

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8231
8232
8233
# File 'lib/syntax_tree.rb', line 8231

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



8228
8229
8230
# File 'lib/syntax_tree.rb', line 8228

def location
  @location
end

#partsObject (readonly)

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



8220
8221
8222
# File 'lib/syntax_tree.rb', line 8220

def parts
  @parts
end

Instance Method Details

#child_nodesObject



8240
8241
8242
# File 'lib/syntax_tree.rb', line 8240

def child_nodes
  parts
end

#format(q) ⇒ Object



8244
8245
8246
8247
# File 'lib/syntax_tree.rb', line 8244

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

#pretty_print(q) ⇒ Object



8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
# File 'lib/syntax_tree.rb', line 8249

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



8260
8261
8262
8263
8264
8265
8266
8267
8268
# File 'lib/syntax_tree.rb', line 8260

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