Class: SyntaxTree::BlockVar

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

Overview

BlockVar represents the parameters being declared for a block. Effectively this node is everything contained within the pipes. This includes all of the various parameter types, as well as block-local variable declarations.

method do |positional, optional = value, keyword:, █ local|
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, locals:, location:, comments: []) ⇒ BlockVar

Returns a new instance of BlockVar.



2076
2077
2078
2079
2080
2081
# File 'lib/syntax_tree/node.rb', line 2076

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



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

def locals
  @locals
end

#locationObject (readonly)

Location

the location of this node



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

def location
  @location
end

#paramsObject (readonly)

Params

the parameters being declared with the block



2065
2066
2067
# File 'lib/syntax_tree/node.rb', line 2065

def params
  @params
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



2083
2084
2085
# File 'lib/syntax_tree/node.rb', line 2083

def child_nodes
  [params, *locals]
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

def format(q)
  q.group(0, "|", "|") do
    doc = q.format(params)
    RemoveBreaks.call(doc)

    if locals.any?
      q.text("; ")
      q.seplist(locals, -> { q.text(", ") }) { |local| q.format(local) }
    end
  end
end

#pretty_print(q) ⇒ Object



2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
# File 'lib/syntax_tree/node.rb', line 2105

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

    q.breakable
    q.pp(params)

    if locals.any?
      q.breakable
      q.group(2, "(", ")") { q.seplist(locals) { |local| q.pp(local) } }
    end

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

#to_json(*opts) ⇒ Object



2121
2122
2123
2124
2125
2126
2127
2128
2129
# File 'lib/syntax_tree/node.rb', line 2121

def to_json(*opts)
  {
    type: :block_var,
    params: params,
    locals: locals,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end