Class: SyntaxTree::HashLiteral
- Inherits:
-
Object
- Object
- SyntaxTree::HashLiteral
- Defined in:
- lib/syntax_tree.rb
Overview
HashLiteral represents a hash literal.
{ key => value }
Instance Attribute Summary collapse
-
#assocs ⇒ Object
readonly
- Array[ AssocNew | AssocSplat ]
-
the optional contents of the hash.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(assocs:, location:, comments: []) ⇒ HashLiteral
constructor
A new instance of HashLiteral.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
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
#assocs ⇒ Object (readonly)
- Array[ AssocNew | AssocSplat ]
-
the optional contents of the hash
5951 5952 5953 |
# File 'lib/syntax_tree.rb', line 5951 def assocs @assocs end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5957 5958 5959 |
# File 'lib/syntax_tree.rb', line 5957 def comments @comments end |
#location ⇒ Object (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_nodes ⇒ Object
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 |