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, #pretty_print, #to_json

Constructor Details

#initialize(lbrace:, assocs:, location:, comments: []) ⇒ HashLiteral

Returns a new instance of HashLiteral.



4733
4734
4735
4736
4737
4738
# File 'lib/syntax_tree/node.rb', line 4733

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

Instance Attribute Details

#assocs ⇒ Object (readonly)

[Array[ AssocNew | AssocSplat ]] the optional contents of the hash



4728
4729
4730
# File 'lib/syntax_tree/node.rb', line 4728

def assocs
  @assocs
end

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



4731
4732
4733
# File 'lib/syntax_tree/node.rb', line 4731

def comments
  @comments
end

#lbrace ⇒ Object (readonly)

[LBrace] the left brace that opens this hash



4725
4726
4727
# File 'lib/syntax_tree/node.rb', line 4725

def lbrace
  @lbrace
end

Instance Method Details

#accept(visitor) ⇒ Object



4740
4741
4742
# File 'lib/syntax_tree/node.rb', line 4740

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

#child_nodes ⇒ Object Also known as: deconstruct



4744
4745
4746
# File 'lib/syntax_tree/node.rb', line 4744

def child_nodes
  [lbrace] + assocs
end

#deconstruct_keys(_keys) ⇒ Object



4750
4751
4752
# File 'lib/syntax_tree/node.rb', line 4750

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

#format(q) ⇒ Object



4754
4755
4756
4757
4758
4759
4760
# File 'lib/syntax_tree/node.rb', line 4754

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



4762
4763
4764
# File 'lib/syntax_tree/node.rb', line 4762

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