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

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of BlockVar.



2007
2008
2009
2010
2011
2012
# File 'lib/syntax_tree/node.rb', line 2007

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



2005
2006
2007
# File 'lib/syntax_tree/node.rb', line 2005

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



2002
2003
2004
# File 'lib/syntax_tree/node.rb', line 2002

def locals
  @locals
end

#paramsObject (readonly)

Params

the parameters being declared with the block



1999
2000
2001
# File 'lib/syntax_tree/node.rb', line 1999

def params
  @params
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



2014
2015
2016
# File 'lib/syntax_tree/node.rb', line 2014

def child_nodes
  [params, *locals]
end

#deconstruct_keys(keys) ⇒ Object



2020
2021
2022
# File 'lib/syntax_tree/node.rb', line 2020

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

#format(q) ⇒ Object



2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
# File 'lib/syntax_tree/node.rb', line 2024

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



2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
# File 'lib/syntax_tree/node.rb', line 2036

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



2052
2053
2054
2055
2056
2057
2058
2059
2060
# File 'lib/syntax_tree/node.rb', line 2052

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