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

Constructor Details

#initialize(assocs:, location:) ⇒ BareAssocHash

Returns a new instance of BareAssocHash.



1841
1842
1843
1844
1845
# File 'lib/syntax_tree/node.rb', line 1841

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

Instance Attribute Details

#assocsObject (readonly)

Array[ Assoc | AssocSplat ]


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

def assocs
  @assocs
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1839
1840
1841
# File 'lib/syntax_tree/node.rb', line 1839

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



1876
1877
1878
# File 'lib/syntax_tree/node.rb', line 1876

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

#accept(visitor) ⇒ Object



1847
1848
1849
# File 'lib/syntax_tree/node.rb', line 1847

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

#child_nodesObject Also known as: deconstruct



1851
1852
1853
# File 'lib/syntax_tree/node.rb', line 1851

def child_nodes
  assocs
end

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



1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
# File 'lib/syntax_tree/node.rb', line 1855

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



1868
1869
1870
# File 'lib/syntax_tree/node.rb', line 1868

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

#format(q) ⇒ Object



1872
1873
1874
# File 'lib/syntax_tree/node.rb', line 1872

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

#format_key(q, key) ⇒ Object



1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
# File 'lib/syntax_tree/node.rb', line 1880

def format_key(q, key)
  @key_formatter ||=
    case q.parents.take(3).last
    when Break, Next, ReturnNode
      HashKeyFormatter::Identity.new
    else
      HashKeyFormatter.for(self)
    end

  @key_formatter.format_key(q, key)
end