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.
5702 5703 5704 5705 5706 5707 |
# File 'lib/syntax_tree/node.rb', line 5702 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
5697 5698 5699 |
# File 'lib/syntax_tree/node.rb', line 5697 def assocs @assocs end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5700 5701 5702 |
# File 'lib/syntax_tree/node.rb', line 5700 def comments @comments end |
#lbrace ⇒ Object (readonly)
- LBrace
-
the left brace that opens this hash
5694 5695 5696 |
# File 'lib/syntax_tree/node.rb', line 5694 def lbrace @lbrace end |
Instance Method Details
#===(other) ⇒ Object
5743 5744 5745 5746 |
# File 'lib/syntax_tree/node.rb', line 5743 def ===(other) other.is_a?(HashLiteral) && lbrace === other.lbrace && ArrayMatch.call(assocs, other.assocs) end |
#accept(visitor) ⇒ Object
5709 5710 5711 |
# File 'lib/syntax_tree/node.rb', line 5709 def accept(visitor) visitor.visit_hash(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5713 5714 5715 |
# File 'lib/syntax_tree/node.rb', line 5713 def child_nodes [lbrace].concat(assocs) end |
#copy(lbrace: nil, assocs: nil, location: nil) ⇒ Object
5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 |
# File 'lib/syntax_tree/node.rb', line 5717 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
5731 5732 5733 |
# File 'lib/syntax_tree/node.rb', line 5731 def deconstruct_keys(_keys) { lbrace: lbrace, assocs: assocs, location: location, comments: comments } end |
#format(q) ⇒ Object
5735 5736 5737 5738 5739 5740 5741 |
# File 'lib/syntax_tree/node.rb', line 5735 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
5748 5749 5750 |
# File 'lib/syntax_tree/node.rb', line 5748 def format_key(q, key) (@key_formatter ||= HashKeyFormatter.for(self)).format_key(q, key) end |