Class: SyntaxTree::BlockVar

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



2623
2624
2625
2626
2627
2628
# File 'lib/syntax_tree.rb', line 2623

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



2621
2622
2623
# File 'lib/syntax_tree.rb', line 2621

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



2615
2616
2617
# File 'lib/syntax_tree.rb', line 2615

def locals
  @locals
end

#locationObject (readonly)

Location

the location of this node



2618
2619
2620
# File 'lib/syntax_tree.rb', line 2618

def location
  @location
end

#paramsObject (readonly)

Params

the parameters being declared with the block



2612
2613
2614
# File 'lib/syntax_tree.rb', line 2612

def params
  @params
end

Instance Method Details

#child_nodesObject



2630
2631
2632
# File 'lib/syntax_tree.rb', line 2630

def child_nodes
  [params, *locals]
end

#format(q) ⇒ Object



2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
# File 'lib/syntax_tree.rb', line 2634

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



2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
# File 'lib/syntax_tree.rb', line 2646

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



2662
2663
2664
2665
2666
2667
2668
2669
2670
# File 'lib/syntax_tree.rb', line 2662

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