Class: SyntaxTree::BlockArg

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

Overview

BlockArg represents declaring a block parameter on a method definition.

def method(&block); end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, location:, comments: []) ⇒ BlockArg

Returns a new instance of BlockArg.



2146
2147
2148
2149
2150
# File 'lib/syntax_tree/node.rb', line 2146

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2144
2145
2146
# File 'lib/syntax_tree/node.rb', line 2144

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



2141
2142
2143
# File 'lib/syntax_tree/node.rb', line 2141

def location
  @location
end

#nameObject (readonly)

nil | Ident

the name of the block argument



2138
2139
2140
# File 'lib/syntax_tree/node.rb', line 2138

def name
  @name
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



2152
2153
2154
# File 'lib/syntax_tree/node.rb', line 2152

def child_nodes
  [name]
end

#deconstruct_keys(keys) ⇒ Object



2158
2159
2160
# File 'lib/syntax_tree/node.rb', line 2158

def deconstruct_keys(keys)
  { name: name, location: location, comments: comments }
end

#format(q) ⇒ Object



2162
2163
2164
2165
# File 'lib/syntax_tree/node.rb', line 2162

def format(q)
  q.text("&")
  q.format(name) if name
end

#pretty_print(q) ⇒ Object



2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
# File 'lib/syntax_tree/node.rb', line 2167

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

    if name
      q.breakable
      q.pp(name)
    end

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

#to_json(*opts) ⇒ Object



2180
2181
2182
2183
2184
# File 'lib/syntax_tree/node.rb', line 2180

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