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.



2507
2508
2509
2510
2511
2512
# File 'lib/syntax_tree.rb', line 2507

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



2505
2506
2507
# File 'lib/syntax_tree.rb', line 2505

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



2499
2500
2501
# File 'lib/syntax_tree.rb', line 2499

def locals
  @locals
end

#locationObject (readonly)

Location

the location of this node



2502
2503
2504
# File 'lib/syntax_tree.rb', line 2502

def location
  @location
end

#paramsObject (readonly)

Params

the parameters being declared with the block



2496
2497
2498
# File 'lib/syntax_tree.rb', line 2496

def params
  @params
end

Instance Method Details

#child_nodesObject



2514
2515
2516
# File 'lib/syntax_tree.rb', line 2514

def child_nodes
  [params, *locals]
end

#format(q) ⇒ Object



2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
# File 'lib/syntax_tree.rb', line 2518

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



2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
# File 'lib/syntax_tree.rb', line 2530

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



2546
2547
2548
2549
2550
2551
2552
2553
2554
# File 'lib/syntax_tree.rb', line 2546

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