Class: SyntaxTree::BareAssocHash

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

Overview

BareAssocHash represents a hash of contents being passed as a method argument (and therefore has omitted braces). It’s very similar to an AssocListFromArgs node.

method(key1: value1, key2: value2)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of BareAssocHash.



1733
1734
1735
1736
1737
# File 'lib/syntax_tree/node.rb', line 1733

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

Instance Attribute Details

#assocsObject (readonly)

Array[ AssocNew | AssocSplat ]


1725
1726
1727
# File 'lib/syntax_tree/node.rb', line 1725

def assocs
  @assocs
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1731
1732
1733
# File 'lib/syntax_tree/node.rb', line 1731

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



1728
1729
1730
# File 'lib/syntax_tree/node.rb', line 1728

def location
  @location
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



1739
1740
1741
# File 'lib/syntax_tree/node.rb', line 1739

def child_nodes
  assocs
end

#deconstruct_keys(keys) ⇒ Object



1745
1746
1747
# File 'lib/syntax_tree/node.rb', line 1745

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

#format(q) ⇒ Object



1749
1750
1751
# File 'lib/syntax_tree/node.rb', line 1749

def format(q)
  q.seplist(assocs) { |assoc| q.format(assoc) }
end

#format_key(q, key) ⇒ Object



1753
1754
1755
# File 'lib/syntax_tree/node.rb', line 1753

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

#pretty_print(q) ⇒ Object



1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
# File 'lib/syntax_tree/node.rb', line 1757

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("bare_assoc_hash")

    q.breakable
    q.group(2, "(", ")") { q.seplist(assocs) { |assoc| q.pp(assoc) } }

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



1768
1769
1770
1771
1772
1773
1774
1775
# File 'lib/syntax_tree/node.rb', line 1768

def to_json(*opts)
  {
    type: :bare_assoc_hash,
    assocs: assocs,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end