Class: SyntaxTree::HashLiteral

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

Overview

HashLiteral represents a hash literal.

{ key => value }

Defined Under Namespace

Classes: EmptyWithCommentsFormatter

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(lbrace:, assocs:, location:, comments: []) ⇒ HashLiteral

Returns a new instance of HashLiteral.



4890
4891
4892
4893
4894
4895
# File 'lib/syntax_tree/node.rb', line 4890

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

Instance Attribute Details

#assocsObject (readonly)

Array[ AssocNew | AssocSplat ]

the optional contents of the hash



4885
4886
4887
# File 'lib/syntax_tree/node.rb', line 4885

def assocs
  @assocs
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4888
4889
4890
# File 'lib/syntax_tree/node.rb', line 4888

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that opens this hash



4882
4883
4884
# File 'lib/syntax_tree/node.rb', line 4882

def lbrace
  @lbrace
end

Instance Method Details

#accept(visitor) ⇒ Object



4897
4898
4899
# File 'lib/syntax_tree/node.rb', line 4897

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

#child_nodesObject Also known as: deconstruct



4901
4902
4903
# File 'lib/syntax_tree/node.rb', line 4901

def child_nodes
  [lbrace] + assocs
end

#deconstruct_keys(_keys) ⇒ Object



4907
4908
4909
# File 'lib/syntax_tree/node.rb', line 4907

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

#format(q) ⇒ Object



4911
4912
4913
4914
4915
4916
4917
# File 'lib/syntax_tree/node.rb', line 4911

def format(q)
  if q.parent.is_a?(Assoc)
    format_contents(q)
  else
    q.group { format_contents(q) }
  end
end

#format_key(q, key) ⇒ Object



4919
4920
4921
# File 'lib/syntax_tree/node.rb', line 4919

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