Class: SyntaxTree::ArrayLiteral

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

Overview

ArrayLiteral represents an array literal, which can optionally contain elements.

[]
[one, two, three]

Defined Under Namespace

Classes: QSymbolsFormatter, QWordsFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents:, location:, comments: []) ⇒ ArrayLiteral

Returns a new instance of ArrayLiteral.



1389
1390
1391
1392
1393
# File 'lib/syntax_tree.rb', line 1389

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1387
1388
1389
# File 'lib/syntax_tree.rb', line 1387

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



1381
1382
1383
# File 'lib/syntax_tree.rb', line 1381

def contents
  @contents
end

#locationObject (readonly)

Location

the location of this node



1384
1385
1386
# File 'lib/syntax_tree.rb', line 1384

def location
  @location
end

Instance Method Details

#child_nodesObject



1395
1396
1397
# File 'lib/syntax_tree.rb', line 1395

def child_nodes
  [contents]
end

#format(q) ⇒ Object



1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
# File 'lib/syntax_tree.rb', line 1399

def format(q)
  unless contents
    q.text("[]")
    return
  end

  if qwords?
    QWordsFormatter.new(contents).format(q)
    return
  end

  if qsymbols?
    QSymbolsFormatter.new(contents).format(q)
    return
  end

  q.group(0, "[", "]") do
    q.indent do
      q.breakable("")
      q.format(contents)
    end
    q.breakable("")
  end
end

#pretty_print(q) ⇒ Object



1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
# File 'lib/syntax_tree.rb', line 1424

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

    q.breakable
    q.pp(contents)

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

#to_json(*opts) ⇒ Object



1435
1436
1437
1438
1439
# File 'lib/syntax_tree.rb', line 1435

def to_json(*opts)
  { type: :array, cnts: contents, loc: location, cmts: comments }.to_json(
    *opts
  )
end