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, VarRefsFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ArrayLiteral.



1460
1461
1462
1463
1464
1465
# File 'lib/syntax_tree.rb', line 1460

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1458
1459
1460
# File 'lib/syntax_tree.rb', line 1458

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



1452
1453
1454
# File 'lib/syntax_tree.rb', line 1452

def contents
  @contents
end

#lbracketObject (readonly)

LBracket

the bracket that opens this array



1449
1450
1451
# File 'lib/syntax_tree.rb', line 1449

def lbracket
  @lbracket
end

#locationObject (readonly)

Location

the location of this node



1455
1456
1457
# File 'lib/syntax_tree.rb', line 1455

def location
  @location
end

Instance Method Details

#child_nodesObject



1467
1468
1469
# File 'lib/syntax_tree.rb', line 1467

def child_nodes
  [lbracket, contents]
end

#format(q) ⇒ Object



1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
# File 'lib/syntax_tree.rb', line 1471

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

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

  if var_refs?(q)
    VarRefsFormatter.new(contents).format(q)
    return
  end

  q.group do
    q.format(lbracket)

    if contents
      q.indent do
        q.breakable("")
        q.format(contents)
      end
    end

    q.breakable("")
    q.text("]")
  end
end

#pretty_print(q) ⇒ Object



1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
# File 'lib/syntax_tree.rb', line 1502

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



1513
1514
1515
1516
1517
# File 'lib/syntax_tree.rb', line 1513

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