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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(assocs:, location:) ⇒ BareAssocHash

Returns a new instance of BareAssocHash.



1797
1798
1799
1800
1801
# File 'lib/syntax_tree/node.rb', line 1797

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

Instance Attribute Details

#assocsObject (readonly)

Array[ Assoc | AssocSplat ]


1792
1793
1794
# File 'lib/syntax_tree/node.rb', line 1792

def assocs
  @assocs
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1795
1796
1797
# File 'lib/syntax_tree/node.rb', line 1795

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



1832
1833
1834
# File 'lib/syntax_tree/node.rb', line 1832

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

#accept(visitor) ⇒ Object



1803
1804
1805
# File 'lib/syntax_tree/node.rb', line 1803

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

#child_nodesObject Also known as: deconstruct



1807
1808
1809
# File 'lib/syntax_tree/node.rb', line 1807

def child_nodes
  assocs
end

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



1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
# File 'lib/syntax_tree/node.rb', line 1811

def copy(assocs: nil, location: nil)
  node =
    BareAssocHash.new(
      assocs: assocs || self.assocs,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



1824
1825
1826
# File 'lib/syntax_tree/node.rb', line 1824

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

#format(q) ⇒ Object



1828
1829
1830
# File 'lib/syntax_tree/node.rb', line 1828

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

#format_key(q, key) ⇒ Object



1836
1837
1838
# File 'lib/syntax_tree/node.rb', line 1836

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