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



1492
1493
1494
1495
1496
1497
# File 'lib/syntax_tree.rb', line 1492

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



1490
1491
1492
# File 'lib/syntax_tree.rb', line 1490

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



1484
1485
1486
# File 'lib/syntax_tree.rb', line 1484

def contents
  @contents
end

#lbracketObject (readonly)

LBracket

the bracket that opens this array



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

def lbracket
  @lbracket
end

#locationObject (readonly)

Location

the location of this node



1487
1488
1489
# File 'lib/syntax_tree.rb', line 1487

def location
  @location
end

Instance Method Details

#child_nodesObject



1499
1500
1501
# File 'lib/syntax_tree.rb', line 1499

def child_nodes
  [lbracket, contents]
end

#format(q) ⇒ Object



1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
# File 'lib/syntax_tree.rb', line 1503

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



1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
# File 'lib/syntax_tree.rb', line 1534

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



1545
1546
1547
1548
1549
# File 'lib/syntax_tree.rb', line 1545

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