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

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of BlockArg.



2074
2075
2076
2077
2078
# File 'lib/syntax_tree/node.rb', line 2074

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



2072
2073
2074
# File 'lib/syntax_tree/node.rb', line 2072

def comments
  @comments
end

#nameObject (readonly)

nil | Ident

the name of the block argument



2069
2070
2071
# File 'lib/syntax_tree/node.rb', line 2069

def name
  @name
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



2080
2081
2082
# File 'lib/syntax_tree/node.rb', line 2080

def child_nodes
  [name]
end

#deconstruct_keys(keys) ⇒ Object



2086
2087
2088
# File 'lib/syntax_tree/node.rb', line 2086

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

#format(q) ⇒ Object



2090
2091
2092
2093
# File 'lib/syntax_tree/node.rb', line 2090

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

#pretty_print(q) ⇒ Object



2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
# File 'lib/syntax_tree/node.rb', line 2095

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



2108
2109
2110
2111
2112
# File 'lib/syntax_tree/node.rb', line 2108

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