Class: SyntaxTree::HashLiteral

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

HashLiteral represents a hash literal.

{ key => value }

Defined Under Namespace

Classes: EmptyWithCommentsFormatter

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

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.



5687
5688
5689
5690
5691
5692
# File 'lib/syntax_tree/node.rb', line 5687

def initialize(lbrace:, assocs:, location:)
  @lbrace = lbrace
  @assocs = assocs
  @location = location
  @comments = []
end

Instance Attribute Details

#assocsObject (readonly)

Array[ Assoc | AssocSplat ]

the optional contents of the hash



5682
5683
5684
# File 'lib/syntax_tree/node.rb', line 5682

def assocs
  @assocs
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5685
5686
5687
# File 'lib/syntax_tree/node.rb', line 5685

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that opens this hash



5679
5680
5681
# File 'lib/syntax_tree/node.rb', line 5679

def lbrace
  @lbrace
end

Instance Method Details

#===(other) ⇒ Object



5728
5729
5730
5731
# File 'lib/syntax_tree/node.rb', line 5728

def ===(other)
  other.is_a?(HashLiteral) && lbrace === other.lbrace &&
    ArrayMatch.call(assocs, other.assocs)
end

#accept(visitor) ⇒ Object



5694
5695
5696
# File 'lib/syntax_tree/node.rb', line 5694

def accept(visitor)
  visitor.visit_hash(self)
end

#child_nodesObject Also known as: deconstruct



5698
5699
5700
# File 'lib/syntax_tree/node.rb', line 5698

def child_nodes
  [lbrace].concat(assocs)
end

#copy(lbrace: nil, assocs: nil, location: nil) ⇒ Object



5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
# File 'lib/syntax_tree/node.rb', line 5702

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



5716
5717
5718
# File 'lib/syntax_tree/node.rb', line 5716

def deconstruct_keys(_keys)
  { lbrace: lbrace, assocs: assocs, location: location, comments: comments }
end

#format(q) ⇒ Object



5720
5721
5722
5723
5724
5725
5726
# File 'lib/syntax_tree/node.rb', line 5720

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



5733
5734
5735
# File 'lib/syntax_tree/node.rb', line 5733

def format_key(q, key)
  (@key_formatter ||= HashKeyFormatter.for(self)).format_key(q, key)
end