Class: SyntaxTree::HashLiteral

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

Overview

HashLiteral represents a hash literal.

{ key => value }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HashLiteral.



5959
5960
5961
5962
5963
# File 'lib/syntax_tree.rb', line 5959

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

Instance Attribute Details

#assocsObject (readonly)

Array[ AssocNew | AssocSplat ]

the optional contents of the hash



5951
5952
5953
# File 'lib/syntax_tree.rb', line 5951

def assocs
  @assocs
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5957
5958
5959
# File 'lib/syntax_tree.rb', line 5957

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5954
5955
5956
# File 'lib/syntax_tree.rb', line 5954

def location
  @location
end

Instance Method Details

#child_nodesObject



5965
5966
5967
# File 'lib/syntax_tree.rb', line 5965

def child_nodes
  assocs
end

#format(q) ⇒ Object



5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
# File 'lib/syntax_tree.rb', line 5969

def format(q)
  contents = -> do
    q.text("{")
    q.indent do
      q.breakable
      q.format(HashFormatter.for(self))
    end
    q.breakable
    q.text("}")
  end

  q.parent.is_a?(Assoc) ? contents.call : q.group(&contents)
end

#pretty_print(q) ⇒ Object



5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
# File 'lib/syntax_tree.rb', line 5983

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

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

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

#to_json(*opts) ⇒ Object



5996
5997
5998
5999
6000
# File 'lib/syntax_tree.rb', line 5996

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