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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of HashLiteral.



4657
4658
4659
4660
4661
4662
# File 'lib/syntax_tree/node.rb', line 4657

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

Instance Attribute Details

#assocsObject (readonly)

Array[ AssocNew | AssocSplat ]

the optional contents of the hash



4652
4653
4654
# File 'lib/syntax_tree/node.rb', line 4652

def assocs
  @assocs
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4655
4656
4657
# File 'lib/syntax_tree/node.rb', line 4655

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that opens this hash



4649
4650
4651
# File 'lib/syntax_tree/node.rb', line 4649

def lbrace
  @lbrace
end

Instance Method Details

#accept(visitor) ⇒ Object



4664
4665
4666
# File 'lib/syntax_tree/node.rb', line 4664

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

#child_nodesObject Also known as: deconstruct



4668
4669
4670
# File 'lib/syntax_tree/node.rb', line 4668

def child_nodes
  [lbrace] + assocs
end

#deconstruct_keys(keys) ⇒ Object



4674
4675
4676
# File 'lib/syntax_tree/node.rb', line 4674

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

#format(q) ⇒ Object



4678
4679
4680
4681
4682
4683
4684
# File 'lib/syntax_tree/node.rb', line 4678

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



4686
4687
4688
# File 'lib/syntax_tree/node.rb', line 4686

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