Class: SyntaxTree::BareAssocHash

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



2237
2238
2239
2240
2241
# File 'lib/syntax_tree.rb', line 2237

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

Instance Attribute Details

#assocsObject (readonly)

Array[ AssocNew | AssocSplat ]


2229
2230
2231
# File 'lib/syntax_tree.rb', line 2229

def assocs
  @assocs
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2235
2236
2237
# File 'lib/syntax_tree.rb', line 2235

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



2232
2233
2234
# File 'lib/syntax_tree.rb', line 2232

def location
  @location
end

Instance Method Details

#child_nodesObject



2243
2244
2245
# File 'lib/syntax_tree.rb', line 2243

def child_nodes
  assocs
end

#format(q) ⇒ Object



2247
2248
2249
# File 'lib/syntax_tree.rb', line 2247

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

#format_key(q, key) ⇒ Object



2251
2252
2253
# File 'lib/syntax_tree.rb', line 2251

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

#pretty_print(q) ⇒ Object



2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
# File 'lib/syntax_tree.rb', line 2255

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



2266
2267
2268
2269
2270
2271
2272
2273
# File 'lib/syntax_tree.rb', line 2266

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