Class: SyntaxTree::ArrayLiteral
- Inherits:
-
Object
- Object
- SyntaxTree::ArrayLiteral
- 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
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#contents ⇒ Object
readonly
- nil | Args
-
the contents of the array.
-
#lbracket ⇒ Object
readonly
- LBracket
-
the bracket that opens this array.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(lbracket:, contents:, location:, comments: []) ⇒ ArrayLiteral
constructor
A new instance of ArrayLiteral.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
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
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1471 1472 1473 |
# File 'lib/syntax_tree.rb', line 1471 def comments @comments end |
#contents ⇒ Object (readonly)
- nil | Args
-
the contents of the array
1465 1466 1467 |
# File 'lib/syntax_tree.rb', line 1465 def contents @contents end |
#lbracket ⇒ Object (readonly)
- LBracket
-
the bracket that opens this array
1462 1463 1464 |
# File 'lib/syntax_tree.rb', line 1462 def lbracket @lbracket end |
#location ⇒ Object (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_nodes ⇒ Object
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 |