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.



1473
1474
1475
1476
1477
1478
# File 'lib/syntax_tree.rb', line 1473

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



1471
1472
1473
# File 'lib/syntax_tree.rb', line 1471

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



1465
1466
1467
# File 'lib/syntax_tree.rb', line 1465

def contents
  @contents
end

#lbracketObject (readonly)

LBracket

the bracket that opens this array



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

def lbracket
  @lbracket
end

#locationObject (readonly)

Location

the location of this node



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

def location
  @location
end

Instance Method Details

#child_nodesObject



1480
1481
1482
# File 'lib/syntax_tree.rb', line 1480

def child_nodes
  [lbracket, contents]
end

#format(q) ⇒ Object



1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
# File 'lib/syntax_tree.rb', line 1484

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



1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
# File 'lib/syntax_tree.rb', line 1515

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



1526
1527
1528
1529
1530
# File 'lib/syntax_tree.rb', line 1526

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