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.



2490
2491
2492
2493
2494
2495
# File 'lib/syntax_tree.rb', line 2490

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



2488
2489
2490
# File 'lib/syntax_tree.rb', line 2488

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



2482
2483
2484
# File 'lib/syntax_tree.rb', line 2482

def locals
  @locals
end

#locationObject (readonly)

Location

the location of this node



2485
2486
2487
# File 'lib/syntax_tree.rb', line 2485

def location
  @location
end

#paramsObject (readonly)

Params

the parameters being declared with the block



2479
2480
2481
# File 'lib/syntax_tree.rb', line 2479

def params
  @params
end

Instance Method Details

#child_nodesObject



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

def child_nodes
  [params, *locals]
end

#format(q) ⇒ Object



2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
# File 'lib/syntax_tree.rb', line 2501

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



2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
# File 'lib/syntax_tree.rb', line 2513

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



2529
2530
2531
2532
2533
2534
2535
2536
2537
# File 'lib/syntax_tree.rb', line 2529

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