Class: SyntaxTree::HashLiteral
Overview
HashLiteral represents a hash literal.
{ key => value }
Defined Under Namespace
Classes: EmptyWithCommentsFormatter
Instance Attribute Summary collapse
-
#assocs ⇒ Object
readonly
- Array[ Assoc | AssocSplat ]
-
the optional contents of the hash.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#lbrace ⇒ Object
readonly
- LBrace
-
the left brace that opens this hash.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(lbrace: nil, assocs: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
- #format_key(q, key) ⇒ Object
-
#initialize(lbrace:, assocs:, location:) ⇒ HashLiteral
constructor
A new instance of HashLiteral.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(lbrace:, assocs:, location:) ⇒ HashLiteral
Returns a new instance of HashLiteral.
5634 5635 5636 5637 5638 5639 |
# File 'lib/syntax_tree/node.rb', line 5634 def initialize(lbrace:, assocs:, location:) @lbrace = lbrace @assocs = assocs @location = location @comments = [] end |
Instance Attribute Details
#assocs ⇒ Object (readonly)
- Array[ Assoc | AssocSplat ]
-
the optional contents of the hash
5629 5630 5631 |
# File 'lib/syntax_tree/node.rb', line 5629 def assocs @assocs end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5632 5633 5634 |
# File 'lib/syntax_tree/node.rb', line 5632 def comments @comments end |
#lbrace ⇒ Object (readonly)
- LBrace
-
the left brace that opens this hash
5626 5627 5628 |
# File 'lib/syntax_tree/node.rb', line 5626 def lbrace @lbrace end |
Instance Method Details
#===(other) ⇒ Object
5675 5676 5677 5678 |
# File 'lib/syntax_tree/node.rb', line 5675 def ===(other) other.is_a?(HashLiteral) && lbrace === other.lbrace && ArrayMatch.call(assocs, other.assocs) end |
#accept(visitor) ⇒ Object
5641 5642 5643 |
# File 'lib/syntax_tree/node.rb', line 5641 def accept(visitor) visitor.visit_hash(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5645 5646 5647 |
# File 'lib/syntax_tree/node.rb', line 5645 def child_nodes [lbrace] + assocs end |
#copy(lbrace: nil, assocs: nil, location: nil) ⇒ Object
5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 |
# File 'lib/syntax_tree/node.rb', line 5649 def copy(lbrace: nil, assocs: nil, location: nil) node = HashLiteral.new( lbrace: lbrace || self.lbrace, assocs: assocs || self.assocs, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
5663 5664 5665 |
# File 'lib/syntax_tree/node.rb', line 5663 def deconstruct_keys(_keys) { lbrace: lbrace, assocs: assocs, location: location, comments: comments } end |
#format(q) ⇒ Object
5667 5668 5669 5670 5671 5672 5673 |
# File 'lib/syntax_tree/node.rb', line 5667 def format(q) if q.parent.is_a?(Assoc) format_contents(q) else q.group { format_contents(q) } end end |
#format_key(q, key) ⇒ Object
5680 5681 5682 |
# File 'lib/syntax_tree/node.rb', line 5680 def format_key(q, key) (@key_formatter ||= HashKeyFormatter.for(self)).format_key(q, key) end |